12-16-2015 06:30 AM
I am trying to send via Serial @ 9600 Baud a set of about 20 strings each with about 72 characters. It is terminated by a "\n" character.
The receiver is a Microcontroller unit and after it gets each String completely and saves it sends a "OK\n" back to LV.
At the end of 20 such transactions the port is closed.
But i seem to be having timing issues as each string is not transmitted in full. I am running the LV code inside of a while loop and i have tried various delays of 10ms to 200 ms with no significant improvement.
1. So instead of such a while loop with fixed delay is there any method to time these transmissions?
2. Do we need any delay between the WRITE and READ functions assuming there is no delay at the microcontroller end ?
( Attaching the ZIP file with my code in LV2012 and the garbled data received at the microcontroller end. )
12-16-2015 06:43 AM
Hi Moga,
2. Do we need any delay between the WRITE and READ functions assuming there is no delay at the microcontroller end ?
Well, transmitting 72 bytes at 9600baud takes about 72*10/9600 = 75ms - without any possible delays in serial port drivers on your PC and µC…
But good news is: you don't need any additional delay when you use the TermChar for your serial communication!
1. So instead of such a while loop with fixed delay is there any method to time these transmissions?
Simple: Send the string, then read the answer.
Send the next string after you received an answer!
12-16-2015 09:35 AM
Hello Gerd,
Thanks.
Bringing in TermChar would entail a basic change to my code ? Looks like I need to use the Event Handling functions on the VISA Advanced palette ?
( In an any case i alrteady use the "\n" character for indication end of message on both write and read)
And as to the simple write read sequence, I am sorry I quite dont get you. Could it be possible tolink to a example using this method ? I am also trying to locate some examples on similar lines..
12-16-2015 09:47 AM - edited 12-16-2015 09:48 AM
Hi Moga,
Bringing in TermChar would entail a basic change to my code ? Looks like I need to use the Event Handling functions on the VISA Advanced palette ?
No, you just enable the TermChar at SerialPortInit. Then get rid of any BytesAtPort calls. (When your answer is always "OK\n" you just request to read 99 (read "more than 3") bytes from the port.)
No event handling needed…
And as to the simple write read sequence, I am sorry I quite dont get you.
You send a message to your device and it will answer with "OK\n" - atleast you wrote so.
So what's the problem to send your string using VISAWrite followed by a VISARead to receive the "OK" answer? That's your "handshake" you need to time your loops…
12-16-2015 09:49 AM
12-16-2015 09:53 AM
MogaRaghu wrote:
Bringing in TermChar would entail a basic change to my code ? Looks like I need to use the Event Handling functions on the VISA Advanced palette ?
Very simple change. Just wire up a constant to the VISA Read for the number of bytes to read instead of the Bytes At Port. Your read message is really short, so I would use something like 10. The VISA Read will then stop reading when it finds the termination character. No need for any of the event handling functions.
12-16-2015 10:42 PM
Thanks to all those who responded. It helped get a picture ( at least partially ) as to whats happening behind the screens.
I was able to re-organize things in the VI and have achieved the required functionality with handshake.( see the captured serial monitor )
I know all of you wanted me to steer clear of "Bytes at Port" property and use a constant value instead. But what is puzzling is when i tried that, the Read was waiting for ever and timed out. I dont think i missed anything but still it does not work.
Maybe one reason could be that when the Serail port is initilaized my MCU puts out many messages about the system health - but for sure all of them terminate with CR+LF and none of them are more than even 20 Bytes long.
Or is my implementation of Term Char wrong ??
12-16-2015 11:58 PM
Are you remember to send a line feed, (or carriage return-linefeed) with your command you are writing?
Most devices that send responses with a termination character also require a termination character be sent to them to end each message.
12-17-2015 12:13 AM
Reading and writing 20 bytes each at 9600 baud takes about 42 ms. With the 200 ms Wait your code may get bogged down trying to keep up with the initialization messages. It certainly slows your data transfer by a factor of ~5.
Lynn
12-17-2015 05:12 AM
@RavensFan wrote:
Are you remember to send a line feed, (or carriage return-linefeed) with your command you are writing?
Most devices that send responses with a termination character also require a termination character be sent to them to end each message.
Yes I am sending a <CR+LF> with each message from the microcontroller. And since I have enabled the TermChar as LF in the Serial Config, I expect the VISA read to respond to that and stop reading, instead of timing out ?