From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

Having issues with simple serial communication.

Solved!
Go to solution

 I followed a tutorial online to start learning basic serial communication. The tutorial involved shorting pins 1 and 3 on the Comm 1 port, and writing a simple program (attached). I edited the program a little, to show the varied results that I get when trying to write and read a simple phrase via serial port. 

 

Could someone explain what causes the kind of result shown in "Serial Comm Example.png"? I believe it has to do with timing. Why would only parts of the phrase be transmitted?

 

Thanks,

 

Sarah

Download All
0 Kudos
Message 1 of 6
(5,588 Views)
Solution
Accepted by sarahzig

@sarahzig wrote:

The tutorial involved shorting pins 1 and 3 on the Comm 1 port


Shouldn't that be pins 2 and 3?  On a standard DB-9 serial port, pin 2 is the Recieve and pin 3 is the Transmit.

 

Now for your code...

1. Move the Configure Serial Port to be before the loop.  You only need to configure the port once.

2. Close the port after the loop.

3. Handle your error after the close.

4. Stop your loop on an error or the stop button was pressed.  You will need an OR in there.

5. No need for the Flush Buffer function.  Remove it.

6. Using the Bytes At Port is a dangerous thing.  It introduces all kinds of interesting race conditions.  Instead, tell the VISA Read to read the number of bytes you expect to read.

6a. Alternatively, be sure to send an End Of Line character at the end of your sent data and set the number of bytes to read to something a lot more than you would ever expect to read.  The VISA Read will stop reading when it a) sees the number of bytes it was told to read, b) sees the termination character (currently enabled and set to a Line Feed, which is part of the End Of Line), or c) the timeout happens.  Whichever happens first.

7. With 6, the wait is no longer needed.


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
Message 2 of 6
(5,566 Views)

Thanks for the help. I followed all your steps and am getting consistent writing + reading.

 

Sarah 

0 Kudos
Message 3 of 6
(5,555 Views)

How should I go about communicating with the pressure calibrator?I have two documents from the manufacturer. Would I write the commands into the "Write buffer" control on my VI?

 

Download All
0 Kudos
Message 4 of 6
(5,545 Views)
Basically, yes. You send the commands properly terminated with a carriage return/line feed. If you right click on a string control or constant, you can enable '\' Codes Display and use \r\n. You can also use the constants on the string palette. I notice that there is a command to disable the check sum. I would recommend that you do this to make things simpler.
0 Kudos
Message 5 of 6
(5,531 Views)

Hi crossrulz,

 

I wanted to thank you for your comment on item 6a, I've been working on a program that contains a serial read component and had this issue of using Bytes At Port causing a race condition and I couldn't figure it out how to correct for until I read this.

 

I will add a Kudos!

 

Thanks.

 

Mike

 

 

Certified LabVIEW Architect
Miami University
Instrumentation Laboratory

CLA Logo
0 Kudos
Message 6 of 6
(5,032 Views)