Shop OBEX P1 Docs P2 Docs Learn Events
Send string through serial port — Parallax Forums

Send string through serial port

mytraxmytrax Posts: 2
edited 2012-05-12 21:39 in Propeller 1
Hi,

I burned my brain since two days looking for a solution !
I got some trouble with RS232 communication. I wrote a VB.net program in order to send to the propeller the coordinates of a joystick connected to my PC . I can receive those data but I can't differentiate them. Take a look :

Part of my VB.net Prog :

I send those informations to my propeller :

RS232.WriteLine(state.Y)
RS232.WriteLine(state.X)
RS232.WriteLine(state.GetPointOfView(0))
RS232.WriteLine(state.GetSlider(0))
RS232.WriteLine(state.Rz)

And I receive them with the "Full-Duplex_COMEngine" object like that :

serial.readString(@data, 512)

The problem is that I can't differentiate all the incoming datas. I would like to put state.Y , state.X ... in differents VAR. Or maybe in an array.

I tried that :

repeat until index == 4
char := serial.readString(@data, 512)
str[index] := char
index++

but it doen't works

Could you help me please ? Sorry but I'am a newbie :-D

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-06 15:35
    What does the data look like coming out of the PC? How is it formatted?

    It helps if you use code tags when posting code. Here's a link to learn how.

    attachment.php?attachmentid=78421&d=1297987572
  • turbosupraturbosupra Posts: 1,088
    edited 2012-05-06 16:34
    You need to make sure the vb.net is sending at the same protocol values that the receiving object on the propeller is expecting, say it is fullduplexserial or the parallax serial terminal, 8N1, 8 bits, no parity, 1 stop bit.

    I do this with code like this, if you need more specific help, we'll need more specifics and specific code
    l_myReceivedByte := pst.RxCheck
           
          if l_myReceivedByte <> -1
            b_waitingToBeParsed[b_byteMoveIndex++] := l_myReceivedByte
           
            ' pst.str(@b_waitingToBeParsed)
            ' pst.str(string(pst#NL))
           
            if l_myReceivedByte == 13
              DelimiterFinder(@b_waitingToBeParsed)
              ByteFill(@b_waitingToBeParsed, 0, strsize(@b_waitingToBeParsed))
              b_byteMoveIndex := 0
    
    PUB DelimiterFinder(RxStringAddr) : idx | localCount, localIndex, pstDelimiterFinder                                            
    
      ' Initialize variables here
      localIndex := 0
      pstDelimiterFinder := 0
    
      repeat
        ifnot localCount := byte[RxStringAddr][idx++]                ' ifnot c, itterate to the next byte in byte[RxStringAddr] ?
        
          printAt( 1,12, String("No delimiter, aborted"))
          return -1
    
        charAt(1+idx,13,localCount)
        ' pst.dec(localCount)
        ' pst.str(string(pst#NL))  
      until localCount == b_delimiter'localCount == "="
     
      bytemove(@b_prefix, RxStringAddr, --idx)              ' idx points to the character after the delimiter, copy prefix
      b_prefix[idx] := 0                                    ' add terminator  
      RxStringAddr += idx + 1                               ' advance pointer, skip delimiter (+1)                                                 
      bytemove(@b_suffix, RxStringAddr, strsize(RxStringAddr) +1)                   ' copy tail including the existing terminator (+1)
      variableUpdator(@b_prefix, @b_suffix)
    
    
    
  • mytraxmytrax Posts: 2
    edited 2012-05-08 11:29
    Hi,

    Thanks for your answer, but I'a code, I'am a real newbie !
    For more informations ; in fact I would send that through my pc : "122|999|647|455" and catch all numbers between "|" and put them in differents variables.

    I tried to wrote my code ; take a look :

    PUB StrParse(strAddr, start, count)


    count <#= constant(STR_MAX_LENGTH - 1)
    bytemove(@ostr, strAddr + start, count) ' just move the selected section


    ostr[count] := 0 ' terminate string
    RETURN @ostr


    Pub Parse(Mystr) | BarPos, FindStr, nextstr, count, numletter, field, letter


    count := 0
    field := 0
    numletter := 0
    BYTEFILL(@parsed, 0, 40)
    BYTEFILL(@cmdfield, 0, 40)


    repeat strsize(Mystr)


    letter := StrParse(Mystr,numletter,1)


    if parsed[count] == "|"
    cmdfield[field]:= @parsed
    field++
    count := 0
    BYTEFILL(@parsed, 0, 40)


    else
    parsed[count] := letter
    count++
    serial.str(parsed[count])
    serial.newline

    numletter++


    But it doen't worl :'( , I think I'am near the solution ! Help me please ^^ !
  • turbosupraturbosupra Posts: 1,088
    edited 2012-05-12 20:55
    Sorry Mytrax,

    I just saw your reply. Try the example I've attached and see if you can adapt it to what you'd like to do.

    If you type "bob=100" you'll see it parse through the data and give the values before the equals sign and after the equals sign.

    Let me know how this works for you.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2012-05-12 21:39
    mytrax wrote: »
    in fact I would send that through my pc : "122|999|647|455" and catch all numbers between "|" and put them in differents variables.

    It would help if you took another look at the code tags link I posted earlier. Using a quote block doesn't keep the indentation that is so important in Spin.

    I see you want to use "|" as a delimiter but you're sending values that you want stored in certain variables right? What indicator is there to indicate a new message? What are the end of messae charaters?

    It's common to use a carriage return to indicate an end of message. It would also help if you had some sort of beginning of message indicator.

    Is the data "122|999 etc" sent as ASCII characters? Where "122" is three bytes?

    You mentioned you want the data assigned to various variables. In your example where you're sending four numbers, do you want to keep reusing the same four variables (such as (I'm just making these up) "xAxis", "yAxis", "zAxis" and "buttonValue")?
Sign In or Register to comment.