Example #1
0
        protected string init(IComApp app, int baudRate)
        {
            this.comApp = app;
            if (serial != null)
            {
                return("Already Open");
            }
            restartFunc = () =>
            {
                try
                {
                    var portName = app.PortName;
                    SerialPortFixer.Execute(portName);
                    serial  = new SerialPort(portName, baudRate);
                    running = true;

                    serial.Open();
                } catch (Exception exc)
                {
                    Console.WriteLine(exc.Message);
                    Thread.Sleep(2000);
                    Restart();
                }
                var writeThread = CreateSerialWriteThread();
                writeThread.Start();


                CreateSerialReadThread(app).Start();
                return("");
            };
            return(restartFunc());
        }
Example #2
0
        protected string init(IComApp app, string portName = "COM3", int baudRate = 9600)
        {
            var pns = System.IO.Ports.SerialPort.GetPortNames();

            if (pns.Length == 0)
            {
                return("No ports found");
            }
            started   = true;
            this._app = app;
            _comm.SetErrorListener(this);
            try
            {
                _comm.Open(portName, baudRate);
                _comm.Start(app);
            }
            catch (Exception exc)
            {
                return(exc.Message);
            }
            return("");
        }
Example #3
0
        private Thread CreateSerialReadThread(IComApp app)
        {
            var thread = new Thread(() =>
            {
                byte[] buf = new byte[2048];
                try
                {
                    while (running)
                    {
                        var readLen = serial.BaseStream.Read(buf, 0, buf.Length);
                        if (readLen <= 0)
                        {
                            Console.WriteLine("end of stream");
                            break;
                        }
                        var data = new byte[readLen];
                        Array.Copy(buf, data, readLen);
                        app.OnData(data);
                    }
                }
                catch (Exception exc)
                {
                    if (running)
                    {
                        Console.WriteLine(exc.Message);
                        Restart();
                    }
                    else
                    {
                        Console.WriteLine("serial read thread done");
                    }
                }
            });

            thread.Name = "SerialReadThread";
            return(thread);
        }
Example #4
0
        public void Start(IComApp app)
        {
            GWin32.PurgeComm(m_hCommPort, 0x0004 | 0x0008);
            app.OnStart(this);
            if (_thread != null)
            {
                return;
            }
            threadStarted = true;
            new Thread(() =>
            {
                bool inWrite        = false;
                uint writeErr       = 0;
                uint writeOk        = 0;
                NativeOverlapped ov = new System.Threading.NativeOverlapped();
                while (threadStarted)
                {
                    if (m_hCommPort == null || m_hCommPort.IsInvalid)
                    {
                        break;
                    }
                    SerWriteInfo wi = null;
                    lock (_writeQueueLock)
                    {
                        if (_writeQueue.Count == 0)
                        {
                            Monitor.Wait(_writeQueueLock);
                        }
                        while (inWrite)
                        {
                            Thread.Sleep(100);
                            Console.WriteLine("in write wait");
                        }
                        wi = _writeQueue[0];
                        _writeQueue.RemoveAt(0);
                    }
                    //if (wi.Done != null) try { wi.Done(0, "no buf"); } catch { };
                    //continue;
                    if (wi == null || wi.buf == null || wi.buf.Length == 0)
                    {
                        if (wi.Done != null)
                        {
                            try { wi.Done(0, "no buf"); } catch { }
                        }
                        ;
                        continue;
                    }
                    inWrite = true;
                    ResetOverlapped(ov);
                    var writeRes = GWin32.WriteFileEx(m_hCommPort, wi.buf, (uint)wi.buf.Length, ref ov, (uint err, uint b, ref NativeOverlapped c) =>
                    {
                        writeErr = err;
                        writeOk  = b;
                        inWrite  = false;
                    });
                    if (!writeRes)
                    {
                        //Console.WriteLine("failed write comm " + getWinErr());
                        if (wi.Done != null)
                        {
                            try { wi.Done(255, "failed write comm " + getWinErr()); } catch { }
                        }
                        ;
                        inWrite = false;
                    }
                    // IOCompletion routine is only called once this thread is in an alertable wait state.
                    gwait(); //only with thread
                    if (writeRes)
                    {
                        if (wi.Done != null)
                        {
                            try { wi.Done(writeErr, writeErr == 0 ? writeOk.ToString() : "WriteError"); } catch { }
                        }
                    }
                }
                Console.WriteLine("Com Write Queue done");
            }).Start();
            _thread = new Thread(() =>
            {
                var tbuf             = new byte[2048];
                var buf1             = new byte[1];
                NativeOverlapped ovo = new System.Threading.NativeOverlapped();
                try
                {
                    while (threadStarted)
                    {
                        SetTimeout(0); //set always wait
                        GWin32.SetLastError(0);
                        ResetOverlapped(ovo);
                        GWin32.ReadFileEx(m_hCommPort, buf1, (uint)buf1.Length, ref ovo, (uint err, uint len, ref NativeOverlapped ovoo) =>
                        {
                            if (err != 0)
                            {
                                Console.WriteLine("read got err " + err);
                            }
                            else
                            {
                                SetTimeout();
                                uint numRead;
                                NativeOverlapped ov = new System.Threading.NativeOverlapped();
                                ov.EventHandle      = GWin32.CreateEvent(IntPtr.Zero, true, false, null);

                                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);
                                    app.OnData(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;
                    if (onErr != null)
                    {
                        onErr.OnError("InvalidOperationException " + iv.Message, true);
                        return;
                    }
                    Console.WriteLine("InvalidOperationException " + iv.Message);
                }
                Close();
                onErr.OnError("thread done", true);
                Console.WriteLine("thread done");
            });
            _thread.Start();
        }
Example #5
0
 public SimpleDriver(IComApp app)
 {
     comm.Init(app);
 }
Example #6
0
 public void Init(IComApp app)
 {
     base.init(app, 9600);
 }
Example #7
0
 public void Init(IComApp app)
 {
     app.PortName = "COM3";
     base.init(app, 9600);
 }