11-13-2013 06:40 AM
I want to read serial data from my hardware. Messages are constructed like:
Start byte (1 byte) (0xFF)
Message type (1byte)
Data lenght (1byte)
Data (N bytes)
Checksum (1 byte)
Hardware sending this messages continious.
So my question is Which way for reading is more effective:
1. Read one byte and find start of message (read while value is not 0xFF), then read second byte and analyze and then third byte etc. So you need to read serial port at least 4 times per message.
2. Read 100 bytes - or all available bytes at once and then iterating through it using while loop and shift register.
I think that each visa read use computer resource, but also managing shift registers use resource especially if there is a lot of data. So which way is better?
Kind regards
11-13-2013 07:28 AM
What I have done in the past is read all data available at the port. I use concatinate strings to append this read data to my buffer stored in a shift register. I then use Search/Split String to find the sync byte. I can then look at the command byte and byte count using Substring. Do whatever other processing you need to do from there. I then used Substring to remove the processed command from my buffer. This buffer is stored in a shift register.
That setup worked pretty well for what I needed. I believe I found that the VISA Read was the slow part, so I wanted to read as much data as possible at one time.
11-13-2013 09:32 AM
Multiple VISA reads is not a problem. It isn't using "computer resources". And shift registers aren't really using any computer resources that you'd need to worry about.
What I do is assume that the buffer is clear when I open VISA. Then every write command returns a full response, and I'm not getting any other data in the serial port that would interfere with the response, or getting unsolicited messages between write commands.
Read 3 bytes. Check that the first byte is FF. (If it's not, then you'll have to do figure out how to sort out the error.
Get the value of the 3rd byte. Let's call it N since it is the number of data bytes.
Now Read N+1 bytes.
Now you should have the full message and can work with it.
Loop repeats for the next Write command, Read 3 bytes. Read N+1 bytes sequence.
11-14-2013 09:02 AM
I forgot to write that a program will run on cRIO device. Is it change anything?
11-14-2013 10:47 AM
I don't understand your question.
What did what you asked before have to do with a cRIO?