Hi,
We are trying to read data from sensor. Each sensor output reading is in three data bytes. The first byte indicates the validity of the reading (i.e. Valid or Invalid), the second byte contains the upper 7 bits of the reading plus a header bit and the third byte contains the lower 7 bits of the reading plus a header bit.
We have written a code shown below, which receives the data from sensor and display it on the terminal window of basicmicro studio. We are sending sensor data to the pin 10 and then used serin command to receive the data. Then we used serout command to send the data to computer.
[code]RX con 10
ht var word
main
serin RX,i9600,[bin ht]
serout s_out, i9600,[bin ht]
goto main[/code]
But we are not receiving anything on the terminal window. Is this correct command to read binary data? Plz do reply.
Thank you.
I’m unfortunately not verify familiar with programming Basic Atom, but I see that a “Word” is 16 bits in BAP40 so not big enough for your 3-byte message. This might not be important at this point since you’re looping and will catch the missing byte in the next loop.
Is your sensor continuously sending the 3-byte message?
Have you double checked that both your sensor and your host computer are both using 9600 baud, no parity, inverted? Have you tried just sending fixed data from your BAP40 to your computer to verify this portion of the communication is working properly?
Thank you for your reply.
It is Miniature Radar Altimeter.
This sensor sends 3bytes data for every 100ms. We have checked the baud rate, it is same 9600 for both sensor and computer. We tried sending fixed data from BAP to computer
x var word
serin s_in,i9600,[dec a]
serout s_out, i9600,[dec a]
For the above code it is displaying ‘x’ value on the terminal window.
Is there any function which reads binary data? We saw shifin function, but it works for synchronous serial device. But our sensor asynchronous serial device.
Sorry I don’t monitor this forum as often as I used to… Also I have done any programming for a basic micro for a long while now.
You say you are reading in 3 bytes, so my assumption is that this is binary data not Ascii characters. That is for example the first byte is not a series of ASCII “0” and “1” characters making up the binary value, likewise the value, is two bytes either high byte low byte or other way…
If true, remove the dec modifier. It works by reading in the ASCII string and as long as the characters are in the ASCII range of “0” to “9”, it will multiply the value up to then by 10 and add in the Ascii value -“0”.
so yes you can use serin to read in binary data that is
serin s_in, i9600, [a]
will read in one byte into the variable a. If you need to read in a word value, you can do it something like:
serin s_in, i9600, [a.lowbyte, a.highbyte]
Might have to reverse depending on your byte order.
If you need to read in multiple bytes into an array, you can use the str modifier, and specify how many bytes you expect…
REV function is used to “reverse” the value of the low order bits of a number (i.e. change 0’s to 1’s and vice versa). Example:
x var byte
y var byte
x = %101110 ;this is decimal 46
y = x rev 3 ;gives g a value of %101001 or 41
I have read above function with example in Basicatom pro manual. It is not reversing the bits of a byte.
I tried using shift left operator. But how I can save Lsb of the original byte into msb of the reversed byte?