Winform serial communication platform, winform Serial Communication
Next to the last card reading, you need to make a weighbridge read.
The following figure shows how to read the lbs.
The weighbridge always sends data to the serial port, so it is necessary to intercept the data to determine whether the data is legal and then calculate the result.
I encountered a small problem. I will introduce it at the end of this article.
I am a newbie. You are welcome to comment.
1 using System; 2 using System. collections. generic; 3 using System. componentModel; 4 using System. data; 5 using System. drawing; 6 using System. linq; 7 using System. text; 8 using System. windows. forms; 9 using System. IO. ports; 10 using System. IO; 11 using System. threading; 12 13 namespace WindowsFormsApplicationcs 14 {15 public partial class FormMain: Form 16 {17 // declare port object 18 SerialPort myport = nu Ll; 19 // data array for data processing 20 byte [] buf = new byte [50]; 21 // declare the delegate type 22 public delegate void Displaydelegate (byte [] buf ); 23 // delegate variable 24 public Displaydelegate disp_delegate; 25 26 public FormMain () 27 {28 InitializeComponent (); 29 // security thread update space 30 // Form. checkforillegalcrossthreadcils = false; 31} 32 33 // window loading 34 private void FormMain_Load (object sender, EventArgs e) 35 {36 Form. checkForIllegalCrossThrea Dcils = false; 37 txtPort. text = "COM3"; 38 myport = new SerialPort (); 39 disp_delegate = new Displaydelegate (DispUI); 40} 41 42 // open the serial port button 43 private void button#click (object sender, eventArgs e) 44 {45 try 46 {47 48 // set the serial port 49 myport. portName = txtPort. text. toString (); 50 // sets the bit rate of 51 myport. baudRate = Convert. toInt32 (cmbBaud. text); 52 // set the data bit 53 myport. dataBits = Convert. toInt32 (cmbBits. text); 54 // set the stop bit 55 switch (cmbStopBits. selectedIndex) 56 {57 case 0: myport. stopBits = StopBits. none; break; 58 case 1: myport. stopBits = StopBits. one; break; 59 case 2: myport. stopBits = StopBits. onePointFive; break; 60 case 3: myport. stopBits = StopBits. two; break; 61} 62 63 // sets the 64 switch (cmbParity. selectedIndex) 65 {66 case 0: myport. parity = Parity. even; break; 67 case 1: myport. parity = Parity. mark; break; 68 case 2: myport. parity = Parity. none; break; 69 case 3: myport. parity = Parity. odd; break; 70 case 4: myport. parity = Parity. space; break; 71} 72 73 // the buffer zone accepts only one character 74 myport. receivedBytesThreshold = 1; 75 // Add a delegate to receive events 76 myport. dataReceived + = new SerialDataReceivedEventHandler (this. myport_DataReceived); 77 // open the serial port 78 myport. open (); 79 if (myport. isOpen) 80 {81 MessageBox. show ("Port enabled"); 82 this. tstldqzt. Text = "Current status: Port enabled"; 83} 84 else 85 {86 MessageBox. Show ("port cannot be enabled! "); 87} 88} 89 catch (Exception ex) 90 {91 MessageBox. Show (" An error occurred while opening the port! \ N "+ ex. message. toString (); 92} 93} 94 95 // delegate Method 96 // receive data 97 private void myport_DataReceived (object sender, SerialDataReceivedEventArgs e) 98 {99 Thread. sleep (100); 100 if (myport. bytesToRead> 0) // If data in the buffer zone is 101 {102 myport. read (buf, 0, 1); // Read data from the buffer to the buf temporary storage array, 103} 104 105 if (buf [0] = 0x02) // If the start is equal to 2, it indicates that the information starts 0x02106 {107 try108 {109 while (myport. bytesToRead = 9); 110 myport. read (buf, 0, 9 );// Read a correct data from the buffer to the buf temporary storage array 111 string s = string. empty; 112 113 for (int I = 0; I <9; I ++) 114 {115 s + = (char) buf [I]; 116} 117 118 StringBuilder B = new StringBuilder (""); 119 120 if (s. substring (0, 1) = "-") 121 B. append ("-"); 122 123 int strint = Convert. toInt32 (s. substring (1, 7); 124 int dianint = Convert. toInt32 (s. substring (125); 126 127 switch (dianint) 128 {129 case 1: strint = strint/10; break; ca Se 2: strint = strint/100; break; 130 case 3: strint = strint/1000; break; 131 case 4: strint = strint/10000; break; 132} 133 B. append (strint); 134 txtReceive. text + = "start" + B + "End \ r \ n"; 135 136 // this. invoke (disp_delegate, buf); 137} 138 finally139 {140 myport. discardInBuffer (); // clear the buffer 141} 142} 143} 144 145 146 // update the ui method public void DispUI (byte [] buf) 147 {148 149} 150 151 152 // close method 153 private voi D button2_Click (object sender, EventArgs e) 154 {155 myport. Close (); 156 if (! Myport. isOpen) 157 {158 this. tstldqzt. text = "Current status: port closed"; 159 MessageBox. show ("port closed"); 160} 161} 162 163 // clear screen button 164 private void button3_Click (object sender, EventArgs e) 165 {166 this.txt Receive. clear (); 167} 168 169 // menu bar about 170 private void tsmiabout_Click (object sender, EventArgs e) 171 {172 frmAbout frmabout = new frmAbout (); 173 frmabout. showDialog (); 174} 175 176 // Save the result in the menu bar 177 private void tsmisave_Click (object sender, EventArgs e) 178 {179 if (this.txt Receive. text = "") 180 {MessageBox. show ("the current result is empty, please test first");} 181 string path = Directory. getCurrentDirectory () + "\ Client port test result .txt"; 182 File. writeAllText (path, this.txt Receive. text. toString (), Encoding. ASCII); 183} 184 185} 186}
There is actually a problem here, that is, a window usually involves communication between multiple serial devices,
The current requirements of the company are no exception. We can see that I used a thread. sleep,
Although the problem was solved at the time, when a window has multiple serial communication interfaces to update the ui, the problem may occur,
However, because there were no devices at hand and no further tests were conducted ,.
Without this sentence, the ui cannot be updated in real time. At that time, I recorded a lot of data at a short point over an hour,
I have determined that it is not the problem of data processing. I think the reason should be that while blocks the thread and is still performing operations.
But isn't invoke a new thread delegate? Why cannot I update the ui.
Baidu's findings are explained as follows:
Invoke is the specified delegate on the thread that owns the basic window handle of this control.
Begininvoke is used to asynchronously execute the specified delegate on the thread that creates the basic window handle of this control.
You can use begininvoke to update the ui without interfering with other serial communication.