public void Start(IX4Tran tran) { GWin32.PurgeComm(m_hCommPort, 0x0004 | 0x0008); WriteComm(new byte[] { 0xA5, 0x60 }); if (_thread != null) { return; } threadStarted = true; _thread = new Thread(() => { NativeOverlapped ov = new System.Threading.NativeOverlapped(); try { while (threadStarted) { var buf1 = new byte[1]; SetTimeout(0); //set always wait GWin32.SetLastError(0); GWin32.ReadFileEx(m_hCommPort, buf1, (uint)buf1.Length, ref ov, (uint err, uint len, ref NativeOverlapped ov1) => { if (err != 0) { Console.WriteLine("read got err " + err); } else { SetTimeout(); uint numRead; ov.EventHandle = GWin32.CreateEvent(IntPtr.Zero, true, false, null); var tbuf = new byte[2048]; if (!GWin32.ReadFile(m_hCommPort, tbuf, (uint)tbuf.Length, out numRead, ref ov)) { if (GWin32.GetLastError() == 997) //IO Pending { GWin32.WaitForSingleObject(ov.EventHandle, GWin32.INFINITE); } else { Console.WriteLine("read got err " + getWinErr()); } GWin32.GetOverlappedResult(m_hCommPort, ref ov, out numRead, true); } GWin32.CloseHandle(ov.EventHandle); ov.EventHandle = IntPtr.Zero; //Console.WriteLine("got data " + numRead); var buf = new byte[1 + numRead]; buf[0] = buf1[0]; if (numRead > 0) { Array.Copy(tbuf, 0, buf, 1, numRead); tran.Translate(buf); } //Console.WriteLine(BitConverter.ToString(buf)); } }); var le = GWin32.GetLastError(); if (le != 0) { _thread = null; Console.WriteLine("Read Error " + le); break; } gwait(); } } catch (InvalidOperationException iv) { _thread = null; threadStarted = false; Console.WriteLine("InvalidOperationException " + iv.Message); } Console.WriteLine("thread done"); }); _thread.Start(); }
public void OnData(byte[] buf) { //Console.Write(System.Text.ASCIIEncoding.ASCII.GetString(buf)); tran.Translate(buf); }