Problem
Recently more often use the serial port to send and transmit data, but the author at the beginning of contact with the SerialPort class, for write after the read data, because the device has not returned data, read only the null value. However, when the next write operation is performed, the last data and this data may be returned. This makes the data obtained by the author difficult to maintain accuracy.
1, solve the idea
For the above problem, the data should be returned in real time. So the idea is that after write, the thread waits (thread.sleep) to wait for the data to return. However, the final wait depends on the serial port transmission and the device return command. This waiting time is a tangled matter. I consider: "You can repeat the corresponding number of times to wait, if the wait after the data is not obtained, directly to the data." I wonder if there is any better solution in the park.
2. Solve the source code
This inserts a digression: For the serial port programming, should pay attention to the timely switch, but the author wants the real-time data to keep the data to maintain a kind of normally open state, so the author can think of is realizes the IDisposable interface to clean up.
The core encoding for writing and acquiring data is to specify an internal variable (_isworking) in the serial data acquisition class, and if _isworking is working, let the next write command wait (the waiting time must be longer than the total number of repetitions), and after fetching the data, the _ The isworking is set to false. "There is a problem here, if the high data command case, will block!! I have yet to find a good solution. "
Okay, so much for the main code.
<summary>///serial write data, and wait for return value///</summary>//<param name= "Writecommandbyte" &G t; Write command </param>//<param name= "Replydata" > Return data </param>//<returns> return data offset bit </ret urns> protected int writewithreplydata (byte[] writecommandbyte, out byte[] replydata) {if (WR Itecommandbyte = = null) {throw new ArgumentNullException ("Writecommandbyte"); }//wait for other send data to complete var waittimes = 0; while (Waittimes < 4 && _isworking) {waittimes++; Thread.Sleep (40); }//If the timeout is not awaited, return directly if (_isworking) {replydata = null; return 0; }//start working, set serial port working status not true _isworking = true; _port. Write (writecommandbyte, 0, writecommandbyte.length); Replydata = new Byte[_port. ReadBufferSize]; int count = 0; var times = 0; Wait for the data to return and limit the wait time to 100ms while (Times < 5 && Count <= 0) {times++; Count = _port. Read (replydata, 0, _port. Readbuffersize); if (count <= 0) {thread.sleep (20); }//wait to get data, set the working state to false _isworking = false; return count; }
3. Worth considering
① There is no other better solution to solve this data waiting process without data return or merge data return?
② the author of the solution, in the middle if the command set dense words back to the situation often lost data, there is no good solution?
(If you feel good, please click on the wrong words, please point out, the halogen pigeon here Thanks)
C # simply calls the serial port via SerialPort