Rceived value by serial communication
qiuqiuaaa
Posts: 37
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:
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
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".
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,
Instead, just use:
And modify your variables in the SERIN statement as follows (assuming the data is coming in highbyte/lowbyte):
Now the variables are complete and you no longer have to do the following math (remove these lines as well):
The program is smaller, you saved 4 bytes in variables and it should run much faster. I hope this helps.
And I sent "A" before the series.
Combine the lines from this:
Into (adding your prefix):
This will save a bunch more code space since SERIN commands take up a lot of memory, relatively speaking.