08-11-2009 07:00 PM
I am trying to communication with a few modules using RS232 protocol. Almost every single module is sending data to the PC in packets. The problem is that on my first VISA Read, I don't always get data from the beginning. For example, a temperature module (http://www.medlab.eu/english/downloads/tempoem11.pdf) sends data in the following format (31 bytes long):
„1“,<bl>,<bl>,<bl>,T1H,T1T,T1O,<bl>,<bl>, „2“,<bl>,<bl>,<bl>,T2H,T2T,T2O,<ER2>,<bl>,<bl>,
„3“,<bl>,<bl>,<bl>,T3H,T3T,T3O,<ER3>,<bl>,<bl>,<0x0d>,<0x0A>
Everytime I call a VISA Read command, I want to be able to read all 31 bytes from the beginning. It seems like I have to do additional synchronizing steps such as checking each byte to make sure I see a certain pattern (eg: first byte is 1 and previous 2 bytes are 0d0A). This takes a bit of time, especially when I am trying to start getting data from all the different modules at the exact same time. Is there a better way to accomplish all this?
Solved! Go to Solution.
08-11-2009 09:25 PM
You should post your VI to see if something is not right in it.
If you know your message is 31 bytes long, and always ends in the new line character (ASCII hex 0A), then you should request 31 bytes to read and enable the termination character with the new line character. If you get a message that is less than 31 bytes long, then you can discard it and retry the read.
Also, make sure you are not constantly opening and closing the serial port, otherwise you will risk losing data.
08-12-2009 08:25 AM - edited 08-12-2009 08:26 AM
The module sends out a new packet every two seconds, the total time to transmit the message is 32.292 ms, 9600 bits/second * 31 bytes * 10 bits/byte (8 data bits + 1 start bit + 1 stop bit) which gives you a lot of "idle" time on the serial line.
I would configure the serial ports between a minimum delay time of 35ms and a max delay time of 1800ms, but probably closer to the lower end. Take a look at the screen shot to get you started. The serial buffer is flushed, then the program loops until a byte is trasmitted by the module. If 31 bytes are transmitted within the delay time you should have a valid message, if not the read was started in the middle of the message and the entire process should be repeated.
Ryan
Certified LabVIEW Developer
08-12-2009 08:11 PM
Ryan_K, wouldn't flushing the buffer before the read erase all data at the port before the read?
So then there would be 0 bytes at the port.
Or am i misunderstanding what that function does?
08-12-2009 09:39 PM - edited 08-12-2009 09:40 PM
No reason to flush.
I would program it like this.
08-12-2009 09:53 PM
08-12-2009 10:32 PM
Cory K wrote:
Code snippets? .... show off![]()
I've got to learn to use them some way.
(I'm looking forward to seeing it incorporated in the code capture tool. Without CCT, it takes a couple extra steps like a file save to get the picture into the post.)
08-13-2009 09:49 AM
ryan_k, I tried your method but always gave me 0 bytes. Perhaps flushing and checking for only >1 byte was a problem.
Raven, you method worked great! I used the exact same logic except I had to use 13 (0xd) as a termination character becase the 31-byte string I read always had 0xd at the beginning and 0xA at the end (unlike what the manual says).
Thanks everyone!
08-13-2009 10:02 AM
08-13-2009 10:08 AM
Yes.
I think what your code does is it simply waits for any number of bytes (>1) to be available and then it reads 31 bytes. The problem is that those 31 bytes are not necessarily from start to end. Flushing the buffer will only discard all the available bytes in the buffer.