C # compiled serial communication program,

Source: Internet
Author: User

C # compiled serial communication program,

If you read my previous blog, you will find an article on simulating IIC communication using I/O. Well, if you cannot find it, click here. I will not go into details here, and the system has been fully debugged.

Today's task is to display the test data on the host computer interface, so the keyboard operator spent two days imitating the giant's shoulder and passed the interface program written in C, the interface is as simple as shown below.

Next we will show you my program step by step.

C # very powerful and friendly. You can use the programming environment for Serial Communication (Visual Studio 2010 is used here). If necessary, you can give me information. I have a full installation package. For example, the basic configurations of the serial port are completed by simple call and selection.

The following is a programming problem. You can start the serial port in the form Load process.

 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();10         }

The next step is to read data and use the event

1 private void serialPort1_DataReceived (object sender, SerialDataReceivedEventArgs e)

You can program in this event, but when you operate on the text in the form, you will find a thread conflict and error. One solution provided on the Internet is to use a proxy. The specific procedure is as follows:

1 public partial class Form1: Form 2 {3 private delegate void FlushClient (); // proxy 4 Thread thread = null; 5 uint DateTemp; 6 uint Datefalg = 0; 7 uint counter = 0; 8 uint DateTemp1 = 0; 9 uint TMP1; 10 uint RH1; 11 uint PRESS1; 12 double TMP; 13 double RH; 14 double PRESS; 15 16 public Form1 () 17 {18 InitializeComponent (); 19} 20 21 22 private void Form1_Load (object sender, EventArgs e) 23 {24 SerialPort1.PortName = "COM4"; 25 serialPort1.BaudRate = 9600; 26 serialPort1.Open (); 27 this. textBox1.Clear (); 28 thread = new Thread (CrossThreadFlush); 29 thread. isBackground = true; 30 thread. start (); 31} 32 33 34 private void serialport=datareceived (object sender, SerialDataReceivedEventArgs e) 35 {36 counter = counter + 1; 37 DateTemp = (uint) this. serialPort1.ReadByte (); 38 if (DateTemp = 0x F1) 39 {40 Datefalg = 1; 41 DateTemp1 = 0; 42 DateTemp = 0; 43 counter = 0; 44} 45 if (DateTemp = 0xF2) 46 {47 Datefalg = 2; 48 DateTemp1 = 0; 49 DateTemp = 0; 50 counter = 0; 51} 52 if (DateTemp = 0xF3) 53 {54 Datefalg = 3; 55 DateTemp1 = 0; 56 DateTemp = 0; 57 counter = 0; 58} 59 if (Datefalg = 1 & DateTemp! = 0xF1) 60 {if (counter = 1) 61 DateTemp1 = DateTemp; 62 if (counter = 2) 63 {64 TMP1 = DateTemp1 * 256 + DateTemp; 65 TMP = (0.01 * TMP1)-40; 66} 67 68} 69 if (Datefalg = 2 & DateTemp! = 0xF2) 70 {71 if (counter = 1) 72 DateTemp1 = DateTemp; 73 if (counter = 2) 74 {75 RH1 = DateTemp1 * 256 + DateTemp; 76 RH = (0.0405 * RH1)-4-0.0000028 * RH1 * RH1; 77} 78 79} 80 if (Datefalg = 3 & DateTemp! = 0xF3) 81 {82 if (counter = 1) 83 DateTemp1 = DateTemp; 84 if (counter = 2) 85 {86 PRESS1 = DateTemp1 * 256 + DateTemp; 87 PRESS = (PRESS1-16384) * 90/16384 + 30; 88} 89} 90} 91 92 private void CrossThreadFlush () 93 {94 while (true) 95 {96 // place sleep and infinite loop outside waiting for Asynchronization 97 Thread. sleep (500); 98 ThreadFunction (); 99} 100} 101 102 private void ThreadFunction () 103 {104 if (this. textBox1.InvokeRequired) // wait for asynchronous 105 {106 FlushClient fc = new FlushClient (ThreadFunction); 107 this. invoke (fc); // call the refresh method by proxy 108 109} 110 else111 textBox1.Text = TMP. toString ("0.00"); 112 if (this. textBox2.InvokeRequired) // wait for asynchronous 113 {114 FlushClient fc = new FlushClient (ThreadFunction); 115 this. invoke (fc); // call the refresh method by proxy 116 117} 118 else119 textBox2.Text = RH. toString ("0.00"); 120 if (this. textBox3.InvokeRequired) // wait for asynchronous 121 {122 FlushClient fc = new FlushClient (ThreadFunction); 123 this. invoke (fc); // call the refresh method by proxy 124 125} 126 else127 textBox3.Text = PRESS. toString ("0.00"); 128 129} 130 131 132}

After such debugging, the program can be correctly run. Due to the difference in programming ideas between engineering programmers and computer programmers, it is easy to write.

Important notifications]

