Shop OBEX P1 Docs P2 Docs Learn Events
Help with serial appretiated... — Parallax Forums

Help with serial appretiated...

OlovAOlovA Posts: 23
edited 2008-11-25 23:29 in Propeller 1
Hi. I would like to receive 8 byte's of binary data, i dont have a special start or stop character.

I just must wait for the data to come and then i want to store my data and clear the receive buffer and wait again...

I have tried a lot, but i just cant get it work perfectly...

Would appretiate some help / Olov, Sweden.

Comments

  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-23 09:24
    Olvo -

    Have you looked at the FullDuplexSerial Object? This is a great object for sending and receiving bytes. This object does the send/receive and you can read the buffer to get your data.

    Tell us more about what you tried. What device is on the other end of the serial line?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
  • OlovAOlovA Posts: 23
    edited 2008-11-23 09:55
    Thank's for your answer, it looks like this...
    It works sometime, but sometime i only get back 5 characters to the PC, wouldn't that be impossible
    because of my tx-loop ???
    I tell the prop. to send 0 to 7 in my tx-loop ???



    CON
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    VAR
    byte DataStr1[noparse][[/noparse]8],Ptr,x


    obj
    Serial : "FullDuplexSerial"

    PUB main

    Serial.start(31,30,0,9600)

    repeat

    Ptr := 0

    Repeat
    repeat
    DataStr1[noparse][[/noparse]ptr] := Serial.Rx
    while DataStr1[noparse][[/noparse]ptr] == -1 'Here i want the prop to wait for character in receive buffer
    ptr++ ' Then increase my pointer
    while Ptr < 8 ' Until 8 characters received ( 0 to 7 )

    repeat x from 0 to 7
    Serial.tx(dataStr1[noparse][[/noparse]x]) ' Then send it back to the PC
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-23 09:59
    Can you post the file for your code or post the code using the "code comment" blocks so we can see the proper formatting of the code? In the post reply section you should be able to setup brackets with the word code and paste your text there.

    Like this....

    Code goes here
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
  • OlovAOlovA Posts: 23
    edited 2008-11-23 12:05
    Code
    CON
    _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    VAR
    byte DataStr1[noparse][[/noparse]8],Ptr,x


    obj
    Serial : "FullDuplexSerial"

    PUB main

    Serial.start(31,30,0,9600)

    repeat

    Ptr := 0

    Repeat
    repeat
    DataStr1[noparse][[/noparse]ptr] := Serial.Rx
    while DataStr1[noparse][[/noparse]ptr] == -1
    ptr++
    while Ptr < 8

    repeat x from 0 to 7
    Serial.tx(dataStr1[noparse][[/noparse]x])
  • OlovAOlovA Posts: 23
    edited 2008-11-23 12:09
    (CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
    VAR
      byte DataStr1[noparse][[/noparse]8],Ptr,x
      
    obj
      Serial  : "FullDuplexSerial"
      
    PUB main
      
      Serial.start(31,30,0,9600)
      
      repeat
        Ptr := 0
        
        Repeat
          repeat
            DataStr1[noparse][[/noparse]ptr] := Serial.Rx
          while DataStr1[noparse][[/noparse]ptr] == -1    'Here i want the prop to wait for character in receive buffer
          ptr++
        while Ptr < 8                  ' Then increase my pointer
        repeat x from 0 to 7
          Serial.tx(dataStr1[noparse][[/noparse]x])       ' Then send it back to the PC
          
    
  • StefanL38StefanL38 Posts: 2,292
    edited 2008-11-23 19:42
    Hello Olov,

    also take a look into the Extended_FDSerial-object. It uses FullDuplexSerial as some mind of a "base-object"
    It has already a amtheod for receiving strings
    this method uses delimiters but as in your case you have 8 bytes all the time you can change the condition

    you don't have to wait extra for bytes beeing received rx himself waits until there comes something in

    her's the code from Extended_FDSerial

    'modified for a fixed size of 8 characters
    PUB RxStr (stringptr) : Value | ptr
    {{
      Accepts a string of 8 characters  to be passed by reference
      String acceptance terminates after 8 characters
      Serial.Rxstr(@MyStr)        ' accept
      serial.str(@MyStr)          ' transmit
     }} 
        ptr:=0
        bytefill(@dataIn,0,8)                               
       dataIn[noparse][[/noparse]ptr] :=  Rx        
       ptr++                                                  
       'repeat while (ptr < 8)       
           dataIn[noparse][[/noparse]ptr] :=  RX    
           ptr++
       dataIn[noparse][[/noparse]ptr-1]:=0                                         
       byteMove(stringptr,@datain,8)  
    
    
    'original code
    PUB RxStr (stringptr) : Value | ptr
    {{
      Accepts a string of characters - up to 15 - to be passed by reference
      String acceptance terminates with a carriage return or the defined delimiter characters.
      Will accept up to 15 characters before passing back.
      Serial.Rxstr(@MyStr)        ' accept
      serial.str(@MyStr)          ' transmit
     }} 
        ptr:=0
        bytefill(@dataIn,0,15)                               
       dataIn[noparse][[/noparse]ptr] :=  Rx        
       ptr++                                                  
       'repeat while ((DataIn[noparse][[/noparse]ptr-1] <> Delimiter2) and (DataIn[noparse][[/noparse]ptr-1] <> Delimiter)) and (ptr < 15)       
           dataIn[noparse][[/noparse]ptr] :=  RX    
           ptr++
       dataIn[noparse][[/noparse]ptr-1]:=0                                         
       byteMove(stringptr,@datain,16)  
    
    
    



    best regards

    Stefan
  • OlovAOlovA Posts: 23
    edited 2008-11-23 21:02
    Thank you wery much Stefan. I will try it out. I have been "experimenting" a lot with code today, right now i am really confused freaked.gif
    (CON
      _clkmode = xtal1 + pll16x
      _xinfreq = 5_000_000
      
      MaxStrSize = 20
    VAR
      byte DataStr1[noparse][[/noparse]MaxStrSize],Ptr,MyTestVar,Slav,ModFunk,H_Start,L_Start,H_Antal,L_Antal,L_CRC,H_CRC
      Byte Rx,TmpHi,TmpLo,CRC_Tmp,CRC_i,x
      word CRC 
      long CogStack1[noparse][[/noparse] 20]
     
    obj
      Serial  : "Extended_FDSerial"
     
    PUB main
      dira[noparse][[/noparse]17]~~
      Serial.start(6,5,0,9600)
      
      repeat
        Ptr := 0
        
        
        
        repeat 
          DataStr1[noparse][[/noparse]ptr] := Serial.Rx
          
          Slav    := DataStr1[noparse][[/noparse]0]
          ModFunk := DataStr1[noparse][[/noparse]1]
          H_Start := DataStr1[noparse][[/noparse]2]
          L_Start := DataStr1[noparse][[/noparse]3]
          H_Antal := DataStr1[noparse][[/noparse]4]
          L_Antal := DataStr1[noparse][[/noparse]5]
          L_CRC   := DataStr1[noparse][[/noparse]6]
          H_CRC   := DataStr1[noparse][[/noparse]7]
          ptr++
        while Ptr < 8  
          
        Ptr := 0
        Serial.RxFlush
        'waitcnt(1_000_000+cnt)
        repeat CRC_i from 0 to 7
            Serial.tx(dataStr1[noparse][[/noparse]CRC_i])
        !OutA[noparse][[/noparse]17]  
        Serial.tx("A")
    )
    

    This code should just do that job, and then send it back to the pc. Okay it does, BUT...
    As u can see i toggle P17 just to see that "i have been there", That works too, but the last transmit "Serial.tx("A")
    That one comes at the next cycle !!! I dont get it...
    As u might now, i just started with the prop. i have done some programming with Microchip before...
    No, i cant understand why my "Big A" comes at the next cycle and not directly after my toggle of P17
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-24 11:07
    How are you getting along with this? Is the "A" showing up later because of the terminal program you are using?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
  • OlovAOlovA Posts: 23
    edited 2008-11-24 12:01
    Hi, yes it's really strange isn't it ? I have tried two different terminalprogram's
    #1 HHD Free serial port monitor
    #2 Device monitoring studio

    I use this program's to monitor my serial activity, then i'll use "Artsoft Mach 3, Modbus Serial control monitor" to send the message. I'm trying to create a modbus client.
    I have done this with microchip's 16F876,77 and that worked just fine, but now i want more "power" ( cog's ) so that's why i want to use the propeller...

    When i use the second program, "Device monitoring studio", then i'll get the timestamps too from the serial activity so i can see that it is like this...
    Can my prop be broken ??? I really dont think so, but ???
    If u guys just try this small program and some terminalprogram, do u get the same result ???
    Thank's / Olov...
  • OlovAOlovA Posts: 23
    edited 2008-11-24 12:04
    Ohh, i should tell u that i send hexadecimal digits to the prop.
  • OlovAOlovA Posts: 23
    edited 2008-11-24 12:06
    One more thing... I even tried with different serial ports, and with prop-stick, and with a max232 to eliminate the thought's about USB-Serial conversion...
    Same result!!!
  • Mike GreenMike Green Posts: 23,101
    edited 2008-11-24 14:29
    FullDuplexSerial is buffered. That means that the 'serial.tx("A")' call puts the "A" into the buffer, but it may be a little while (microseconds if it's not transmitting something else) before it actually gets transmitted, then the transmission itself takes about a millisecond at 9600 Baud.

    The simple solution is to put a delay of a few milliseconds after the last 'serial.tx' call like 'waitcnt(clkfreq/500 + cnt)'

    The more complex solution is to use a modified version of FullDuplexSerial with a routine (method) that waits for the output buffer to become empty, then waits for the last character to be transmitted completely.

    Post Edited (Mike Green) : 11/24/2008 2:36:30 PM GMT
  • OlovAOlovA Posts: 23
    edited 2008-11-24 21:16
    Hi Mike, MANY thank's for the answer. Where can i find this kind of answer's ? ( Ofcourse from u! ) But is there some document that explains this kind of behaviours somewhere ?
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-11-24 21:49
    Here's a simple approach. Without a start or stop character, your receiver routine may not be aligned with the series coming from the PC. It would help if you had a way to synchronize at the message level.

    repeat
      repeat idx from 0 to 8
        DataStr1[noparse][[/noparse]idx] := serial.rx
      serial.rxflush
      repeat idx from 0 to 8
        serial.tx(DataStr1[noparse][[/noparse]idx])  
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • OlovAOlovA Posts: 23
    edited 2008-11-24 23:39
    Now i know my problem! I receive and send hex, for example,
    Serial.tx($01)
    Serial.tx($04)
    Serial.tx($00)
    Serial.tx($00)
    Serial.tx($00)
    Serial.tx($01)
    Serial.tx($31)
    Serial.tx($CA)
    But that really makes "FullDuplexSerial.spin" confused
    This sequence is ok, but not that one above... I dont know how to make it work, i cannot use "Serial.hex" because it converts to
    for example "01" to ascii 31
    Serial.tx($01)
    Serial.tx($06)
    Serial.tx($00)
    Serial.tx($00)
    Serial.tx($00)
    Serial.tx($00)
    Serial.tx($89)
    Serial.tx($CA)
    So, does anyone know how to transmit like this ?
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-25 05:17
    OlovA -

    I think you are getting a couple things confussed. I believe the code you have above is truely sending the hexadecimal you want to send. I mean, if you have an oscilloscope or a logic analyzer or even the View Port (free 30-day trial - find it in the other threads - it is way cool) you will see that the serial pin is indeed transmitting your hex out.

    The problem is reading the hex on the terminal program. The terminal program sees $01 and interprets it as "Start of heading" and doesn't dispaly anything. However, if you are using the "serial.hex" I believe it is taking your hex value and converting it to ASCII readable characters to show in the terminal program. This explains why $01 is translated to "1" in ASCII which is $31.

    Does this make sense? Search out ViewPort, it looks like a great tool. I am about to start using it too.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-25 05:22
    Oh - it is cool you are creating a Modbus client. What is the application you are going to use it in?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
  • OlovAOlovA Posts: 23
    edited 2008-11-25 11:50
    Hi, My terminalprogram does show... It might be a problem in the "Serverprogram", I think i must "hook up" another PC to analyze what's really happening...
    I can take a screenshot later and show it here...

    I'm going to use this application along with Mach3, That is a CNC-software for PC.
    It work's really nice with my Microchip Pic16F876 and 877, so it should work...
  • OlovAOlovA Posts: 23
    edited 2008-11-25 19:59
    PROBLEM SOLVED!
    I found it! It was "rubbish behind the keyboard", my own fault, i sent two bytes to much back to the serverprogram, then the checksum was ofcourse illegal...
    That's life, anyway, many thank's for the lessons in propeller programming, i'm sure i'll be back with more questions, maybe i can assist someone someday.
    Now i can continue my project, that is a small Modbus-client, some digital in/out's and servocontrol, communication via RS485, multidrop...
    I really start to like the prop.
    Thank's / Olov
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-25 23:29
    OlovA -

    This is great to hear that the problem was solved! Excellent! You are going to control a CNC machine with it? I have heard of Mach3 but I have not used it. I bet there are others interested on the forum in CNC and using the Propeller as a controller.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
Sign In or Register to comment.