38 #include <sys/select.h> 40 namespace rp{
namespace arch{
namespace net{
43 :
rp::hal::serial_rxtx()
51 raw_serial::~raw_serial()
57 bool raw_serial::open()
59 return open(_portName, _baudrate, _flags);
62 bool raw_serial::bind(
const char * portname,
uint32_t baudrate,
uint32_t flags)
64 strncpy(_portName, portname,
sizeof(_portName));
70 bool raw_serial::open(
const char * portname,
uint32_t baudrate,
uint32_t flags)
72 if (isOpened()) close();
74 serial_fd = ::open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
76 if (serial_fd == -1)
return false;
78 struct termios options, oldopt;
79 tcgetattr(serial_fd, &oldopt);
80 bzero(&options,
sizeof(
struct termios));
82 _u32 termbaud = getTermBaudBitmap(baudrate);
84 if (termbaud == (
_u32)-1) {
88 cfsetispeed(&options, termbaud);
89 cfsetospeed(&options, termbaud);
92 options.c_cflag |= (CLOCAL | CREAD);
95 options.c_cflag &= ~PARENB;
96 options.c_cflag &= ~CSTOPB;
98 options.c_cflag &= ~CSIZE;
99 options.c_cflag |= CS8;
102 options.c_cflag &= ~CNEW_RTSCTS;
105 options.c_iflag &= ~(IXON | IXOFF | IXANY);
108 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
110 options.c_oflag &= ~OPOST;
112 tcflush(serial_fd,TCIFLUSH);
120 if (tcsetattr(serial_fd, TCSANOW, &options))
126 _is_serial_opened =
true;
134 void raw_serial::close()
140 _is_serial_opened =
false;
146 if (!isOpened())
return 0;
148 if (
data == NULL ||
size ==0)
return 0;
153 int ans = ::write(serial_fd,
data + tx_len,
size-tx_len);
155 if (ans == -1)
return tx_len;
158 required_tx_cnt = tx_len;
159 }
while (tx_len<
size);
169 if (!isOpened())
return 0;
171 int ans = ::read(serial_fd,
data,
size);
173 if (ans == -1) ans=0;
174 required_rx_cnt = ans;
179 void raw_serial::flush(
_u32 flags)
181 tcflush(serial_fd,TCIFLUSH);
186 if (returned_size) *returned_size = required_tx_cnt;
192 if (!isOpened() )
return -1;
194 if (returned_size) *returned_size = required_rx_cnt;
206 struct timeval timeout_val;
210 FD_SET(serial_fd, &input_set);
211 max_fd = serial_fd + 1;
214 timeout_val.tv_sec = timeout / 1000;
215 timeout_val.tv_usec = (timeout % 1000) * 1000;
219 if ( ioctl(serial_fd, FIONREAD, returned_size) == -1)
return ANS_DEV_ERR;
220 if (*returned_size >= data_count)
229 int n = ::select(max_fd, &input_set, NULL, NULL, &timeout_val);
244 assert (FD_ISSET(serial_fd, &input_set));
247 if ( ioctl(serial_fd, FIONREAD, returned_size) == -1)
return ANS_DEV_ERR;
248 if (*returned_size >= data_count)
254 int remain_timeout = timeout_val.tv_sec*1000000 + timeout_val.tv_usec;
255 int expect_remain_time = (data_count - *returned_size)*1000000*8/_baudrate;
256 if (remain_timeout > expect_remain_time)
257 usleep(expect_remain_time);
265 size_t raw_serial::rxqueue_count()
267 if ( !isOpened() )
return 0;
270 if (::ioctl(serial_fd, FIONREAD, &remaining) == -1)
return 0;
274 void raw_serial::setDTR()
276 if ( !isOpened() )
return;
279 ioctl(serial_fd, TIOCMBIS, &dtr_bit);
282 void raw_serial::clearDTR()
284 if ( !isOpened() )
return;
287 ioctl(serial_fd, TIOCMBIC, &dtr_bit);
290 void raw_serial::_init()
294 required_tx_cnt = required_rx_cnt = 0;
299 _u32 raw_serial::getTermBaudBitmap(
_u32 baud)
301 #define BAUD_CONV(_baud_) case _baud_: return _baud_ 335 namespace rp{
namespace hal{
#define BAUD_CONV(_baud_)
static serial_rxtx * CreateRxTx()
static void ReleaseRxTx(serial_rxtx *)
typedef _word_size_t(THREAD_PROC *thread_proc_t)(void *)
GLuint GLsizei GLsizei * length
unsigned __int32 uint32_t
GLsizei GLsizei GLenum GLenum const GLvoid * data