LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Sending Serial data with Handshake

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. )

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
Download All
0 Kudos
Message 1 of 13
(4,123 Views)

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!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 13
(4,117 Views)

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..

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 3 of 13
(4,082 Views)

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…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 13
(4,073 Views)
If you have a Bytes at Serial Port, just delete that and make sure that term character is enabled with the VISA Configure Serial Port. No need for events or any wait functions. It would be simplifying your code.

A write/read sequence is simply the two functions joined by the error and VISA reference. The shipping examples show this though you don't need any case statements that enable a write or a read. Put a single sequence inside a for loop and pass in a command array. You'll get an array of responses out. Or simply build one long chain of wires and reads. Or, create a state machine if the next command is dependent on a reading that you get.
0 Kudos
Message 5 of 13
(4,068 Views)

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.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 13
(4,063 Views)

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 ??

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
Download All
0 Kudos
Message 7 of 13
(4,026 Views)

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.

0 Kudos
Message 8 of 13
(4,006 Views)

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

0 Kudos
Message 9 of 13
(3,996 Views)

@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 ?

Raghunathan
LabVIEW to Automate Hydraulic Test rigs.
0 Kudos
Message 10 of 13
(3,979 Views)