C# Communication with external device 1 – Serial communication

2020年9月9日

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.

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.

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.

 serialPort1.PortName = "COM1";

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

serialPort1.Open();

After opening the port, read and write data.

serialPort1.Write("*IDN?");
string readData = serialPort1.ReadExisting();

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.

serialPort1.Close();

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.

serialPort1.PortName = "COM1";

try
{
    serialPort1.Open();
    serialPort1.Write("*IDN?");
    string readData = serialPort1.ReadExisting();
    serialPort1.Close();
}
catch
{
    MessageBox.Show("A communication error has occurred");
}

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.

try
{
    serialPort1.Open();
    serialPort1.Write("*IDN?");
    System.Threading.Thread.Sleep(100);
    string readData = serialPort1.ReadExisting();
    serialPort1.Close();
}
catch
{
    MessageBox.Show("A communication error has occurred");
}

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.

Conclusion

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.