3
\brief �V���A���ʐM�̎����� (Linux, Mac)
5
\author Satofumi KAMIMURA
7
$Id: SerialDevice_lin.cpp 1811 2010-04-30 16:12:05Z satofumi $
23
string error_message_;
25
struct termios sio_; //!< �ʐM�^�[�~�i������
26
fd_set rfds_; //!< �^�C���A�E�g����
29
bool waitReceive(int timeout)
36
tv.tv_sec = timeout / 1000;
37
tv.tv_usec = (timeout % 1000) * 1000;
39
if (select(fd_ + 1, &rfds_, NULL, NULL,
40
(timeout < 0) ? NULL : &tv) <= 0) {
41
/* �^�C���A�E�g���� */
49
RawSerialDevice(void) : error_message_("no error."), fd_(InvalidFd)
54
const char* what(void)
56
return error_message_.c_str();
60
bool connect(const char* device, long baudrate)
63
enum { O_EXLOCK = 0x0 }; // Linux �ł͎g���Ȃ��̂Ń_�~�[���쐬���Ă���
65
fd_ = open(device, O_RDWR | O_EXLOCK | O_NONBLOCK | O_NOCTTY);
68
error_message_ = string(device) + ": " + strerror(errno);
71
int flags = fcntl(fd_, F_GETFL, 0);
72
fcntl(fd_, F_SETFL, flags & ~O_NONBLOCK);
75
tcgetattr(fd_, &sio_);
78
sio_.c_cflag &= ~(CSIZE | PARENB | CSTOPB);
79
sio_.c_cflag |= CS8 | CREAD | CLOCAL;
80
sio_.c_lflag &= ~(ICANON | ECHO | ISIG | IEXTEN);
86
if (! setBaudrate(baudrate)) {
95
if (fd_ != InvalidFd) {
102
bool isConnected(void)
104
return (fd_ == InvalidFd) ? false : true;
108
bool setBaudrate(long baudrate)
110
long baudrate_value = -1;
111
enum { ErrorMessageSize = 256 };
112
char error_message[ErrorMessageSize];
117
baudrate_value = B4800;
121
baudrate_value = B9600;
125
baudrate_value = B19200;
129
baudrate_value = B38400;
133
baudrate_value = B57600;
137
baudrate_value = B115200;
141
sprintf(error_message, "No handle baudrate value: %ld", baudrate);
142
error_message_ = string(error_message);
147
cfsetospeed(&sio_, baudrate_value);
148
cfsetispeed(&sio_, baudrate_value);
149
tcsetattr(fd_, TCSANOW, &sio_);
156
int send(const char* data, int count)
158
if (! isConnected()) {
159
error_message_ = "no connection.";
162
return write(fd_, data, count);
166
int receive(char buffer[], int count, int timeout)
170
// �w��T�C�Y�̓ǂݏo�����s��
171
int n = read(fd_, buffer, count);
177
// ��M���������Ă�����A�߂�
178
if (filled >= count) {
182
// �^�C���A�E�g�t���œǂݏo�����s��
183
while (filled < count) {
184
if (! waitReceive(timeout)) {
188
int required_n = count - filled;
189
n = read(fd_, &buffer[filled], required_n);
191
/* �ǂݏo���G���[�B���݂܂ł̎�M���e�Ŗ߂� */
203
tcflush(fd_, TCIOFLUSH);