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;
139 if (!SetCommState(__handle, &dcb))
154 struct termios config;
155 if (tcgetattr(__handle, &config))
159 config.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
160 | INLCR | IGNCR | ICRNL | IXON);
161 config.c_oflag &= ~OPOST;
162 config.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
164 config.c_cflag |= CLOCAL | CREAD;
168 config.c_cflag &= ~CRTSCTS;
170 config.c_iflag &= ~(IXON | IXOFF | IXANY);
171 switch (_flowControl) {
172 case SerialPort::FC_NONE :
174 case SerialPort::FC_RTSCTS :
176 config.c_cflag |= CRTSCTS;
179 case SerialPort::FC_XONXOFF :
180 config.c_iflag |= IXON | IXOFF;
187 if (_stopBits == SerialPort::SB_1)
188 config.c_cflag &= ~CSTOPB;
190 config.c_cflag |= CSTOPB;
194 case SerialPort::BS_8NO :
195 config.c_iflag &= ~INPCK;
196 config.c_cflag &= ~(CSIZE | PARENB);
197 config.c_cflag |= CS8;
199 case SerialPort::BS_7EVEN :
200 config.c_iflag |= INPCK;
201 config.c_cflag &= ~(CSIZE | PARODD);
202 config.c_cflag |= CS7 | PARENB;
204 case SerialPort::BS_7ODD :
205 config.c_iflag |= INPCK;
206 config.c_cflag &= ~CSIZE;
207 config.c_cflag |= CS7 | PARENB | PARODD;
213 cfsetispeed(&config, _baudRate);
214 cfsetospeed(&config, _baudRate);
216 config.c_cc[VMIN] = 1;
217 config.c_cc[VTIME] = 5;
219 if (tcsetattr(__handle, TCSANOW, &config))
226void SerialPort::purge(
int _flags)
233 if (!PurgeComm(__handle,
234 (_flags & PURGE_RX ? PURGE_RXABORT | PURGE_RXCLEAR : 0)
235 | (_flags & PURGE_TX ? PURGE_TXABORT | PURGE_TXCLEAR : 0)))
239 if (tcflush(__handle, --_flags))
248 SECURITY_ATTRIBUTES securityAttributes = {
sizeof(SECURITY_ATTRIBUTES),
NULL,
TRUE };
249 HANDLE
handle = CreateFileW(
251 GENERIC_READ | GENERIC_WRITE,
265 return __access(_path, R_OK | W_OK);
278bool SerialPort::waitCommEvent()
282 __overlapped.Internal = 0;
283 __overlapped.InternalHigh = 0;
284 __overlapped.Offset = 0;
285 __overlapped.OffsetHigh = 0;
290 if (::WaitCommEvent(
handle(), &__revents, &__overlapped)) {
291 SetEvent(__overlapped.hEvent);
295 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)