C# Remote control protocol 1 – Serial communication

December 23, 2020

画像に alt 属性が指定されていません。ファイル名: クリップボード01-30.jpg

This website summarizes how to write communication programs necessary for measuring instrument control for those who have a general understanding of C # introduction.

In addition to this article, there are the following articles.

This article introduces serial communication between C # and external devices.

Check communication port

In serial communication, the OS automatically assigns a port number for the connection between Windows and an external device. So every time windows starts, the port number may change. Normally, it seems to keep using the assigned port number once, but if communication becomes impossible, check that the port number has not changed.

The assigned port number can be checked in Windows Device Manager.

画像に alt 属性が指定されていません。ファイル名: クリップボード01-27.jpg
Device manager COM1 is assigned to serial communication

Serial communication

Drag and drop SerialPort in Toolbox> Component on the left of the Visual C # designer screen into the form. Place labels and buttons.

画像に alt 属性が指定されていません。ファイル名: クリップボード01-29.jpg

After serialPort1 is located, select serialPort1 and make the necessary settings from the properties at the bottom right of the screen. Here, check that BaudRate and Parity are correct values.

Here, make a program to perform serial communication when button1 is clicked.

First, set the port numbers used for serial communication as follows.

Next, open the serial port and prepare the communication between the PC and the external device.

After opening the port, read and write data.

Use .Write () to send a command to an external device. * IDN? Is a command to inquire about the ID of the device. Since this depends on the connected device, follow the device manual. Commands for each device will be introduced in another article.

Data sent from the external device is read by .ReadExisiting () and stored in readData.

After communication is completed, close the port.

The above is the flow of a series of communication. If there is a communication error in the middle, the program will end abnormally. Therefore, enclose it in a try statement so that it does not end abnormally.

If an error occurs in the middle of the try statement, catch processing is performed. In the example, if an error occurs, the error is notified by a message box.

In the actual process, the external device performs the process by receiving the command of .Write () and returns the result by .ReadExisting (), so a little time is required between these two processes. In that case, it works well if you insert Sleep () after the .Write () instruction.

In the example, it waits for 100 ms, but this also depends on the external device, so you need to change the number and try several times.

Running the program

Execute when you can write a serial communication program. For external devices with multiple interfaces, the communication port must be set to a serial port in advance.

If communication is not successful, check that the values ​​of BaudRate and Parity in the properties of serialPort1 are correct.


This article introduced how to communicate with external devices using serial communication. Communication settings may vary depending on the type of external device, so I would like to introduce the programming method of a specific device on another occasion.


Posted by naozane