In order to enrich the spare time lives of programmers and hardware dogs, we made a difficult decision. In our "bubble fish Studio", we opened the public subscription number "hardware is King: king_hardware. Focus on smart hardware and Internet entrepreneurship, as well as daily learning materials/funny jokes. Scan the QR code below!

Powered By Bubble_fish; Email: bubble_fish@yeah.net


In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

In C language-> what?

-> Is a whole. It is used to point to a struct, class in C ++, and other pointers containing sub-data to obtain sub-data. In other words, if we define a struct in C and declare a pointer pointing to this struct, we need to use "->" to retrieve the data in the struct using the pointer ".
For example:
Struct Data
{
Int a, B, c;
};/* Define struct */
Struct Data * p;/* define struct pointer */
Struct Data A = {1, 2, 3};/* declare variable */
Int x;/* declare a variable x */
P = & A;/* point p to */
X = p-> a;/* indicates that the data item a in the struct pointed to by p is assigned to x */
/* Because p points to A, p-> a = A. a, that is, 1 */

For the first problem, p = p-> next; this should appear in the linked list of C language. next here should be a struct pointer of the same type as p, and its definition format should be:
Struct Data
{
Int;
Struct Data * next;
};/* Define struct */
............
Main ()
{
Struct Data * p;/* declare the pointer Variable p */
......
P = p-> next;/* assign the value in next to p */
}
The linked list pointer is a difficulty in C language, but it is also the key. It is very useful to learn it. To be careful, you must first talk about variables and pointers.
What is a variable? The so-called variables should not be simply thought that the amount will become a variable. Let's use the question of our Dean: "Is the classroom changing ?" Change, because there are different people in the classroom every day, but they do not change, because the classroom is always there, and it does not become larger or smaller. This is the variable: There is a constant address and a variable storage space. Under normal circumstances, we only see the variable in the room, that is, its content, but do not pay attention to the variable address, but the C language pointer is the address of the room. We declare that variables are equivalent to building a house to store things. We can directly watch things in the house, while declaring pointers is equivalent to getting a positioner. When a pointer points to a variable, it is to use the pointer to locate the variable. Then we can use the pointer to find the variable "tracked" and get the content in it.
What about struct? The structure is equivalent to a villa composed of several houses, and several houses are bound for use together. Suppose there are many such villas distributed in a big maze, and each villa has a house. The location information of another villa is put in it. Now you have found the first villa with the positioner and obtained what you want from it (the data part of the linked list ), then, calculate the location of the next villa into your positioner (p = p-> next), and go down to the next villa ...... If you go on like this, you will know that the information of a villa on the ground is gone (p-> next = NULL), and your trip is over. This is the process of traversing a linked list. Now you can understand the meaning of p = p-> next!
Write so much. I hope you can understand.
If you want to learn c and C ++ well, you must be familiar with linked lists and pointers!

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.