Esempio n. 1
0
        private static System.String GetMessage(int errorCode)
        {
            StringBuilder sb     = new StringBuilder(512);
            int           result = Win32API_Serial.FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
                                                                 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ARGUMENT_ARRAY,
                                                                 Win32API_Serial.NULL, errorCode, 0, sb, sb.Capacity, Win32API_Serial.NULL);

            if (result != 0)
            {
                System.String s = sb.ToString();
                return(s);
            }
            else
            {
                return(GetResourceString("IO_UnknownError", errorCode));
            }
        }
Esempio n. 2
0
        public string ReadLine()
        {
            if (!isOpen)
            {
                throw new InvalidOperationException("Serial Port Read - port not open");
            }

            string inBufferString;
            bool   carriageReturnFlag = false;
            int    startTicks         = Win32API_Serial.GetTickCount();
            int    lastChar;
            int    beginReadPos = readPos;

            char [] charTestBuf = new char[1];
            charTestBuf[0] = '\r';
            int crLength = encoding.GetByteCount(charTestBuf);

            charTestBuf[0] = '\n';
            int lfLength = encoding.GetByteCount(charTestBuf);
            int timeUsed = 0;
            int timeNow;

            readLen += internalSerialStream.Read(inBuffer, readLen, InBufferBytes - (readLen - readPos));

            while (true)
            {
                timeNow   = Win32API_Serial.GetTickCount();
                lastChar  = ReadOneChar((readTimeout == InfiniteTimeout) ? InfiniteTimeout : readTimeout - timeUsed);
                timeUsed += Win32API_Serial.GetTickCount() - timeNow;

                if (lastChar == -1)
                {
                    break;
                }

                if ((char)lastChar == '\r')
                {
                    if (InBufferBytes == 0)
                    {
                        inBufferString = encoding.GetString(inBuffer, beginReadPos, readPos - beginReadPos - crLength);
                        readPos        = readLen = 0;
                        return(inBufferString);
                    }
                    else if (carriageReturnFlag == true)
                    {
                        inBufferString = encoding.GetString(inBuffer, beginReadPos, readPos - beginReadPos - 2 * crLength);
                        readPos       -= crLength;
                        return(inBufferString);
                    }
                    else
                    {
                        carriageReturnFlag = true;
                    }
                }
                else if ((char)lastChar == '\n')
                {
                    if (carriageReturnFlag == true)
                    {
                        inBufferString = encoding.GetString(inBuffer, beginReadPos, readPos - beginReadPos - crLength - lfLength);
                        if (readPos == readLen)
                        {
                            readPos = readLen = 0;
                        }
                        return(inBufferString);
                    }
                    else
                    {
                        inBufferString = encoding.GetString(inBuffer, beginReadPos, readPos - beginReadPos - lfLength);
                        if (readPos == readLen)
                        {
                            readPos = readLen = 0;
                        }
                        return(inBufferString);
                    }
                }
                else
                {
                    if (carriageReturnFlag == true)
                    {
                        charTestBuf[0] = (char)lastChar;
                        int lastCharLength = encoding.GetByteCount(charTestBuf);
                        inBufferString = encoding.GetString(inBuffer, beginReadPos, readPos - beginReadPos - crLength - lastCharLength);
                        readPos       -= lastCharLength;
                        return(inBufferString);
                    }
                }
            }

            readPos = beginReadPos;

            return((string)null);
        }
Esempio n. 3
0
        public int Read(char[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer", Resources.GetResourceString("ArgumentNull_Buffer"));
            }
            if (offset < 0)
            {
                throw new ArgumentOutOfRangeException("offset", Resources.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException("count", Resources.GetResourceString("ArgumentOutOfRange_NeedNonNegNum"));
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException(Resources.GetResourceString("Argument_InvalidOffLen"));
            }
            if (!isOpen)
            {
                throw new InvalidOperationException("Serial Port Read - port not open");
            }

            if (count == 0)
            {
                return(0);
            }

            int bytesInDriver = InBufferBytes - (readLen - readPos);

            if (readLen + bytesInDriver >= inBuffer.Length)
            {
                ResizeBuffer();
            }
            internalSerialStream.Read(inBuffer, readLen, bytesInDriver);
            readLen += bytesInDriver;

            int charsWeAlreadyHave = encoding.GetCharCount(inBuffer, readPos, readLen - readPos);

            if (charsWeAlreadyHave >= count)
            {
                return(ReadBufferIntoChars(buffer, offset, count));
            }

            if (readTimeout == 0)
            {
                return(ReadBufferIntoChars(buffer, offset, count));
            }

            int startTicks = Win32API_Serial.GetTickCount();
            int justRead;

            do
            {
                internalSerialStream.Read(inBuffer, readLen, Encoding.GetMaxByteCount(count - charsWeAlreadyHave));
                justRead = ReadBufferIntoChars(buffer, offset, count);
                if (justRead > 0)
                {
                    return(justRead);
                }
            } while (readTimeout == SerialPort.InfiniteTimeout || readTimeout - (Win32API_Serial.GetTickCount() - startTicks) > 0);

            return(0);
        }
Esempio n. 4
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]);
        }
Esempio n. 5
0
        internal static void WinIOError(int errorCode, System.String str)
        {
            switch (errorCode)
            {
            case Win32API_Serial.ERROR_FILE_NOT_FOUND:
                if (str.Length == 0)
                {
                    throw new FileNotFoundException(GetResourceString("IO.FileNotFound"));
                }
                else
                {
                    throw new FileNotFoundException(System.String.Format(GetResourceString("IO.FileNotFound_FileName"), str), str);
                }

            case Win32API_Serial.ERROR_PATH_NOT_FOUND:
                if (str.Length == 0)
                {
                    throw new DirectoryNotFoundException(GetResourceString("IO.PathNotFound_NoPathName"));
                }
                else
                {
                    throw new DirectoryNotFoundException(System.String.Format(GetResourceString("IO.PathNotFound_Path"), str));
                }

            case Win32API_Serial.ERROR_ACCESS_DENIED:
                if (str.Length == 0)
                {
                    throw new System.UnauthorizedAccessException(GetResourceString("UnauthorizedAccess_IODenied_NoPathName"));
                }
                else
                {
                    throw new System.UnauthorizedAccessException(System.String.Format(GetResourceString("UnauthorizedAccess_IODenied_Path"), str));
                }

            case Win32API_Serial.ERROR_FILENAME_EXCED_RANGE:
                throw new PathTooLongException(GetResourceString("IO.PathTooLong"));

            case Win32API_Serial.ERROR_INVALID_PARAMETER:
                throw new IOException(GetMessage(errorCode), Win32API_Serial.MakeHRFromErrorCode(errorCode));

            case Win32API_Serial.ERROR_SHARING_VIOLATION:
                if (str.Length == 0)
                {
                    throw new IOException(GetResourceString("IO.IO_SharingViolation_NoFileName"));
                }
                else
                {
                    throw new IOException(GetResourceString("IO.IO_SharingViolation_File", str));
                }

            case Win32API_Serial.ERROR_FILE_EXISTS:
                if (str.Length == 0)
                {
                    goto default;
                }
                throw new IOException(System.String.Format(GetResourceString("IO.IO_FileExists_Name"), str));

            default:
                throw new IOException(GetMessage(errorCode), Win32API_Serial.MakeHRFromErrorCode(errorCode));
            }
        }