09-16-2010 01:07 AM
Hi,
I wish to read some data from a DSP through RS232 to my VI. The DSP will send the data continuously. There are basically three components, which are position x, position y and position z. All three are 2 bytes each. Therefore the sequence of the data will be "posXposYposZposXposYpoZposX....."
How can i decompose the incoming string data into I16 of posX, posY, posZ, i.e. to convert the string to integer and at the same time separate them to I16 each. Is it possible to achieve this? Please advise. Thank you.
09-16-2010 09:35 AM
The Initialize and Close should be outside the loop.
It's easy enough to convert a string to an array of I16 values - the Type Cast function can do that. However, in order to be able to correctly parse the data you're getting you need to have some way of knowing the start of the three numbers. In other words, you need some form of synchronization. Since each one is two bytes you have no way to know how many numbers you're actually going to get. What happens if all you get on the serial port is 5 bytes? Is that a PosX, PosY and half of PosZ, or is it half of the last PosX, a PosY, and a PosZ? Without some form of synchronization marker there's no way to know.
09-16-2010 12:39 PM - edited 09-16-2010 12:39 PM
Smerc: The OP stated that all values are 2 bytes each. So there should not be a case where you would get half of a 16-bit value. You should always get an even number of bytes.
The first byte would be converted to a numeric U8. The next byte would also be converted to a numeric U8. Then the two U8s could be joined using Join Numbers to form a 16-bit numeric. What needs to be known is the order of the bytes. Is the first byte the most significant or least significant? In other words, is the format big endian or little endian?
09-16-2010 12:43 PM
Even with getting a consistent 16 bytes and even with knowing whether it is big or little endian, the synchronization issue remains if the transmission is as the op describes.
09-16-2010 01:08 PM
@tbob wrote:
Smerc: The OP stated that all values are 2 bytes each. So there should not be a case where you would get half of a 16-bit value. You should always get an even number of bytes.
The first byte would be converted to a numeric U8. The next byte would also be converted to a numeric U8. Then the two U8s could be joined using Join Numbers to form a 16-bit numeric. What needs to be known is the order of the bytes. Is the first byte the most significant or least significant? In other words, is the format big endian or little endian?
All values may be 2 bytes long, but there's no guarantee that you'll get an even number of bytes over a serial port.
09-16-2010 10:20 PM
oh, even if the DSP is constantly sending I16 data over, i.e. posX, posY and posZ are always sent in I16, i may not be able to receive the exact I16 data without synchronization?
09-16-2010 10:43 PM
That's not how serial transmission works. Bytes are sent over the serial port one at a time. One byte doesn't know that it has to "go along" with the one behind it. Once it's sent over a serial interface the UART at the receiving end just sees a stream of bytes. It doesn't see I16 numbers, or any other kind of number of any size. It just sees a sequence of bytes, and sends that information to the operating system. The UART could get 1 byte or it could get 50 or it could get 200 - it depends on the speed of the transmission and the buffer size of the UART.
09-16-2010 10:43 PM
No. How can you possibly tell one I16 from another since the start of your read will happen at any time. If you used a specific termination after the z data (and a code that would not appear in your data, you would simply read until that character was detected and after that, read 6 bytes at a time.
09-16-2010 10:52 PM
Hi smercurio_fc,
I understand your point that bytes are sent one at a time. If the DSP is continuously sending I16 data, won't it actually 'queue' up in bytes? i.e. for example, i have X, Y and Z coming in in this sequence continuously. so at my receiving end, will it be X(I8),X(I8),Y(I8),Y(I8),Z(I8),Z(I8),X(I8),X(I8),Y(I8),Y(I8).... etc?
09-16-2010 10:57 PM
That would only work if you start the read before the DSP starts transmitting and that you can keep up with the transmission speed. Can you guarantee this? Otherwise you have no way of knowing where you are in that sequence.