C # to solve the PC serial port to receive data loss

Projects need to achieve data transmission lower computer and the host computer, read a lot of online processing method There are two main host computer. Method One: port_DataReceived (data reception method SerialPort control, when data comes triggers) creates a thread, so when the serial port while waiting for data, does not affect the operation of the main form or main thread. Second method: using a caching mechanism (reference: http: //blog.csdn.net/jiqiang_paul/article/details/6914619). First, by defining a member variable List ReceBuf new List (4096); used to store all data in the receive function, the continuously received by RecBuf.AddRange () method was added to ReceBuf the data, and in the same time Recebuf inspection data, if it reaches a certain length and correct check result (check method consistent sender and receiver), and then processed.

After testing found that the two methods of data packets sent in less than 200ms, the problem of data loss can occur interval, and now these two methods are combined, while the second method will improve the mechanism for double-buffered been tested and found to solve data packet loss problem.
A, defines serial data reception event () in the public MainForm
  public MainForm()
        {
            InitializeComponent();
            serialPort_port.DataReceived + = new SerialDataReceivedEventHandler (port_DataReceived); // serial port to receive event
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; // close the thread checks boast
        }
Second, the definition delegates receive the event under MainForm class and write reception program
 
  public delegate void PortDelegate();
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            PortDelegate proc_PORTGETDATA = new PortDelegate(getComData);
            IAsyncResult async_PORTGETDATA = proc_PORTGETDATA.BeginInvoke(null, null);
        }
private List ReceBuffer = new List (4096); // serial port to receive first-level cache, the default allocate no more than a memory and always 
 private byte [] DataTemp = new byte [10]; // copy from a secondary cache to cache 
 private List DataBuf = new List (1000); // serial port to receive the secondary cache, default assignment and never more than 1000
 private byte [] DataTemp2 = new byte [10]; // copy out from the secondary cache analysis
 private byte[] DataAnalyBuf = new byte[8]; 
 public void getComData()
 { 
 try
 { 
 int n = serialPort_port.BytesToRead; 
 byte[] Receive = new byte[n]; 
 serialPort_port.Read(Receive, 0, Receive.Length); 
 bool DataCatched = false; // Flag data may be analyzed
 // cache data 
 ReceBuffer.AddRange(Receive); 
 // integrity judgment 
 while(ReceBuffer.Count >= 10)
 { 
 See //// frame header and trailer data, the accuracy of determination 
// this can be written as CRC check on the data
if (ReceBuffer[0] == 0x50 && ReceBuffer[9] == 0x42) 
 { 
 ReceBuffer.CopyTo (0, DataTemp, 0, 10); 
 DataBuf.AddRange(DataTemp); 
DataCatched = true; 
 ReceBuffer.RemoveRange (0, 10); // get a correct data, delete the cache
 }
 else
 { 
 ReceBuffer.RemoveAt (0); // not a header, delete data
 }
 }
 //analyze data 
 if (DataCatched == true) 
 { 
 while (DataBuf.Count > 0)
 {
 DataBuf.CopyTo(0,DataTemp2,0,10); 
Here write your own data analysis
DataBuf.RemoveRange(0, 10);
 } 
 }
 } catch { } 
 }

Guess you like

Origin www.cnblogs.com/cmblogs/p/11129313.html