public void Close() { if (handle_ == WinAPI.INVALID_HANDLE_VALUE) { return; } /* ポートクローズ */ if (handle_ != WinAPI.INVALID_HANDLE_VALUE) { WinAPI.CloseHandle(handle_); handle_ = WinAPI.INVALID_HANDLE_VALUE; } error_stat_ = 0; comstat_ = new WinAPI.COMSTAT(); }
public bool Setup() { if (handle_ == WinAPI.INVALID_HANDLE_VALUE) { return(false); } /* === COMポート設定読み込み === */ var dcb = new WinAPI.DCB(); WinAPI.GetCommState(handle_, out dcb); /* Baudrate */ dcb.BaudRate = BaudRate; /* Parity */ switch (Parity) { case SerialPortParity.None: dcb.Parity = WinAPI.NOPARITY; break; case SerialPortParity.Odd: dcb.Parity = WinAPI.ODDPARITY; break; case SerialPortParity.Even: dcb.Parity = WinAPI.EVENPARITY; break; case SerialPortParity.Mark: dcb.Parity = WinAPI.MASKPARITY; break; case SerialPortParity.Space: dcb.Parity = WinAPI.SPACEPARITY; break; default: dcb.Parity = WinAPI.NOPARITY; break; } /* BitSize */ dcb.ByteSize = (byte)DataBits; /* StopBits */ switch (StopBits) { case SerialPortStopBits.One: dcb.StopBits = WinAPI.ONESTOPBIT; break; case SerialPortStopBits.OnePointFive: dcb.StopBits = WinAPI.ONE5STOPBITS; break; case SerialPortStopBits.Two: dcb.StopBits = WinAPI.TWOSTOPBIT; break; default: dcb.StopBits = WinAPI.ONESTOPBIT; break; } /* Simplex Mode */ simplex_mode_ = SimplexMode; loop_data_size_ = 0; /* バイナリモード */ dcb.fBinary = 1; dcb.fOutxCtsFlow = (fOutxCtsFlow) ? (1U) : (0U); dcb.fOutxDsrFlow = (fOutxDsrFlow) ? (1U) : (0U); dcb.fDsrSensitivity = (fDsrSensitivity) ? (1U) : (0U); dcb.fTXContinueOnXoff = (fTXContinueOnXoff) ? (1U) : (0U); dcb.fOutX = (fOutX) ? (1U) : (0U); dcb.fInX = (fInX) ? (1U) : (0U); switch (fDtrControl) { case fDtrControlType.DTR_CONTROL_DISABLE: dcb.fDtrControl = WinAPI.DTR_CONTROL_DISABLE; break; case fDtrControlType.DTR_CONTROL_ENABLE: dcb.fDtrControl = WinAPI.DTR_CONTROL_ENABLE; break; case fDtrControlType.DTR_CONTROL_HANDSHAKE: dcb.fDtrControl = WinAPI.DTR_CONTROL_HANDSHAKE; break; } switch (fRtsControl) { case fRtsControlType.RTS_CONTROL_DISABLE: dcb.fRtsControl = WinAPI.RTS_CONTROL_DISABLE; break; case fRtsControlType.RTS_CONTROL_ENABLE: dcb.fRtsControl = WinAPI.RTS_CONTROL_ENABLE; break; case fRtsControlType.RTS_CONTROL_HANDSHAKE: dcb.fRtsControl = WinAPI.RTS_CONTROL_HANDSHAKE; break; case fRtsControlType.RTS_CONTROL_TOGGLE: dcb.fRtsControl = WinAPI.RTS_CONTROL_TOGGLE; break; } dcb.XonLim = XonLim; dcb.XoffLim = XoffLim; dcb.XonChar = XonChar; dcb.XoffChar = XoffChar; /* === COMポート設定更新 === */ if (!WinAPI.SetCommState(handle_, ref dcb)) { return(false); } if (!WinAPI.SetupComm(handle_, (uint)InQueue, (uint)OutQueue)) { return(false); } /* === タイムアウト設定 === */ var timeout = new WinAPI.COMMTIMEOUTS(); #if false timeout.ReadIntervalTimeout = 0; timeout.ReadTotalTimeoutMultiplier = 0; timeout.ReadTotalTimeoutConstant = 10; #else timeout.ReadIntervalTimeout = 0; timeout.ReadTotalTimeoutMultiplier = 0; timeout.ReadTotalTimeoutConstant = 5; #endif timeout.WriteTotalTimeoutConstant = 5; WinAPI.SetCommTimeouts(handle_, ref timeout); /* === イベント設定 === */ if (!WinAPI.SetCommMask( handle_, // WinAPI.EV_BREAK // | WinAPI.EV_CTS // | WinAPI.EV_DSR // | WinAPI.EV_ERR // | WinAPI.EV_RING // | WinAPI.EV_RLSD WinAPI.EV_RXCHAR // | WinAPI.EV_RXFLAG // | WinAPI.EV_TXEMPTY ) ) { return(false); } error_stat_ = 0; comstat_ = new WinAPI.COMSTAT(); return(true); }