Share this

PLC serial port free protocol communication routine

2026-04-06 05:16:01 · · #1

Free protocol communication, also known as protocol-free communication, requires writing a temporary protocol based on the communication data format of the other device. Although the application of free protocol communication is decreasing with the popularization of standard protocols (Modbus, USS, etc.), it is still necessary for some small devices such as barcode scanners and LCD displays, which do not integrate standard communication protocols. The following example demonstrates free protocol communication between the RS232 and RS485 hardware ports of the MC532 PLC and a computer serial port debugging assistant (used to simulate serial port devices).

MC532 PLC Setup and Programming

1. Add communication function library

Double-click the Library Manager with the left mouse button—left-click Add Library, search for Serial Communication, and click OK to complete the addition.

2. Create the variables shown in the figure.

3. Call the function block shown in the figure.

4. Write the program shown in the figure: ① Call COM.Open, this function block opens the serial port; the variable usiListLength is the data length of the variable pParameterList (as will be explained later, the data length is 7); the variable pParameterList is a pointer variable, the pointer address points to the array aParamsB115200 of the structure COM.PARAMETER, the array contains udiPort, udiValue (baud rate), udiBaudrate, udiValue (port number), udiParity, udiValue (parity), udiStopBits, udiValue (stop bits), udiTimeout, udiValue (timeout time). The code snippet provides seven serial port parameters: udiByteSize, udiValue (data bits), and udiBinary, udiValue (binary parameter, enabling binary mode). The creation of these variables is based on the examples in the following library files, followed by parameter settings. The variable hCom is a CAA.HANDLE structure used to obtain the communication handle. Other parameters include: ① udiPort, udiValue: baud rate (set to 115200 in this example); ② udiBaudrate, udiValue: port number (COM2 for the 2PLC's RS232 port, COM0 for the first RS485 port, and COM1 for the second RS485 port; values ​​can be assigned based on the hardware port used); ③ udiParity, udiValue: parity type (odd, even, or no parity); ④ udiStopBits, udiValue: stop bits (ONESTOPBIT/ONE5STOPBIT/TWOSTOPBIT). In this example, the timeout is set to ONESTOPBIT; udiTimeout, udiValue: timeout duration, set to 500ms in this example; udiByteSize, udiValue: data bits, set to 8 in this example; udiBinary, udiValue: binary parameter, the default value of 1 can be used; after setting, since pParameterList is a pointer type, ADR(aParamsB115200) can be filled in;

③ Call COM.Write, this function block sends data via the serial port; the variable udiTimeout is the timeout period, set to 500ms in this example; the variable hCom is a CAA.HANDLE data type structure, representing the communication handle, which can be obtained from the output variable hCom in Com.Open; pBuffer is a pointer type, pointing to the address of the serial port data buffer, and in this example, the write variable is set to BYTE type byWriteData, so ADR(byWriteData) is filled in; szSize is the length of the data to be written, set to 10 BYTE data length in this example, so 10 is filled in; ④ Call COM.Read, this function block reads data via the serial port; the variable udiTimeout is the timeout period, set to 500ms in this example; the variable hCom is a CAA.HANDLE data type structure, representing the communication handle, which can be obtained from Com.Open. The output variable hCom is obtained from the code; pBuffer is a pointer type, and the data points to the address of the serial port read data buffer. In this example, the written value variable is set to BYTE type byReadData, so ADR(byReadData) is filled in this example; szBuffer is the size of the data read from the serial port buffer. In this example, it is set to 10 BYTE data length, so 10 is filled in; ⑤ Call COM.Close, this function block closes the serial port; the variable hCom is a CAA.HANDLE structure, which is a communication handle, and can be obtained from the output variable hCom in Com.Open.

Open the serial port assistant and set the relevant parameters.

1. Open Computer Management and check the computer's serial port number (this example uses a USB to serial adapter, the port number is COM4).

2. Open the serial port assistant software (you can search and purchase it online; the usage is basically similar) and set the relevant parameters.

[Port] COM4 (computer port number); [Baud Rate] 115200; [Data Bits] 8; [Parity] E even; [Stop Bits] 1; Left-click to select [Input HEX] and [Display HEX], setting the data type for writing and display to hexadecimal; Left-click to [Open Serial Port].

PLC communicates with computer for data transmission and retrieval.

1. PLC sends data, and computer reads the data.

After triggering COM.Open to open the serial port, and assigning the decimal number 65 (0x41 in hexadecimal) to byWriteData, COM.Write is triggered, and it can be seen that the computer can read the value sent by the PLC.

2. Data is sent from the computer and read from the PLC.

Sending the hexadecimal value 0x86 via a serial port assistant triggers COM.Open to open the serial port. Closing the serial data write function block then triggers COM.Read, successfully reading the hexadecimal value 0x86 (decimal 134).

-END-

Read next

CATDOLL Nanako Soft Silicone Head

You can choose the skin tone, eye color, and wig, or upgrade to implanted hair. Soft silicone heads come with a functio...

Articles 2026-02-22