C # Serial Communication-send data to the serial port and receive the returned data synchronously,

Source: Internet
Author: User

C # Serial Communication-send data to the serial port and receive the returned data synchronously,

Recently, I wrote a C # serial communication program. The system is based on B/S architecture. The SerialPort class has a DataReceived event, which is used to receive data returned by the serial port. However, this method is useful in the C/S architecture, but B/S is not easy to handle. Therefore, I wrote a method to receive returned data in synchronization mode without using the DataReceived event. After testing, it can be used normally (multithreading is not supported ).

I. Machine class

1. The Machine class has a static variable, which is defined as follows:

private static SerialPort serialPort = null;

2. How to send data to the serial port and synchronously receive the returned data:

/// <Summary> /// send data to the serial port, read the returned data /// </summary> /// <param name = "sendData"> sent data </param> /// <returns> returned data </returns> private byte [] ReadPort (byte [] sendData) {if (serialPort = null) {serialPort = new SerialPort ("COM1", 9600, Parity. none, 8, StopBits. one); serialPort. readBufferSize = 1024; serialPort. writeBufferSize = 1024;} if (! SerialPort. isOpen) {serialPort. open ();} // send data serialPort. write (sendData, 0, sendData. length); // read the returned data while (serialPort. bytesToRead = 0) {Thread. sleep (1);} Thread. sleep (50); // The data is received within 50 milliseconds. You can adjust byte [] recData = new byte [serialPort. bytesToRead]; serialPort. read (recData, 0, recData. length); return recData ;}View Code

3. Send online commands:

/// <Summary> /// online /// </summary> /// <returns> online success or failure </returns> public bool Connect (out string msg) {byte [] sendData = new byte [] {0x01, 0x01, 0x00, 0x00}; CommonUtil. calCheckCode (sendData); byte [] recData = ReadPort (sendData); if (recData. length> = 4 & recData [0] = 0x01 & recData [1] = 0x02 & recData [2] = 0x00 & CommonUtil. validCheckCode (recData) {switch (recData [3]) {case 0x00: msg = "the control board is restarting"; return false; case 0x01: msg = "online success"; return true; case 0x02: msg = "the control board is being maintained"; return false; case 0x03: msg = "the data format received by the control board is incorrect"; return false; default: msg = "unknown state"; return false ;}} else {msg = "the data format returned by the cargo machine is incorrect"; return false ;}}View Code

Ii. how to use it. It is tested in the C/S program and can be used normally in the B/S program. The following is the code in the form Form1 class:

1. define the object and initialize it.

Private Machine machine = null; public Form1 () {InitializeComponent (); machine = new Machine ();}View Code

2. Online events

// Private void btnConnect_Click (object sender, EventArgs e) {string msg = null; if (machine. connect (out msg) {MessageBox. show ("success:" + msg);} else {MessageBox. show ("failed:" + msg );}}View Code

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.