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.

Example Code

.NET Serial Port Write and Read Using LabVIEW

Products and Environment

This section reflects the products and operating system used to create the example.

To download NI software, including the products shown below, visit ni.com/downloads.

    Software

  • LabVIEW

Code and Documents

Attachment

Overview
This example shows how to use the .NET Serial Port Class with a remake of a classic VI, the Serial Basic Write and Read. 


Description
This is useful if you don't have the VISA driver installed on the machine.  Note that this class is found in version 2.0 or higher of the .NET Framework.

 
Requirements

  • LabVIEW 2012 (or compatible)


Steps to Implement or Execute Code

 

1.Select a port of serial device, configure serial setting.

2.Run the VI, and write to the string

 

Additional Information or References
VI Block Diagram

1.png 

 

 

 

 

 

 **This document has been updated to meet the current required format for the NI Code Exchange.**

 

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
JPowell
Member
Member
on

Thanks much for this.  This gave me what I needed to talk to an old spectrometer that needed some wierd delay settings for the serial port that normal VISA couldn't do.

Jeff'sGPIB
Member
Member
on

Hi,  I see this is an old thread.  I have a new comment.  First, thanks!  This is a good example of .NET.  Most appreciate.  What I am trying to do is to discover what com ports are available.  i.e. get a list of com's. programmically.  I see in this example, the com name is a string and it is given.  How would I discover what com's are available?

Thanks JeffsGPIB.

JPowell
Member
Member
on

Hi Jeff,

First, go to the connectivity palette and there to the .NET palette.  Select Constructor.  Once the constructor is placed, a dialog will pop up so you can select what creator to use.  In the Objects section of the dialog box, select System, then System.io.ports then click on Serial Port.  In the Constructor section, select the SerialPort() object and press OK.  Now, right click on the constructor and select create, then the option that says Method for System... .  Then, select GetPortNames().  An invoke method node will be created.  In my test VI, I just connected the output of the invoke node to an indicator.  You will be able to delete the constructor at that point.  See the attached image for an example and the results on the front panel.  You can also get the example at https://decibel.ni.com/content/docs/DOC-43774 along with a version of the example from this page, in which I used the example code.  I created that page, since I couldn't attach the example here.

John

GetSerialPorts.jpg