Shop OBEX P1 Docs P2 Docs Learn Events
Rceived value by serial communication — Parallax Forums

Rceived value by serial communication

qiuqiuaaaqiuqiuaaa Posts: 37
edited 2012-04-03 00:27 in BASIC Stamp
Since I want to receive sequential values, and give them to two variables. Each number is larger than 255, so I transfer them into two bytes (high byte and low byte).
For example, high1 = 8, low1 = 23, high2 = 7, low2 = 245. I calculate the final value ser1 = high1*256+low1, ser2= high2256+low2. Sometimes, the values are in wrong order, like high1 = 245 , low1 = 8, high2 = 23, low2 = 7,so the result is also wrong. ( I guess it's because new "high1" has not arrived, it is only the old "low2", I don't know whether it's correct)
The code is below:
' {$STAMP BS2px}
' {$PBASIC 2.5}
serDatahigh1 VAR Byte
serDatalow1 VAR Byte
serData1 VAR Word
serDatahigh2 VAR Byte
serDatalow2 VAR Byte
serData2 VAR Word
'serData VAR Word

noSerialData:
DEBUG "  Hello "

again:
SERIN 0, 813,10000,noSerialData,[serDatahigh1]            
SERIN 0, 813,10000,noSerialData,[serDatalow1]
SERIN 0, 813,10000,noSerialData,[serDatahigh2]            
SERIN 0, 813,10000,noSerialData,[serDatalow2]

serData1 = serDatahigh1 * 256 + serDatalow1
serData2 = serDatahigh2 * 256 + serDatalow2
DEBUG ? serData1
DEBUG ? serData2
DEBUG ? serDatahigh1
DEBUG ? serDatalow1
DEBUG ? serDatahigh2
DEBUG ? serDatalow2
'PAUSE 20
GOTO again

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2012-03-30 06:05
    Can you make your transmit device send a ! followed by high_byte then low_byte (or however you want to organise it)?
    Then have the Stamp WAIT for "!", and then take in the high_byte followed by the low_byte.
    Maybe I should think about that more, ! = $21 (33), so if the device sent out: ! , 33 (high_byte), 33 (low_byte)... ?
    Maybe you should send the "!" and then ASCII characters for the numbers, then you could use the formatter to screen out non-numeric characters? IOW, if the high_byte = 210 then send "2", "1", "0" and if it was = 21 then send "0", "2", "1".
  • stamptrolstamptrol Posts: 1,731
    edited 2012-03-30 06:10
    Your reasoning is OK.

    The issue with using 4 SERIN statements is that you have no way of knowing if a byte in the expected sequence has been missed. The system just grabs whatever 1 byte that appears as each SERIN is executed.

    I would suggest two things:

    Use a single SERIN and load the bytes into an array. That way, at least you really will be grabbing 4 sequential bytes. You could then do some tests to see if the values are valid.

    Secondly, as the sequential bytes arrive, is there some identifying lead character? That would allow use of the WAIT modifier and help ensure the subsequent bytes are in the right order.

    Cheers,
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-03-30 11:02
    Another thing you can do to speed up the program and make things more clear is to make the recombination of the Word variable automatic. Once you've implemented the advice others have given so you know where the actual start of your data is you can simplify things by getting rid of the following lines in your code:
    serDatahigh1 VAR Byte
    serDatalow1 VAR Byte
    serDatahigh2 VAR Byte
    serDatalow2 VAR Byte
    

    Instead, just use:
    serData1 VAR Word
    serData2 VAR Word
    

    And modify your variables in the SERIN statement as follows (assuming the data is coming in highbyte/lowbyte):
    again:
    SERIN 0, 813,10000,noSerialData,[serData1.highbyte]            
    SERIN 0, 813,10000,noSerialData,[serData1.lowbyte]
    SERIN 0, 813,10000,noSerialData,[serData2.highbyte]            
    SERIN 0, 813,10000,noSerialData,[serData2.lowbyte]
    

    Now the variables are complete and you no longer have to do the following math (remove these lines as well):
    serData1 = serDatahigh1 * 256 + serDatalow1
    serData2 = serDatahigh2 * 256 + serDatalow2
    

    The program is smaller, you saved 4 bytes in variables and it should run much faster. I hope this helps.
  • qiuqiuaaaqiuqiuaaa Posts: 37
    edited 2012-04-02 00:22
    Thank you all, with your suggestions I found a way:
    SERIN 0, 813,10000,noSerialData,[WAIT("A"), serData1.HIGHBYTE] 
    

    And I sent "A" before the series.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2012-04-02 08:42
    Now that is is clearer to you, you could also do something like this:

    Combine the lines from this:
    again:
    SERIN 0, 813,10000,noSerialData,[serData1.highbyte]            
    SERIN 0, 813,10000,noSerialData,[serData1.lowbyte]
    SERIN 0, 813,10000,noSerialData,[serData2.highbyte]            
    SERIN 0, 813,10000,noSerialData,[serData2.lowbyte]
    

    Into (adding your prefix):
    again:
    SERIN 0, 813,10000,noSerialData,[WAIT("A"), serData1.highbyte, serData1.lowbyte, serData2.highbyte, serData2.lowbyte]
    

    This will save a bunch more code space since SERIN commands take up a lot of memory, relatively speaking.
  • qiuqiuaaaqiuqiuaaa Posts: 37
    edited 2012-04-03 00:27
    That's a great point. Thank you so much.
    This will save a bunch more code space since SERIN commands take up a lot of memory, relatively speaking.
Sign In or Register to comment.