Essence of Serial Communication

Source: Internet
Author: User

Today's serial communication is mainly used in industrial control and other fields. Although it is not difficult, many new people (including some veterans) still cannot implement serial communication. Next, let's talk about serial communication, if you have more friends, please don't hesitate to give me some advice to make this article a serial communicationArticleA great number of new users can master serial communication correctly.

 

First, you must know the characteristics of serial communication: the speed of sending and receiving data through the serial port is slow, so that sending and receiving a command data may require multiple operations to send or receive a buffer.

 

Below I will mainly collect some articles about serial communication on the network orCodeAnd then comments and put forward your own views.

Post1:Use the SerialPort class in C # To implement serial communication (updated in succession)

Http://blog.csdn.net/cy757/article/details/4474930 this article is well written and has great reference value.

Com. baudrate = 115200;

Com. portname = "COM1 ";

I would like to comment on these two codes. I hope that my friends who are working on the UI can provide a friendly user interface to help control the validity of the incoming data. 1> the serial port baud rate seems to only support a certain number of grades, such as 4800 | 9600 | 19200, etc. Therefore, you want to make the choice control ComboBox in the UI instead of the textbox of the user input number. 2> "COM1" is also selected, but there are still two other points A> you can consider SerialPort. getportnames enumerates the serial ports that exist on the local machine. B> if a non-existent serial port is opened or an opened serial port causes an exception, try to catch the exception.ProgramCrash.

 

Remember, there are two ways to receive data: 1> event reception; 2> thread receiving; B> timer receiving; 3> anywhere, when you directly call the serial port read method to read data, make sure that the read will never return, leading to freezing.

The above private void readport () thread function has two points worth learning:

1> catch (timeoutexception) {}; // No bytes can be read exception

2> thread. Sleep (waittime ). // Make the time slice

 

Remember, cross-thread access control in C # is very dangerous. We should stop it. Use functions such as invoke for Synchronous access. Example: Private void serialport1_datareceived function.

 

Post2:C # serial operation series

5 articles in the http://blog.csdn.net/wuyazhe/article/details/5598945 serial port is worth a look.

 

Post3:Real-time Monitoring System for serial communication between PC and AI Regulator Based on C #

Http://www.autooo.net/classid119-id57828.html written in this article is better, application scenarios, functions, solutions are great, The only pity that the ultimate implementation and encoding failed to receive and parse the multi-thread serial data part of the source code contribution, but unfortunately !!

 

Post4:With VC ++Implement multi-thread applications in Serial Communication Programs

Http://www.ccw.com.cn/applic/prog/htm2003/20031211_13ZAR.htm

 

Post5:Multi-thread Serial Communication Technology inGPSApplications in Navigation(VC)

Http://www.dzsc.com/data/html/2009-10-20/79533.html

Cserialport class

 

Post6:Multi-serial port multi-thread industrial control implementation (cserialport class)

Http://bbs.51cto.com/thread-417831-1.html this article is very good to write, it is worth reading.

The following two lines of code tell us the timeout setting when multiple serial port Devices round-robin.

If (tnow-tlast) * 1000 + (nnowmillsecond-nmillsecond [0])> 800) 9 x * G8 L4 n! R6 S0

Pview-> setcommval (); // send the data command or perform other related processing on the next device.

 

 

Post7:C #MediumSerialPortConnected10Temperature Sensors,It only receives one event.,If a sensor fails,How does one not affect the normal reception of other sensors?

Http://topic.csdn.net/u/20110518/21/9b0aec92-6c3f-43db-af02-8013db733210.html this article asked the question is very good, followed by netizens jsmouse paste code some places worth learning.

Analyze the jsmouse code first:

Crcdata. calculatecrc16 (crctemp, out crch, out CrCl );

If (buffer [10]! = Crch & buffer [11]! = CrCl)

{

Buffer. removeat (0 );

Continue; // continue the next cycle

}

The method of cyclic validation is worth using for reference.

 

Post8:C #Serial PortSerialPortDetails

Baud Rate

Readtimeout

Receivedbytesthreshold

Sp. dtrenable = true; // if it is RS232 to RS485, this sentence must be required, otherwise it cannot communicate

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.