Exemple #1
0
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;
}
Exemple #2
0
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;
}
Exemple #3
0
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;
}
Exemple #4
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;
}