Serial communication program written in C #

Source: Internet
Author: User
Tags visual studio 2010

If, looking over my previous blog, I will find an I/O simulation of IIC Communication program article. Well, if you can not find the click here, here is not to repeat, the system has been fully debugged passed.

Today's task is to test the data in the upper computer interface display, so the keyboard hand spent two days imitating the giant's shoulder through the interface program written in C #, the interface is simple as shown below.

Here's a step-by-step demonstration of my program for everyone.

C # is very powerful and friendly, the serial communication can be through the programming environment (Here I use Visual Studio 2010), if necessary can give me information, I have a full version of the installation package. For example, the simple invocation and selection completes the basic configuration of the serial port.

The following is mainly a programming problem, in the form of load process can complete the start of the serial port

1         private void Form1_Load (object sender, EventArgs e) 2         {3             serialport1.portname = "COM4"; 4             SerialPort1 . BaudRate = 9600; 5             Serialport1.open (); 6             this.textBox1.Clear (); 7             thread = new Thread (crossthreadflush); 8             thread. IsBackground = true; 9             Thread. Start ();         

Then it is the operation to read the data, and the event is used

1 private void serialport1_datareceived (object sender, Serialdatareceivedeventargs e)

It is possible to program in this event, but there are thread conflicts and errors when manipulating the text inside the window. A solution presented on the Internet is to use the proxy approach to the specific program as follows

  1 public partial class Form1:form 2 {3 private delegate void Flushclient ();//proxy 4 Thread THR  EAD = null;  5 UINT Datetemp;  6 UINT DATEFALG = 0;  7 UINT counter = 0;  8 UINT DATETEMP1 = 0; 9 UINT TMP1; Ten UINT RH1; one uint PRESS1; Double TMP; Double RH; Double Press; Public Form1 () (): {InitializeComponent (); OID Form1_Load (object sender, EventArgs e) at $ serialport1.portname = "COM4"; seria Lport1.baudrate = 9600; Serialport1.open (); This.textBox1.Clear (); thread = new Thread (Crossthreadflush); Thread. IsBackground = true; The thread. Start (); + + + private void serialport1_datareceived (object sender, Serialdatareceivedeventargs e)       35 {36      counter=counter+1; PNS datetemp = (UINT) this.serialPort1.ReadByte (); if (datetemp = = 0xF1) (= 1), DATETEMP1 = 0; 4 2 datetemp = 0; counter=0;                 (datetemp = = 0xF2) (+ datefalg = 2; 48 DATETEMP1 = 0; datetemp = 0; counter=0;                 * if (datetemp = = 0xf3) @ datefalg = 3; 55 DATETEMP1 = 0; datetemp = 0; counter=0;                 (Datefalg = = 1 && datetemp! = 0xF1) {if (counter==1) 61 DATETEMP1 = datetemp;                 if (counter = = 2), TMP1 = DATETEMP1 * + datetemp; 65 TMP = (0.01 * TMP1)-40;      66} 67        (Datefalg = = 2 && datetemp! = 0xF2) 70 {71 if (counter = = 1) DateTemp1 = datetemp;                     if (counter = = 2) RH1 = DATETEMP1 * + datetemp; 76 RH = (0.0405 * RH1)-4-0.0000028*rh1*rh1; (Datefalg = = 3 && datetemp! = 0xf3) 81 {8 2 if (counter = = 1) DateTemp1 = datetemp;                     if (counter = = 2), PRESS1 = DATETEMP1 * + datetemp; 87 Press = (press1-16384) *90/16384 + 30;             (+) (Crossthreadflush) () () () 93 {94             while (TRUE) 95 {96//place Sleep and infinite loops outside of the waiting async Thread.Sleep (500); 98    Threadfunction (); }100}101 102 private void Threadfunction () 103 {104 if (THIS.TEXTBO X1.                 invokerequired)//wait for asynchronous 106 {Flushclient FC = new Flushclient (threadfunction); 107 This. Invoke (FC); The Refresh method is called by proxy 108 109}110 else111 TextBox1.Text = TMP. ToString ("0.00"); if (this.textBox2.InvokeRequired)//waits for asynchronous 113 {Flushclient F c = new Flushclient (threadfunction); Invoke (FC); Call Refresh method via proxy 117}118 else119 TextBox2.Text = RH. ToString ("0.00"), if (this.textBox3.InvokeRequired)//waits for asynchronous 121 {122 Flushclient F c = new Flushclient (threadfunction); 123 this. Invoke (FC); Call Refresh method via proxy 124}126 else127 TextBox3.Text = Press.       ToString ("0.00"); 128      129}130 131 132} 

Through such a debugging before the program to run correctly, because the engineering programmer and computer Programmer's programming ideas are still different, so write a bad place please light spray.

Serial communication program written in C #

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.