Shop OBEX P1 Docs P2 Docs Learn Events
programming help — Parallax Forums

programming help

Dirty HowiDirty Howi Posts: 20
edited 2013-12-09 09:59 in BASIC Stamp
I am trying to send a message to my basic stamp using COM6 (its the port that's id'd by the software). I am sending a string of 4 characters, based on that i want a simple select to turn on one of three lights (to wit)
serStr VAR Byte(10)
serStr(9)=0
SERIN 1, 16468, [STR serStr\9]
SELECT serStr
CASE "ormd"
HIGH 15
PAUSE 2000
LOW 15
ENDSELECT

i can see the lights blink on the basic stamp proto module, so i know i'm sending something, unfortunately i cant run both at the same time (the BS2 debugger and my code)
        private void btnORMD_Click(object sender, EventArgs e)
        {
            var c = new communications();
            c.SendMessage("ormd");
        }

and to open a com port
  public  class communications
  {
      private SerialPort sp = new SerialPort();
      public communications()
      {
          sp.PortName = "com6";
          sp.BaudRate = 9600;
          sp.Parity = Parity.None;
          sp.DataBits = 8;
          sp.StopBits = StopBits.One;
          sp.Handshake = Handshake.None;

      }

      public void SendMessage(string message)
      {
          sp.Open();
          sp.WriteLine(message);
      }

  }
}

any help for the dumb puter programmer would be appreciated :D

Comments

  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2013-11-22 13:51
    The most common problem is a signal inversion issue... if you see light's then obviously you are wiggling something, but it may appear dead or you will see garbage characters in the monitor terminal. can you scope the data line? Between byte sends is the voltage level predominately high (TRUE) or is it low (INVERTED)?
  • Dirty HowiDirty Howi Posts: 20
    edited 2013-11-22 14:00
    i'm sending this via a windows app i wrote (quick and dirty), if i open the program in the debugger, i cant talk to the port since the debugger has a hold of it, if i dont run the debugger, my software works (flashes lights next to the microcontroller) but the led wired to p15 does not come on. I'm using the built in USB cable to communicate via COM6. I dont have a scope or meter available to check the levels (i'll have to dig around in the tool box LOL). Not had a scope available in oh 30 years or so.

    Anyway to send the command via the debugger (i'm still reading the books)
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2013-11-22 14:27
    Just for kicks, try "84" instead of "16468" on the Basic Stamp.... 84 - is 9600 baud non-inverted 16468 - is 9600 baud inverted ... both are 8 bit no-parity
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2013-11-22 14:49
    Also... the expression for the select case is a variable, constant or an expression. Pointing to a string might not be valid. Depending on exactly what you want to accomplish, the WAIT condition might be more suitable.
  • Dirty HowiDirty Howi Posts: 20
    edited 2013-11-22 14:58
    i'll give that a shot (the 84) when i get it all hooked back up, took it apart to bring it home from the office :) off next week. will post results.
  • Dirty HowiDirty Howi Posts: 20
    edited 2013-11-25 18:13
    no joy...hmm....all i want to send is one of three values, ormd, ormdw, haz. One for each of the three lights. Its just a proof of concept to show the company owner...this should not be this hard.

    anyone have a example of talking to a basic stamp via .NET (VB or C# dont matter to me, i read/write both and can translate between them)
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-11-26 08:22
    Hi, this line
    SERIN 1, 84, [STR serStr\9]
    

    receives an array of nine bytes. If the array is less than nine or if the length of the array is going to vary then you would need to transmit an end of line character or your Stamp program is just going to hang there.

    For example attaching a carriage return as the end of line would be handled as follows by the Stamp
    SERIN 1, 84, [STR serStr\9\CR]
    

    this would inform the Stamp that a complete string had been received and we can move on.

    Secondly serStr is an array of byte variables and the values or string that it contains must be extracted one byte at a time using the index of the element eg: serStr(0) ,serStr(1)......... so Select Case will not work on the string as a whole only the individual elements.

    As proof of concept try your program with just a one byte variable to begin with.

    The following link is a 2007 post that has a few useful references in it although I now know there is plenty of room for improvement.


    http://forums.parallax.com/showthread.php/96973-VB-Express-to-Stamp-Template


    Jeff T.
  • Dirty HowiDirty Howi Posts: 20
    edited 2013-11-26 14:18
    i'll get my sign out now lol. i'll build an enum to pass one byte values to test it. i'll also look at the links when i get home. after im done feeling like an embicile :)

    thanks
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-11-26 15:04
    I certainly don't believe you are an imbecile although I know too well the feeling <g>

    Your project is the kind I get into and the kind I have struggled with in the past.

    What you are attempting is well within the capabilities of the Stamp and you have probably witnessed already the willingness to help by the members of this forum.

    So just keep posting your questions and you are guaranteed a helpful response.

    Jeff T.
  • Dirty HowiDirty Howi Posts: 20
    edited 2013-11-28 07:14
    i've an odd feeling i know this, but if i send a "message" via the C# program as a string it has to go as "1" which on the other end in computerdom is interpreted as 1 not "1"; is the stamp seeing "1" and not 1? if that's the case then am i better off sending it as an int?

    edit: never mind the int, the serial port class writeline method will only accept a string
  • UnsoundcodeUnsoundcode Posts: 1,532
    edited 2013-11-28 08:18
    Hi, the three main options are

    1) write a binary array of one or more values
    Dim mybyte() As Byte = Nothing
            mybyte(0) = 1
            SerialPort1.Write(mybyte, 0, 1)
    'Stamp receives 1
    

    2) write a string of characters as ASCII character codes with a NewLine character appended. The default newline character has a value of decimal 10 but can be modified with the serial port method.
    Dim terminated_string As String = "1"
            SerialPort1.WriteLine(terminated_string)
    'Stamp receives "1" and then 10
    

    3) write a string of characters as ASCII character codes without the NewLine appended.
    Dim non_terminated_string As String = "1"
            SerialPort1.Write(non_terminated_string)
    'Stamp receives "1"
    

    When VB transmits ASCII characters, for example "1" is transmitted as the character code value of 49, then the Stamp must be set up to format the incoming data from a binary value to a string.

    1) this accepts a transmitted value of "1" and stores 49 into the variable myvar.
    SERIN port,baud,[myvar]
    

    2) this accepts a transmitted value of "1" and stores 1 into the variable myvar.
    [code]SERIN port,baud,[DEC myvar]
    

    Formatters such as DEC and HEX take ASCII string character codes and convert them into their respective values at the Stamp.

    Jeff T.
  • Dirty HowiDirty Howi Posts: 20
    edited 2013-12-08 11:47
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim terminated_string As String = "1"
            SerialPort1.Open()
            SerialPort1.WriteLine(terminated_string)
            SerialPort1.Close()
    
        End Sub
    

    what is in the button (i even moved over to VB since that's what your using)
    program in the stamp itself
    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    DEBUG "Program Running"
    
    serStr VAR Byte
    
    SERIN 1,82, [STR serStr \1]
    
      HIGH 15
      PAUSE 2000
      LOW 15
    

    very simple wait for ANYTHING to come across, and light up the light...still no joy :(:(:(

    and its snowing in iowa, and i have nothing else to do today cept play with this so...
  • Dirty HowiDirty Howi Posts: 20
    edited 2013-12-08 12:27
    ok, its official i'm an idiot.

    when dealing with something new it pays to RTFM :)

    SERIN 16, 84 [DEC strStr] works...it helps if you set the rpin portion of the command to the right dad gum pin LOL.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2013-12-09 09:59
    Glad to see you figured it out and got it working. :thumb:
Sign In or Register to comment.