26
26
class RawSerialDevice
28
string error_message_;
28
string error_message_;
36
: error_message_("no error."), hCom_(INVALID_HANDLE_VALUE),
42
const char* what(void)
44
return error_message_.c_str();
48
bool connect(const char* device, long baudrate)
50
/* COM �|�[�g���J�� */
51
enum { NameLength = 11 };
52
char adjusted_device[NameLength];
53
snprintf(adjusted_device, NameLength, "\\\\.\\%s", device);
54
hCom_ = CreateFileA(adjusted_device, GENERIC_READ | GENERIC_WRITE, 0,
55
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
57
if (hCom_ == INVALID_HANDLE_VALUE) {
58
error_message_ = string("open failed: ") + device;
64
SetupComm(hCom_, 4096 * 8, 4096);
67
setTimeout(current_timeout_);
70
bool ret = setBaudrate(baudrate);
72
error_message_ = "fail SerialDevice::setBaudrate()";
79
void setTimeout(int timeout)
81
COMMTIMEOUTS timeouts;
82
GetCommTimeouts(hCom_, &timeouts);
84
timeouts.ReadIntervalTimeout = (timeout == 0) ? MAXDWORD : 0;
85
timeouts.ReadTotalTimeoutConstant = timeout;
86
timeouts.ReadTotalTimeoutMultiplier = 0;
88
SetCommTimeouts(hCom_, &timeouts);
95
if (hCom_ != INVALID_HANDLE_VALUE) {
97
hCom_ = INVALID_HANDLE_VALUE;
102
bool isConnected(void)
104
return (hCom_ == INVALID_HANDLE_VALUE) ? false : true;
108
bool setBaudrate(long baudrate)
114
baudrate_value = CBR_4800;
118
baudrate_value = CBR_9600;
122
baudrate_value = CBR_19200;
126
baudrate_value = CBR_38400;
130
baudrate_value = CBR_57600;
134
baudrate_value = CBR_115200;
138
baudrate_value = baudrate;
142
GetCommState(hCom_, &dcb);
143
dcb.BaudRate = baudrate_value;
145
dcb.Parity = NOPARITY;
147
dcb.StopBits = ONESTOPBIT;
148
if (SetCommState(hCom_, &dcb) == 0) {
157
int send(const char* data, size_t count)
164
WriteFile(hCom_, data, (DWORD)count, &n, NULL);
169
int receive(char data[], int count, int timeout)
175
if (timeout != current_timeout_) {
177
current_timeout_ = timeout;
181
ReadFile(hCom_, data, count, &n, NULL);
190
PURGE_RXABORT | PURGE_TXABORT | PURGE_RXCLEAR | PURGE_TXCLEAR);
36
: error_message_("no error."), hCom_(INVALID_HANDLE_VALUE),
42
const char* what(void)
44
return error_message_.c_str();
48
bool connect(const char* device, long baudrate)
50
/* COM �|�[�g���J�� */
51
enum { NameLength = 11 };
52
char adjusted_device[NameLength];
53
snprintf(adjusted_device, NameLength, "\\\\.\\%s", device);
54
hCom_ = CreateFileA(adjusted_device, GENERIC_READ | GENERIC_WRITE, 0,
55
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
57
if (hCom_ == INVALID_HANDLE_VALUE) {
58
error_message_ = string("open failed: ") + device;
64
SetupComm(hCom_, 4096 * 8, 4096);
67
setTimeout(current_timeout_);
70
bool ret = setBaudrate(baudrate);
72
error_message_ = "fail SerialDevice::setBaudrate()";
79
void setTimeout(int timeout)
81
COMMTIMEOUTS timeouts;
82
GetCommTimeouts(hCom_, &timeouts);
84
timeouts.ReadIntervalTimeout = (timeout == 0) ? MAXDWORD : 0;
85
timeouts.ReadTotalTimeoutConstant = timeout;
86
timeouts.ReadTotalTimeoutMultiplier = 0;
88
SetCommTimeouts(hCom_, &timeouts);
95
if (hCom_ != INVALID_HANDLE_VALUE) {
97
hCom_ = INVALID_HANDLE_VALUE;
102
bool isConnected(void)
104
return (hCom_ == INVALID_HANDLE_VALUE) ? false : true;
108
bool setBaudrate(long baudrate)
114
baudrate_value = CBR_4800;
118
baudrate_value = CBR_9600;
122
baudrate_value = CBR_19200;
126
baudrate_value = CBR_38400;
130
baudrate_value = CBR_57600;
134
baudrate_value = CBR_115200;
138
baudrate_value = baudrate;
142
GetCommState(hCom_, &dcb);
143
dcb.BaudRate = baudrate_value;
145
dcb.Parity = NOPARITY;
147
dcb.StopBits = ONESTOPBIT;
148
if (SetCommState(hCom_, &dcb) == 0) {
157
int send(const char* data, size_t count)
164
WriteFile(hCom_, data, (DWORD)count, &n, NULL);
169
int receive(char data[], int count, int timeout)
175
if (timeout != current_timeout_) {
177
current_timeout_ = timeout;
181
ReadFile(hCom_, data, count, &n, NULL);
190
PURGE_RXABORT | PURGE_TXABORT |
191
PURGE_RXCLEAR | PURGE_TXCLEAR);