11#if __DCL_HAVE_THIS_FILE__
20#ifndef INVALID_HANDLE_VALUE
21#define INVALID_HANDLE_VALUE -1
24SerialPort::~SerialPort()
27 if (__overlapped.hEvent !=
NULL)
28 CloseHandle(__overlapped.hEvent);
32SerialPort::SerialPort(
const String& _path,
int _oflags,
33 size_t _inQueue,
size_t _outQueue
38 ZeroMemory(&__overlapped,
sizeof(__overlapped));
40 open(_path, _oflags, _inQueue, _outQueue);
43void SerialPort::open(
const String& _path,
int _oflags,
44 size_t _inQueue,
size_t _outQueue
52 if (!SetupComm(__handle, (DWORD)_inQueue, (DWORD)_outQueue))
55 if (_oflags & File::NONBLOCK) {
63 if (!GetCommTimeouts(__handle, &cto))
66 cto.ReadIntervalTimeout = MAXDWORD;
67 cto.ReadTotalTimeoutConstant = 0;
68 cto.ReadTotalTimeoutMultiplier = 0;
69 if (!SetCommTimeouts(__handle, &cto))
78 DWORD dwSaveError = GetLastError();
89void SerialPort::setAttributes(
90 SerialPort::BaudRate _baudRate,
91 SerialPort::ByteSize _byteSize,
92 SerialPort::StopBits _stopBits,
93 SerialPort::FlowControl _flowControl
98 ZeroMemory(&dcb,
sizeof(dcb));
99 dcb.DCBlength =
sizeof(dcb);
100 if (!GetCommState(__handle, &dcb))
103 dcb.BaudRate = _baudRate;
105 case SerialPort::BS_8NO:
107 dcb.Parity = NOPARITY;
109 case SerialPort::BS_7EVEN:
111 dcb.Parity = EVENPARITY;
113 case SerialPort::BS_7ODD:
115 dcb.Parity = ODDPARITY;
118 dcb.fParity = dcb.Parity != NOPARITY;
122 dcb.StopBits = ONESTOPBIT;
125 dcb.StopBits = TWOSTOPBITS;
129 switch (_flowControl) {
131 dcb.fOutxCtsFlow =
TRUE;
132 dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
135 dcb.fOutX = dcb.fInX =
TRUE;
141 if (!SetCommState(__handle, &dcb))
155 struct termios config;
156 if (tcgetattr(__handle, &config))
160 config.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
161 | INLCR | IGNCR | ICRNL | IXON);
162 config.c_oflag &= ~OPOST;
163 config.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
165 config.c_cflag |= CLOCAL | CREAD;
169 config.c_cflag &= ~CRTSCTS;
171 config.c_iflag &= ~(IXON | IXOFF | IXANY);
172 switch (_flowControl) {
173 case SerialPort::FC_NONE :
175 case SerialPort::FC_RTSCTS :
177 config.c_cflag |= CRTSCTS;
180 case SerialPort::FC_XONXOFF :
181 config.c_iflag |= IXON | IXOFF;
188 if (_stopBits == SerialPort::SB_1)
189 config.c_cflag &= ~CSTOPB;
191 config.c_cflag |= CSTOPB;
195 case SerialPort::BS_8NO :
196 config.c_iflag &= ~INPCK;
197 config.c_cflag &= ~(CSIZE | PARENB);
198 config.c_cflag |= CS8;
200 case SerialPort::BS_7EVEN :
201 config.c_iflag |= INPCK;
202 config.c_cflag &= ~(CSIZE | PARODD);
203 config.c_cflag |= CS7 | PARENB;
205 case SerialPort::BS_7ODD :
206 config.c_iflag |= INPCK;
207 config.c_cflag &= ~CSIZE;
208 config.c_cflag |= CS7 | PARENB | PARODD;
214 cfsetispeed(&config, _baudRate);
215 cfsetospeed(&config, _baudRate);
217 config.c_cc[VMIN] = 1;
218 config.c_cc[VTIME] = 5;
220 if (tcsetattr(__handle, TCSANOW, &config))
227void SerialPort::purge(
int _flags)
234 if (!PurgeComm(__handle,
235 (_flags & PURGE_RX ? PURGE_RXABORT | PURGE_RXCLEAR : 0)
236 | (_flags & PURGE_TX ? PURGE_TXABORT | PURGE_TXCLEAR : 0)))
240 if (tcflush(__handle, --_flags))
249 SECURITY_ATTRIBUTES securityAttributes = {
sizeof(SECURITY_ATTRIBUTES),
NULL,
TRUE };
250 HANDLE
handle = CreateFileW(
252 GENERIC_READ | GENERIC_WRITE,
266 return __access(_path, R_OK | W_OK);
279bool SerialPort::waitCommEvent()
283 __overlapped.Internal = 0;
284 __overlapped.InternalHigh = 0;
285 __overlapped.Offset = 0;
286 __overlapped.OffsetHigh = 0;
291 if (::WaitCommEvent(
handle(), &__revents, &__overlapped)) {
292 SetEvent(__overlapped.hEvent);
296 if (GetLastError() == ERROR_IO_PENDING)
DCLCAPI int __access(const String &_path, int _type)
#define INVALID_HANDLE_VALUE
#define __DCL_TRACE1(fmt, arg1)
#define __DCL_ASSERT_PARAM(expr)
#define __DCL_ASSERT(expr)
#define IMPLEMENT_CLASSINFO(class_name, base_class_name)
#define __DCL_ASSERT_HANDLE(expr)
HandleType handle() const
void open(const String &_path, int _oflags=READONLY, int _mode=0666) __DCL_THROWS1(IOException *)
virtual bool onEvent(short _revents, PollThread *_pPollThread) __DCL_THROWS1(IOException *)
static bool access(const String &_path)