RS232_LIB void rs232_end(struct rs232_port_t *p) { struct rs232_windows_t *wx = p->pt; DBG("p=%p p->pt=%p\n", (void *)p, p->pt); if (!rs232_port_open(p)) { free(p->pt); free(p); return; } rs232_flush(p); if (!SetCommState(wx->fd, &wx->old_dcb)) { DBG("SetCommState() %s\n", last_error()); return; } if (!SetCommTimeouts(wx->fd, &wx->old_tm)) { DBG("SetCommTimeouts() %s\n", last_error()); return; } rs232_close(p); free(p->pt); free(p); }
/* error = port:flush() */ static int lua_port_flush(lua_State *L) { struct rs232_port_t *p = *(struct rs232_port_t**) luaL_checkudata(L, 1, MODULE_NAMESPACE); if (p == NULL || !rs232_port_open(p)) { lua_pushinteger(L, RS232_ERR_PORT_CLOSED); return 1; } lua_pushinteger(L, rs232_flush(p)); return 1; }
unsigned int rs232_simple_test(void) { unsigned int try = 0; unsigned int bytes = 0; unsigned char data[1]; unsigned int ret = 0; struct rs232_port_t *p = NULL; p = rs232_init(); if (p == NULL) return 1; #ifdef WIN32 rs232_set_device(p, "COM1"); #else rs232_set_device(p, "/dev/ttyS1"); #endif ret = rs232_open(p); if (ret) { rs232_end(p); return err(ret); } rs232_set_baud(p, RS232_BAUD_115200); printf("[*] port settings: %s\n", rs232_to_string(p)); rs232_flush(p); while (try++ < 10) { printf("%02d. [*] read: ", try); data[0] = 0x00; ret = rs232_read_timeout(p, data, 1, &bytes, 1000); if (ret) err(ret); else printf("0x%02x len: %d\n", data[0], bytes); data[0] = 0x05; bytes = 0; printf("%02d. [*] write: ", try); ret = rs232_write_timeout(p, data, 1, &bytes, 1000); if (ret) err(ret); else printf("len: %d\n", bytes); } rs232_end(p); return 0; }
RS232_LIB unsigned int rs232_open(struct rs232_port_t *p) { wchar_t *wname = a2w(fix_device_name(p->dev)); struct rs232_windows_t *wx = p->pt; DBG("p=%p p->pt=%p name='%s' fix='%s'\n", (void *)p, p->pt, p->dev, fix_device_name(p->dev)); if (wname == NULL) return RS232_ERR_UNKNOWN; wx->fd = CreateFile(wname, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (wname) free(wname); if (wx->fd == INVALID_HANDLE_VALUE) { DBG("CreateFile() %s\n", last_error()); return RS232_ERR_OPEN; } p->status = RS232_PORT_OPEN; rs232_flush(p); GET_PORT_STATE(wx->fd, &wx->old_dcb); GET_PORT_TIMEOUTS(wx->fd, &wx->old_tm); port_timeout(p, wx->r_timeout, wx->w_timeout); port_buffers(p, wx->r_buffer, wx->w_buffer); rs232_set_baud(p, p->baud); rs232_set_data(p, p->data); rs232_set_parity(p, p->parity); rs232_set_stop(p, p->stop); rs232_set_flow(p, p->flow); return RS232_ERR_NOERROR; }
static int init_serial(void) { unsigned int ret = 0; port = rs232_init(); if (port == NULL) return 1; rs232_set_device(port, portname); ret = rs232_open(port); // open port if (ret) { printf("%s (%s)\n", rs232_strerror(ret), errno > 0 ? strerror(errno) : ""); return ret; } rs232_set_baud(port, RS232_BAUD_115200); rs232_flush(port); // wait for arduino init sleep(3); return 0; }
int term_phoenix_reset(uint8_t * atr, int maxlen) { int res; rs232_flush(fd); // res = rs232_set_dtr(fd, 1); res = rs232_set_rts(fd, 1); if (res < 0) return res; // 20ms should do the trick usleep(20000); res = rs232_set_rts(fd, 0); if (res < 0) return res; return rs232_read(fd, atr, maxlen); }