RS232_LIB unsigned int rs232_write_timeout(struct rs232_port_t *p, unsigned char *buf, unsigned int buf_len, unsigned int *write_len, unsigned int timeout) { unsigned int w = 0; struct rs232_windows_t *wx = p->pt; unsigned int wt = wx->w_timeout; DBG("p=%p p->pt=%p buf_len:%d\n", (void *)p, p->pt, buf_len); if (!rs232_port_open(p)) return RS232_ERR_PORT_CLOSED; if (port_timeout(p, wx->r_timeout, timeout)) return RS232_ERR_UNKNOWN; if (!WriteFile(wx->fd, buf, buf_len, &w, NULL)) { *write_len = 0; DBG("WriteFile() %s\n", last_error()); return RS232_ERR_WRITE; } if (port_timeout(p, wx->r_timeout, wt)) return RS232_ERR_UNKNOWN; *write_len = w; DBG("write_len=%d hex='%s' ascii='%s'\n", w, rs232_hex_dump(buf, w), rs232_ascii_dump(buf, w)); return RS232_ERR_NOERROR; }
RS232_LIB unsigned int rs232_read_timeout(struct rs232_port_t *p, unsigned char *buf, unsigned int buf_len, unsigned int *read_len, unsigned int timeout) { unsigned int r = 0; struct rs232_windows_t *wx = p->pt; unsigned int rt = wx->r_timeout; DBG("p=%p p->pt=%p buf_len: %d timeout: %d\n", (void *)p, p->pt, buf_len, timeout); if (!rs232_port_open(p)) return RS232_ERR_PORT_CLOSED; *read_len = 0; if (port_timeout(p, timeout, wx->w_timeout)) return RS232_ERR_UNKNOWN; if (!ReadFile(wx->fd, buf, buf_len, &r, NULL)) { *read_len = 0; DBG("ReadFile() %s\n", last_error()); return RS232_ERR_READ; } if (port_timeout(p, rt, wx->w_timeout)) return RS232_ERR_UNKNOWN; *read_len = r; DBG("read_len=%d hex='%s' ascii='%s'\n", r, rs232_hex_dump(buf, r), rs232_ascii_dump(buf, r)); return RS232_ERR_NOERROR; }
RS232_LIB unsigned int rs232_read_timeout(struct rs232_port_t *p, unsigned char *buf, unsigned int buf_len, unsigned int *read_len, unsigned int timeout) { unsigned int r = 0; struct rs232_windows_t *wx = p->pt; unsigned int rt = wx->r_timeout; DBG("p=%p p->pt=%p buf_len: %d timeout: %d\n", (void *)p, p->pt, buf_len, timeout); if (!rs232_port_open(p)) return RS232_ERR_PORT_CLOSED; *read_len = 0; if (port_timeout(p, timeout, wx->w_timeout)) return RS232_ERR_UNKNOWN; if (!ReadFile(wx->fd, buf, buf_len, &r, NULL)) { *read_len = 0; DBG("ReadFile() %s\n", last_error()); return RS232_ERR_READ; } if (port_timeout(p, rt, wx->w_timeout)) return RS232_ERR_UNKNOWN; *read_len = r; DBG("read_len=%d hex='%s' ascii='%s'\n", r, rs232_hex_dump(buf, r), rs232_ascii_dump(buf, r)); /* TODO - This is lame, since we rely on the fact, that if we read 0 bytes, * that the read probably timeouted. So we should rather measure the reading * interval or rework it using overlapped I/O */ return *read_len == 0 ? RS232_ERR_TIMEOUT : RS232_ERR_NOERROR; }