8.3 Serial port read and write

8.3 Serial port read and write

What is a serial port? The serial port is a computer interface used for data communication, such as a computer. Older PCs and laptops have serial ports. Industrial PCs are generally equipped with serial ports. If not, you can buy a USB-serial converter on Taobao.

What can the serial port do? The serial port is used for data communication. For example, the value of a temperature sensor connected to the computer through the serial port can be read by the computer.

Why talk about serial port reading and writing? Sometimes the purchased equipment only provides the serial port and communication protocol. If you want to save money, you can program yourself to read the serial port data.

Use javascript to read the serial port, you need to use the third-party serialport package, use the npm command to install, for example, the following command:

npm install serialport

If it is slow, please try the following. For the installation and configuration of the cnpm command, please refer to section 2.5 :

cnpm install serialport

Steps to read the serial port:

  • Check the physical hardware connection;
  • To open the serial port (open in the program, non-physical operation);
  • Send commands to the lower computer to send data to the computer serial port;
  • Wait for the data to be uploaded by the lower computer. If it is uploaded to the serial port, it will be read out.

For example, first get the serial port package. The require in node is similar to the include in C language, the using in C#, and the import in java/python. Talk is cheap, show me the code:

var SerialPort = require('serialport')

Next is the key. Hold your breath and open the serial port:

ar serialPort = new SerialPort('COM1', {//Serial port number to view in the computer device manager, the baud rate depends on the lower computer setting baudRate: 9600, autoOpen:false})

Suppose the command to read the device is as follows:

const cmd = Buffer.from([0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B]);

Don't underestimate this series of data commands. There are many doorways and communication protocols are involved. Only a specific command will be answered by the lower-level computer. This measure can ignore signal interference and facilitate the inspection of the lower-level computer. In addition, the last two bytes of the command are the CRC check of the first 6 bits of data. If the CRC check of the following bit machine fails, the command is ignored.

How to send commands to the serial port? In fact, it is to write the serial port operation, the code is as follows:

serialPort.write(cmd, function (error, result){});

Then how do you know that the serial port has data and can be read? We need to monitor the serial port to witness the miracle, and the data is read:

serialPort.on('data',function (data) {console.log('data received:',data);//data is the data you want to read}

Did I just read it once and then Game Over? No, no, you can read it every 1s, and learn about it with setInterval.

Reading a bunch of data at a time, should I use a notepad to keep recording the data manually? You can store it in the database and even display it in real time.

Zhou Xingxing’s friend Leonardo’s "solar flashlight" will light up when there is light, but cannot light up without light. Is it because my computer doesn't have serial port hardware, so I can't do serial port programming? No, Eltima Virtual Serial Port Driver (45-day free trial is provided). Understand, create a virtual serial port, and you can program and operate the serial port happily.

The virtual serial port is solved, what should I do if there is no lower computer? Download a free serial debugging assistant. It is very useful. Combined with the VSPD virtual serial port, you can simulate your computer/laptop connected to a lower computer. Many sites on the Internet provide downloads.

For more professional explanations, please refer to: https://blog.csdn.net/u012612399/article/details/80421730

Reference: https://cloud.tencent.com/developer/article/1418785 8.3 Serial Read and Write-Cloud + Community-Tencent Cloud