public void SetAttributes (int baud_rate, Parity parity, int data_bits, StopBits bits, Handshake hs) { DCB dcb = new DCB (); if (!GetCommState (handle, dcb)) ReportIOError (null); dcb.SetValues (baud_rate, parity, data_bits, bits, hs); if (!SetCommState (handle, dcb)) ReportIOError (null); }
static bool SetCommState (int handle, DCB dcb) { throw new System.NotImplementedException(); }
static extern bool SetCommState (int handle, DCB dcb);
/// <summary> /// Configures the serial device based on the connection parameters pased in by the user. /// </summary> /// <returns>Whether or not the operation succeeded</returns> private bool ConfigureSerialPort() { DCB serialConfig = new DCB(); if (GetCommState(handle, ref serialConfig)) { // setup the DCB struct with the serial settings we need serialConfig.BaudRate = (uint)this.baudRate; 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 Parity.Even: serialConfig.Parity = 2; break; case Parity.Mark: serialConfig.Parity = 3; break; case Parity.Odd: serialConfig.Parity = 1; break; case Parity.Space: serialConfig.Parity = 4; break; case Parity.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(handle, ref serialConfig)) { // set the serial connection timeouts COMMTIMEOUTS timeouts = new COMMTIMEOUTS(); //timeouts.ReadIntervalTimeout = 1; //timeouts.ReadTotalTimeoutMultiplier = 0; //timeouts.ReadTotalTimeoutConstant = 0; //timeouts.WriteTotalTimeoutMultiplier = 0; //timeouts.WriteTotalTimeoutConstant = 0; timeouts.ReadIntervalTimeout = 20; timeouts.ReadTotalTimeoutMultiplier = 10; timeouts.ReadTotalTimeoutConstant = 100; timeouts.WriteTotalTimeoutMultiplier = 10; timeouts.WriteTotalTimeoutConstant = 100; if (SetCommTimeouts(handle, ref timeouts)) return true; else return false; } else return false; } else return false; }
static extern bool GetCommState(IntPtr hFile, ref DCB lpDCB);
static extern bool SetCommState(int handle, DCB dcb);
static extern bool GetCommState(int handle, [Out] DCB dcb);
static bool SetCommState(int handle, DCB dcb) { throw new System.NotImplementedException(); }