Ejemplo n.º 1
0
 public override void Open()
 {
     NativeStream = NativeDevice.Open();
     Task.Run(() =>
     {
         while (IsOpen)
         {
             try
             {
                 int temp = NativeStream.ReadByte();
                 if (temp != -1)
                 {
                     ReceivedData.Enqueue((byte)temp);
                     DataReceived?.Invoke(this, new System.EventArgs());
                 }
             }
             catch
             {
             }
         }
     });
 }
Ejemplo n.º 2
0
        private int ReadOneChar(int timeout)
        {
            int beginReadPos = readPos;
            int nextByte;
            int timeUsed = 0;

            Debug.Assert(isOpen, "ReadOneChar - port not open");

            if (timeout < 0 && timeout != SerialPort.InfiniteTimeout)
            {
                return(-1);
            }

            if (encoding.GetCharCount(inBuffer, readPos, readLen - readPos) != 0)
            {
                do
                {
                    nextByte = (int)inBuffer[readPos++];
                } while (encoding.GetCharCount(inBuffer, beginReadPos, readPos - beginReadPos) < 1);
                encoding.GetChars(inBuffer, beginReadPos, readPos - beginReadPos, oneChar, 0);
                return(oneChar[0]);
            }
            else
            {
                if (timeout == 0)
                {
                    if (InBufferBytes + readPos >= inBuffer.Length)
                    {
                        ResizeBuffer();
                    }
                    int bytesRead = internalSerialStream.Read(inBuffer, readLen, InBufferBytes - (readLen - readPos));                     // read all immediately avail.
                    readLen += bytesRead;

                    if (ReadBufferIntoChars(oneChar, 0, 1) == 0)
                    {
                        return(-1);
                    }
                    else
                    {
                        return(oneChar[0]);
                    }
                }

                int startTicks = Win32API_Serial.GetTickCount();
                do
                {
                    timeUsed = Win32API_Serial.GetTickCount() - startTicks;
                    if (timeout != SerialPort.InfiniteTimeout && (timeout - timeUsed <= 0))
                    {
                        nextByte = -1;
                        break;
                    }
                    nextByte = internalSerialStream.ReadByte((timeout == InfiniteTimeout) ? InfiniteTimeout : timeout - timeUsed);
                    if (nextByte == -1)
                    {
                        break;
                    }
                    if (readLen >= inBuffer.Length)
                    {
                        ResizeBuffer();
                    }
                    inBuffer[readLen++] = (byte)nextByte;
                    readPos++;
                } while (encoding.GetCharCount(inBuffer, beginReadPos, readPos - beginReadPos) < 1);
            }

            if (nextByte == -1)
            {
                return(-1);
            }

            encoding.GetChars(inBuffer, beginReadPos, readPos - beginReadPos, oneChar, 0);
            return(oneChar[0]);
        }