Exemplo n.º 1
0
    public void Open(string portName, uint baudrate)
    {
        // Check if port can be found
        bool isValid =
            SerialPort.GetPortNames()
            .Any(x => String.Compare(x, portName, StringComparison.OrdinalIgnoreCase) == 0);

        if (!isValid)
        {
            throw new IOException(string.Format("{0} port was not found", portName));
        }
        string port = @"\\.\" + portName;

        Handle = CreateFile(port, GenericRead | GenericWrite, 0, IntPtr.Zero, OpenExisting, 0, IntPtr.Zero);
        if (Handle.IsInvalid)
        {
            throw new IOException(string.Format("{0} port is already open", portName));
        }
        var dcb = new Dcb();

        // first get the current dcb structure setup
        if (GetCommState(Handle, ref dcb) == false)
        {
            throw new IOException(string.Format("GetCommState error {0}", portName));
        }
        dcb.BaudRate = baudrate;
        dcb.ByteSize = 8;
        dcb.Flags    = 129;
        dcb.XoffChar = 0;
        dcb.XonChar  = 0;
        /* Apply the settings */
        if (SetCommState(Handle, ref dcb) == false)
        {
            throw new IOException(string.Format("SetCommState error {0}", portName));
        }
        /* Set DTR, some boards needs a DTR = 1 level */
        if (EscapeCommFunction(Handle, Setdtr) == false)
        {
            throw new IOException(string.Format("EscapeCommFunction error {0}", portName));
        }
        // Write default timeouts
        var cto = new Commtimeouts
        {
            ReadTotalTimeoutConstant    = 500,
            ReadTotalTimeoutMultiplier  = 0,
            ReadIntervalTimeout         = 10,
            WriteTotalTimeoutConstant   = WriteTimeout,
            WriteTotalTimeoutMultiplier = 0
        };

        if (SetCommTimeouts(Handle, ref cto) == false)
        {
            throw new IOException(string.Format("SetCommTimeouts error {0}", portName));
        }
        // Create filestream
        _fileStream = new FileStream(Handle, FileAccess.ReadWrite, 32, false);
    }
Exemplo n.º 2
0
        private bool ConfigureSerialPort()
        {
            var serialConfig = new DCB();
            if (GetCommState(pHandle, ref serialConfig))
            {
                // setup the DCB struct with the serial settings we need
                serialConfig.BaudRate = (uint)this.iBaudRate;
                serialConfig.ByteSize = this.byteSize;
                serialConfig.fBinary = 1; // must be true
                serialConfig.fDtrControl = 1; // DTR_CONTROL_ENABLE "Enables the DTR line when the device is opened and leaves it on."
                serialConfig.fAbortOnError = 0; // false
                serialConfig.fTXContinueOnXoff = 0; // false

                serialConfig.fParity = 1; // true so that the Parity member is looked at
                switch (this.parity)
                {
                    case SerialPortParity.Even:
                        serialConfig.Parity = 2;
                        break;
                    case SerialPortParity.Mark:
                        serialConfig.Parity = 3;
                        break;
                    case SerialPortParity.Odd:
                        serialConfig.Parity = 1;
                        break;
                    case SerialPortParity.Space:
                        serialConfig.Parity = 4;
                        break;
                    case SerialPortParity.None:
                    default:
                        serialConfig.Parity = 0;
                        break;
                }

                switch (this.stopBits)
                {
                    case StopBits.One:
                        serialConfig.StopBits = 0;
                        break;
                    case StopBits.OnePointFive:
                        serialConfig.StopBits = 1;
                        break;
                    case StopBits.Two:
                        serialConfig.StopBits = 2;
                        break;
                    case StopBits.None:
                    default:
                        throw new ArgumentException("stopBits cannot be StopBits.None");
                }

                if (SetCommState(pHandle, ref serialConfig))
                {
                    // set the serial connection timeouts
                    var timeouts = new Commtimeouts
                                       {
                                           ReadIntervalTimeout = 30,
                                           ReadTotalTimeoutMultiplier = 0,
                                           ReadTotalTimeoutConstant = 0,
                                           WriteTotalTimeoutMultiplier = 0,
                                           WriteTotalTimeoutConstant = 0
                                       };
                    return SetCommTimeouts(this.pHandle, ref timeouts);
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
Exemplo n.º 3
0
 private static extern bool SetCommTimeouts(
     SafeFileHandle hFile,           // handle to comm device
     ref Commtimeouts lpCommTimeouts // time-out values
     );
Exemplo n.º 4
0
 protected static extern bool SetCommTimeouts(IntPtr hFile, ref Commtimeouts lpCommTimeouts);