예제 #1
0
 public void WriteComm(W32Serial.SerWriteInfo wi)
 {
     if (wi == null)
     {
         return;
     }
     lock (_writeQueueLock)
     {
         _writeQueue.Add(wi);
         Monitor.Pulse(_writeQueueLock);
     }
 }
예제 #2
0
        private Thread CreateSerialWriteThread()
        {
            var thread = new Thread(() =>
            {
                try
                {
                    var inWrite = false;
                    while (running)
                    {
                        if (serial == null || !serial.IsOpen)
                        {
                            break;
                        }
                        W32Serial.SerWriteInfo wi = null;
                        lock (_writeQueueLock)
                        {
                            if (!running)
                            {
                                break;
                            }
                            if (_writeQueue.Count == 0)
                            {
                                Monitor.Wait(_writeQueueLock);
                            }
                            PreProcessQueue(_writeQueue);
                            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;
                        Action <uint, string> notifyDone = (code, msg) =>
                        {
                            if (wi.Done != null)
                            {
                                Task.Run(() => { try { wi.Done(code, msg); } catch { }; });
                            }
                        };
                        if (wi == null || wi.buf == null || wi.buf.Length == 0)
                        {
                            notifyDone(255, "no buf");
                            continue;
                        }
                        inWrite = true;

                        serial.Write(wi.buf, 0, wi.buf.Length);
                        try
                        {
                            notifyDone(0, comApp.waitSerialResponse());
                        }
                        catch { }
                        inWrite = false;
                    }
                }
                catch (Exception exc)
                {
                    if (running)
                    {
                        Console.WriteLine(exc.Message);
                    }
                    else
                    {
                        Console.WriteLine("serial write thread done");
                    }
                }
                Console.WriteLine("serial write thread end!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            });

            thread.Name = "SerialWriteThread";
            return(thread);
        }