コード例 #1
0
ファイル: test_serial.c プロジェクト: HackLinux/c-periphery
void test_loopback(void) {
    serial_t serial;
    unsigned int count;
    time_t start, stop;
    uint8_t lorem_ipsum[] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    uint8_t lorem_hugesum[4096*3];
    uint8_t buf[sizeof(lorem_hugesum)];

    ptest();

    passert(serial_open(&serial, device, 115200) == 0);

    /* Test write/flush/read */
    passert(serial_write(&serial, lorem_ipsum, sizeof(lorem_ipsum)) == sizeof(lorem_ipsum));
    passert(serial_flush(&serial) == 0);
    passert(serial_read(&serial, buf, sizeof(lorem_ipsum), -1) == sizeof(lorem_ipsum));
    passert(memcmp(lorem_ipsum, buf, sizeof(lorem_ipsum)) == 0);

    /* Test poll/write/flush/poll/input waiting/read */
    passert(serial_poll(&serial, 500) == 0); /* Should timeout */
    passert(serial_write(&serial, lorem_ipsum, sizeof(lorem_ipsum)) == sizeof(lorem_ipsum));
    passert(serial_flush(&serial) == 0);
    passert(serial_poll(&serial, 500) == 1);
    usleep(500000);
    passert(serial_input_waiting(&serial, &count) == 0);
    passert(count == sizeof(lorem_ipsum));
    passert(serial_read(&serial, buf, sizeof(lorem_ipsum), -1) == sizeof(lorem_ipsum));
    passert(memcmp(lorem_ipsum, buf, sizeof(lorem_ipsum)) == 0);

    /* Test non-blocking poll */
    passert(serial_poll(&serial, 0) == 0);

    /* Test a very large read-write (likely to exceed internal buffer size (~4096)) */
    memset(lorem_hugesum, 0xAA, sizeof(lorem_hugesum));
    passert(serial_write(&serial, lorem_hugesum, sizeof(lorem_hugesum)) == sizeof(lorem_hugesum));
    passert(serial_flush(&serial) == 0);
    passert(serial_read(&serial, buf, sizeof(lorem_hugesum), -1) == sizeof(lorem_hugesum));
    passert(memcmp(lorem_hugesum, buf, sizeof(lorem_hugesum)) == 0);

    /* Test read timeout */
    start = time(NULL);
    passert(serial_read(&serial, buf, sizeof(buf), 2000) == 0);
    stop = time(NULL);
    passert((stop - start) > 1);

    /* Test non-blocking read */
    start = time(NULL);
    passert(serial_read(&serial, buf, sizeof(buf), 0) == 0);
    stop = time(NULL);
    /* Assuming we weren't context switched out for a second and weren't on a
     * thin time boundary ;) */
    passert((stop - start) == 0);

    passert(serial_close(&serial) == 0);
}
コード例 #2
0
ファイル: rs.c プロジェクト: DF4OR/hamlib
/*
 * rs_transaction
 * We assume that rig!=NULL, rig->state!= NULL, data!=NULL, data_len!=NULL
 */
int rs_transaction(RIG *rig, const char *cmd, int cmd_len, char *data, int *data_len)
{
	int retval;
	struct rig_state *rs;

	rs = &rig->state;

	serial_flush(&rs->rigport);

	retval = write_block(&rs->rigport, cmd, cmd_len);
	if (retval != RIG_OK)
		return retval;


	/* no data expected */
	if (!data || !data_len)
		return RIG_OK;

	retval = read_string(&rs->rigport, data, BUFSZ, CR, 1);
	if (retval < 0)
		return retval;
	*data_len = retval;

	return RIG_OK;
}
コード例 #3
0
ファイル: ar3000.c プロジェクト: DF4OR/hamlib
/*
 * ar3k_transaction
 * We assume that rig!=NULL, rig->state!= NULL
 * Otherwise, you'll get a nice seg fault. You've been warned!
 * return value: RIG_OK if everything's fine, negative value otherwise
 * TODO: error case handling
 */
static int ar3k_transaction(RIG *rig, const char *cmd, int cmd_len, char *data, int *data_len)
{
	int retval;
	struct rig_state *rs;

	rs = &rig->state;

	serial_flush(&rs->rigport);

	retval = write_block(&rs->rigport, cmd, cmd_len);
	if (retval != RIG_OK)
		return retval;

	/* will flush data on next transaction */
	if (!data || !data_len)
		return RIG_OK;

	retval = read_string(&rs->rigport, data, BUFSZ, EOM, strlen(EOM));
	if (retval == -RIG_ETIMEOUT)
		retval = 0;
	if (retval < 0)
		return retval;

	*data_len = retval;

	return RIG_OK;
}
コード例 #4
0
ファイル: rfBeeSerial.c プロジェクト: bpg/RFBee
int changeUartBaudRate() {
	printf("ok\r\n");
	serial_flush();
	_delay_ms(1);
	setUartBaudRate();
	return OK;
}
コード例 #5
0
dc_status_t
shearwater_common_open (shearwater_common_device_t *device, dc_context_t *context, const char *name)
{
	// Open the device.
	int rc = serial_open (&device->port, context, name);
	if (rc == -1) {
		ERROR (context, "Failed to open the serial port.");
		return DC_STATUS_IO;
	}

	// Set the serial communication protocol (115200 8N1).
	rc = serial_configure (device->port, 115200, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE);
	if (rc == -1) {
		ERROR (context, "Failed to set the terminal attributes.");
		serial_close (device->port);
		return DC_STATUS_IO;
	}

	// Set the timeout for receiving data (3000ms).
	if (serial_set_timeout (device->port, 3000) == -1) {
		ERROR (context, "Failed to set the timeout.");
		serial_close (device->port);
		return DC_STATUS_IO;
	}

	// Make sure everything is in a sane state.
	serial_sleep (device->port, 300);
	serial_flush (device->port, SERIAL_QUEUE_BOTH);

	return DC_STATUS_SUCCESS;
}
コード例 #6
0
ファイル: api.c プロジェクト: BayLibre/libsigrok
static GSList *center_scan(const char *conn, const char *serialcomm, int idx)
{
	int i;
	struct sr_dev_inst *sdi;
	struct dev_context *devc;
	struct sr_serial_dev_inst *serial;

	serial = sr_serial_dev_inst_new(conn, serialcomm);

	if (serial_open(serial, SERIAL_RDWR) != SR_OK)
		return NULL;

	serial_flush(serial);

	sr_info("Found device on port %s.", conn);

	sdi = g_malloc0(sizeof(struct sr_dev_inst));
	sdi->status = SR_ST_INACTIVE;
	sdi->vendor = g_strdup(center_devs[idx].vendor);
	sdi->model = g_strdup(center_devs[idx].device);
	devc = g_malloc0(sizeof(struct dev_context));
	sdi->inst_type = SR_INST_SERIAL;
	sdi->conn = serial;
	sdi->priv = devc;

	for (i = 0; i < center_devs[idx].num_channels; i++)
		sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);

	serial_close(serial);

	return g_slist_append(NULL, sdi);
}
コード例 #7
0
static SERIALPORT *serial_open(const char *device){
    HANDLE fh;
    DCB dcb={sizeof(DCB)};
    COMMTIMEOUTS timeouts;
    SERIALPORT *port;

    fh = CreateFile(device,GENERIC_READ|GENERIC_WRITE,0,NULL,
      OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
    if (!fh) return NULL;

    port = malloc(sizeof(SERIALPORT));
    ZeroMemory(port, sizeof(SERIALPORT));
    port->fh = fh;

    /* save current port settings */
    GetCommState(fh,&port->dcb_save);
    GetCommTimeouts(fh,&port->timeouts_save);

    dcb.DCBlength=sizeof(DCB);
    BuildCommDCB("96,n,8,1",&dcb);
    SetCommState(fh,&dcb);

    ZeroMemory(&timeouts,sizeof(timeouts));
    timeouts.ReadTotalTimeoutConstant=1;
    timeouts.WriteTotalTimeoutConstant=1;
    SetCommTimeouts(fh,&timeouts);

    serial_flush(port);

    return port;
}
コード例 #8
0
ファイル: drake.c プロジェクト: jessicalh/hamlib
/*
 * drake_transaction
 * We assume that rig!=NULL, rig->state!= NULL, data!=NULL, data_len!=NULL
 */
int drake_transaction(RIG *rig, const char *cmd, int cmd_len, char *data, int *data_len)
{
	int retval;
	struct rig_state *rs;

	rs = &rig->state;

	serial_flush(&rs->rigport);

	retval = write_block(&rs->rigport, cmd, cmd_len);

	if (retval != RIG_OK)
		return retval;

	/* no data expected, TODO: flush input? */
	if (!data || !data_len)
		return 0;

	retval = read_string(&rs->rigport, data, BUFSZ, LF, 1);

	if (retval == -RIG_ETIMEOUT)
		retval = 0;

	if (retval < 0)
		return retval;

	*data_len = retval;

	return RIG_OK;
}
コード例 #9
0
ファイル: ic10.c プロジェクト: jnse/qtar8200
/**
 * ic10_transaction
 * Assumes rig!=NULL rig->state!=NULL rig->caps!=NULL
**/
int ic10_transaction (RIG *rig, const char *cmd, int cmd_len, char *data, int *data_len)
{
	int retval;
	struct rig_state *rs;

	rs = &rig->state;

	serial_flush(&rs->rigport);

	retval = write_block(&rs->rigport, cmd, cmd_len);
	if (retval != RIG_OK)
		return retval;

	if (!data || !data_len)
		return 0;

	retval = read_string(&rs->rigport, data, 50, ";", 1);
   	if (retval == -RIG_ETIMEOUT)
		retval = 0;
	if (retval < 0)
		return retval;
	*data_len = retval;

    return RIG_OK;
}
コード例 #10
0
ファイル: racal.c プロジェクト: DF4OR/hamlib
/*
 * racal_transaction
 * We assume that rig!=NULL, rig->state!= NULL
 *
 * TODO: Status Response handling with G/T commands
 */
static int racal_transaction(RIG *rig, const char *cmd, char *data, int *data_len)
{
	struct racal_priv_data *priv = (struct racal_priv_data*)rig->state.priv;
	struct rig_state *rs = &rig->state;
	char cmdbuf[BUFSZ+1];
	int cmd_len;
	int retval;

	cmd_len = sprintf(cmdbuf, SOM "%d%s" EOM, priv->receiver_id, cmd);

	serial_flush(&rs->rigport);

	retval = write_block(&rs->rigport, cmdbuf, cmd_len);
	if (retval != RIG_OK)
		return retval;


	/* no data expected */
	if (!data || !data_len) {
		return retval;
	}

	retval = read_string(&rs->rigport, data, BUFSZ, EOM, strlen(EOM));
	if (retval <= 0)
		return retval;

	/* strip CR from string
	 */
	if (data[retval-1] == '\x0d')  {
		data[--retval] = '\0';	/* chomp */
	}
	*data_len = retval;
	return RIG_OK;
}
コード例 #11
0
/******** OS Specific Serial I/O routines *******/
#if TARGET_HOST_POSIX_X11 /* ==> Linux/BSD/UNIX POSIX serial I/O */
static SERIALPORT *serial_open ( const char *device )
{
    int fd;
    struct termios termio;
    SERIALPORT *port;

    fd = open(device, O_RDWR | O_NONBLOCK );
    if (fd <0) {
        perror(device);
        return NULL;
    }

    port = malloc(sizeof(SERIALPORT));
    memset(port, 0, sizeof(SERIALPORT));
    port->fd = fd;

    /* save current port settings */
    tcgetattr(fd,&port->termio_save);

    memset(&termio, 0, sizeof(termio));
    termio.c_cflag = CS8 | CREAD | HUPCL ;
    termio.c_iflag = IGNPAR | IGNBRK ;
    termio.c_cc[VTIME]    = 0;   /* inter-character timer */
    termio.c_cc[VMIN]     = 1;   /* block read until 1 chars received, when blocking I/O */

    cfsetispeed(&termio, B9600);
    cfsetospeed(&termio, B9600);
    tcsetattr(fd,TCSANOW,&termio);

    serial_flush(port);
    return port;
}
コード例 #12
0
int get_num_files(serial_handle_t serial_handle) {
    int r;
    char reply[2];


    // flush the serial port
    r = serial_flush(serial_handle);
    if (r < 0) {
        return -1;
    }

    // send the command to request the number of recorded files
    r = serial_write_byte(serial_handle, 0x01);
    if (r < 1) {
        return -1;
    }

    // read the reply, 2 bytes
    r = serial_read(serial_handle, reply, 2, 500*1000);
    if (r < 2) {
        return -1;
    }

    g_debug("cmd 0x01 (get num files), 2-byte response: 0x%02x 0x%02x", reply[0], reply[1]);

    // but only the second byte matters
    return reply[1];
} 
コード例 #13
0
ファイル: frg100.c プロジェクト: DF4OR/hamlib
int frg100_get_level(RIG *rig, vfo_t vfo, setting_t level, value_t *val)
{
  unsigned char cmd[YAESU_CMD_LENGTH] = { 0x00, 0x00, 0x00, 0x00, 0xf7};
  int retval;

  if (level != RIG_LEVEL_RAWSTR)
	  return -RIG_EINVAL;

  serial_flush(&rig->state.rigport);

  /* send READ STATUS(Meter only) cmd to rig  */
  retval = write_block(&rig->state.rigport, (char *) cmd, YAESU_CMD_LENGTH);
  if (retval < 0)
	  return retval;

  /* read back the 1 byte */
  retval = read_block(&rig->state.rigport, (char *) cmd, 5);

  if (retval < 1) {
	rig_debug(RIG_DEBUG_ERR,"%s: read meter failed %d\n",
			__FUNCTION__,retval);

	return retval < 0 ? retval : -RIG_EIO;
  }
  val->i = cmd[0];

  return RIG_OK;
}
コード例 #14
0
ファイル: easycomm.c プロジェクト: W4AQL/hamlib
/**
 *  easycomm_transaction
 *
 *  Assumes rot!=NULL and cmdstr!=NULL
 *
 *  cmdstr   - string to send to rotator
 *  data     - buffer for reply string
 *  data_len - (input) Maximum size of buffer
 *             (output) Number of bytes read.
 */
static int
easycomm_transaction (ROT *rot, const char *cmdstr, char *data, size_t data_len)
{
  struct rot_state *rs;
  int retval;

  rig_debug(RIG_DEBUG_TRACE, "%s called: %s\n", __FUNCTION__, cmdstr);

  if (!rot )
    return -RIG_EINVAL;

  rs = &rot->state;
  serial_flush(&rs->rotport);
  retval = write_block(&rs->rotport, cmdstr, strlen(cmdstr));
  if (retval != RIG_OK) {
      goto transaction_quit;
  }

  if (data == NULL || data_len <= 0)
    return RIG_OK;  /* don't want a reply */

  memset(data,0,data_len);
  retval = read_string(&rs->rotport, data, data_len, "\n", 1);
  if (retval < 0) {
    rig_debug(RIG_DEBUG_TRACE, "%s read_string failed with status %d\n", __FUNCTION__, retval);
    goto transaction_quit;
  } else {
    rig_debug(RIG_DEBUG_TRACE, "%s read_string: %s\n", __FUNCTION__, data);
    retval = RIG_OK;
  }

  transaction_quit:
  return retval;
}
コード例 #15
0
ファイル: pcr.c プロジェクト: DF4OR/hamlib
static int
pcr_transaction(RIG * rig, const char *cmd)
{
	int err;
	struct rig_state *rs = &rig->state;
	struct pcr_priv_caps *caps = pcr_caps(rig);
	struct pcr_priv_data *priv = (struct pcr_priv_data *) rs->priv;

	rig_debug(RIG_DEBUG_TRACE, "%s: cmd = %s\n",
		__func__, cmd);

	if (!priv->auto_update)
		serial_flush(&rs->rigport);

	pcr_send(rig, cmd);

	/* the pcr does not give ack in auto update mode */
	if (priv->auto_update)
		return RIG_OK;

	err = pcr_read_block(rig, priv->reply_buf, caps->reply_size);
	if (err < 0) {
		rig_debug(RIG_DEBUG_ERR,
			  "%s: read error, %s\n", __func__, strerror(errno));
		return err;
	}

	if (err != caps->reply_size) {
		priv->sync = 0;
		return -RIG_EPROTO;
	}

	return pcr_parse_answer(rig, &priv->reply_buf[caps->reply_offset], err);
}
コード例 #16
0
ファイル: shutdown.c プロジェクト: PachecOS/FinalProj02
/* Powers down the machine we're running on,
   as long as we're running on Bochs or QEMU. */
void
shutdown_power_off (void)
{
  const char s[] = "Shutdown";
  const char *p;

#ifdef FILESYS
  filesys_done ();
#endif

  print_stats ();

  printf ("Powering off...\n");
  serial_flush ();

  /* ACPI Shutdown sequence supported by Bochs and QEMU
     http://forum.osdev.org/viewtopic.php?t=16990  */
  outw( 0xB004, 0x0 | 0x2000 );

  /* This is a special power-off sequence supported by Bochs and
     QEMU, but not by physical hardware. */
  for (p = s; *p != '\0'; p++)
    outb (0x8900, *p);

  /* This will power off a VMware VM if "gui.exitOnCLIHLT = TRUE"
     is set in its configuration file.  (The "pintos" script does
     that automatically.)  */
  asm volatile ("cli; hlt" : : : "memory");

  /* None of those worked. */
  printf ("still running...\n");
  for (;;);
}
コード例 #17
0
ファイル: api.c プロジェクト: Matthias-Heidbrink/libsigrok-mh
static GSList *scan(struct sr_dev_driver *di, GSList *options)
{
	struct sr_dev_inst *sdi;
	struct drv_context *drvc;
	struct sr_config *src;
	struct sr_serial_dev_inst *serial;
	GSList *l, *devices;
	const char *conn, *serialcomm;

	devices = NULL;
	drvc = di->context;
	drvc->instances = NULL;
	conn = serialcomm = NULL;

	for (l = options; l; l = l->next) {
		src = l->data;
		switch (src->key) {
		case SR_CONF_CONN:
			conn = g_variant_get_string(src->data, NULL);
			break;
		case SR_CONF_SERIALCOMM:
			serialcomm = g_variant_get_string(src->data, NULL);
			break;
		}
	}
	if (!conn)
		return NULL;
	if (!serialcomm)
		serialcomm = SERIALCOMM;

	/*
	 * We cannot scan for this device because it is write-only.
	 * So just check that the port parameters are valid and assume that
	 * the device is there.
	 */

	serial = sr_serial_dev_inst_new(conn, serialcomm);

	if (serial_open(serial, SERIAL_RDWR) != SR_OK)
		return NULL;

	serial_flush(serial);
	serial_close(serial);

	sr_spew("Conrad DIGI 35 CPU assumed at %s.", conn);

	sdi = g_malloc0(sizeof(struct sr_dev_inst));
	sdi->status = SR_ST_ACTIVE;
	sdi->vendor = g_strdup("Conrad");
	sdi->model = g_strdup("DIGI 35 CPU");
	sdi->conn = serial;
	sdi->priv = NULL;
	sdi->driver = di;
	sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "CH1");
	drvc->instances = g_slist_append(drvc->instances, sdi);
	devices = g_slist_append(devices, sdi);

	return devices;
}
コード例 #18
0
/* Define our fault handler. This one is not mandatory, the dummy fault handler
 * will be used when it's not overridden here.
 * Note : The default one does a simple infinite loop. If the watchdog is deactivated
 * the system will hang.
 */
void fault_info(const char* name, uint32_t len)
{
	serial_write(0, name, len);
	/* Wait for end of Tx */
	serial_flush(0);
	/* FIXME : Perform soft reset of the micro-controller ! */
	while (1);
}
コード例 #19
0
ファイル: serial_posix.c プロジェクト: dlebed/stm32blctl
void serial_close(serial_t *h) {
	assert(h && h->fd > -1);

	serial_flush(h);
	tcsetattr(h->fd, TCSANOW, &h->oldtio);
	close(h->fd);
	free(h);
}
コード例 #20
0
ファイル: serial.c プロジェクト: AgustinParmisano/pinguino32
u8 serial_getkey()
{
	u8 c;
	while (!(serial_available()));
	c = serial_read();
	serial_flush();
	return (c);
}
コード例 #21
0
ファイル: gs232a.c プロジェクト: DF4OR/hamlib
/**
 * gs232a_transaction
 *
 * cmdstr - Command to be sent to the rig.
 * data - Buffer for reply string.  Can be NULL, indicating that no reply is
 *        is needed, but answer will still be read.
 * data_len - in: Size of buffer. It is the caller's responsibily to provide
 *            a large enough buffer for all possible replies for a command.
 *
 * returns:
 *   RIG_OK  -  if no error occured.
 *   RIG_EIO  -  if an I/O error occured while sending/receiving data.
 *   RIG_ETIMEOUT  -  if timeout expires without any characters received.
 *   RIG_REJECTED  -  if a negative acknowledge was received or command not
 *                    recognized by rig.
 */
static int
gs232a_transaction (ROT *rot, const char *cmdstr,
                    char *data, size_t data_len)
{
    struct rot_state *rs;
    int retval;
    int retry_read = 0;
    char replybuf[BUFSZ];

    rs = &rot->state;

transaction_write:

    serial_flush(&rs->rotport);

    if (cmdstr) {
        retval = write_block(&rs->rotport, cmdstr, strlen(cmdstr));
        if (retval != RIG_OK)
            goto transaction_quit;
    }

    /* Always read the reply to know whether the cmd went OK */
    if (!data)
        data = replybuf;
    if (!data_len)
        data_len = BUFSZ;

    memset(data,0,data_len);
    retval = read_string(&rs->rotport, data, data_len, REPLY_EOM, strlen(REPLY_EOM));
    if (retval < 0) {
        if (retry_read++ < rot->state.rotport.retry)
            goto transaction_write;
        goto transaction_quit;
    }

#if 0
    /* Check that command termination is correct */
    if (strchr(REPLY_EOM, data[strlen(data)-1])==NULL) {
        rig_debug(RIG_DEBUG_ERR, "%s: Command is not correctly terminated '%s'\n", __FUNCTION__, data);
        if (retry_read++ < rig->state.rotport.retry)
            goto transaction_write;
        retval = -RIG_EPROTO;
        goto transaction_quit;
    }
#endif

    if (data[0] == '?') {
        /* Invalid command */
        rig_debug(RIG_DEBUG_VERBOSE, "%s: Error for '%s': '%s'\n",
                  __FUNCTION__, cmdstr, data);
        retval = -RIG_EPROTO;
        goto transaction_quit;
    }

    retval = RIG_OK;
transaction_quit:
    return retval;
}
コード例 #22
0
ファイル: api.c プロジェクト: russdill/libsigrok
static GSList *mic_scan(const char *conn, const char *serialcomm, int idx)
{
	struct sr_dev_inst *sdi;
	struct drv_context *drvc;
	struct dev_context *devc;
	struct sr_probe *probe;
	struct sr_serial_dev_inst *serial;
	GSList *devices;

	if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
		return NULL;

	if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
		return NULL;

	drvc = mic_devs[idx].di->priv;
	devices = NULL;
	serial_flush(serial);

	/* TODO: Query device type. */
	// ret = mic_cmd_get_device_info(serial);

	sr_info("Found device on port %s.", conn);

	/* TODO: Fill in version from protocol response. */
	if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, mic_devs[idx].vendor,
				    mic_devs[idx].device, "")))
		goto scan_cleanup;

	if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
		sr_err("Device context malloc failed.");
		goto scan_cleanup;
	}

	sdi->inst_type = SR_INST_SERIAL;
	sdi->conn = serial;

	sdi->priv = devc;
	sdi->driver = mic_devs[idx].di;

	if (!(probe = sr_probe_new(0, SR_PROBE_ANALOG, TRUE, "Temperature")))
		goto scan_cleanup;
	sdi->probes = g_slist_append(sdi->probes, probe);

	if (mic_devs[idx].has_humidity) {
		if (!(probe = sr_probe_new(1, SR_PROBE_ANALOG, TRUE, "Humidity")))
			goto scan_cleanup;
		sdi->probes = g_slist_append(sdi->probes, probe);
	}

	drvc->instances = g_slist_append(drvc->instances, sdi);
	devices = g_slist_append(devices, sdi);

scan_cleanup:
	serial_close(serial);

	return devices;
}
コード例 #23
0
ファイル: serial.c プロジェクト: alessiocamillo/telehash-c
// chunkize a packet
void serial_send(pipe_t pipe, lob_t packet, link_t link)
{
  pipe_serial_t to;
  if(!pipe || !packet || !(to = (pipe_serial_t)pipe->arg)) return;

  LOG("chunking a packet of len %d",lob_len(packet));
  util_chunks_send(to->chunks, packet);
  serial_flush(pipe);
}
コード例 #24
0
void setupMockSerial()
{
        if (!s)
                s = serial_create("Mock", 10, BUFF_SIZE,
                                  NULL, NULL, _post_tx_cb, NULL);

        serial_flush(s);
        mock_resetTxBuffer();
}
コード例 #25
0
void serial_close(serial_t *h) {
	if(!h || (h->fd <= -1))
		return;

	serial_flush(h);
	tcsetattr(h->fd, TCSANOW, &h->oldtio);
	close(h->fd);
	free(h);
}
コード例 #26
0
dc_status_t
uwatec_meridian_device_open (dc_device_t **out, dc_context_t *context, const char *name)
{
	if (out == NULL)
		return DC_STATUS_INVALIDARGS;

	// Allocate memory.
	uwatec_meridian_device_t *device = (uwatec_meridian_device_t *) malloc (sizeof (uwatec_meridian_device_t));
	if (device == NULL) {
		ERROR (context, "Failed to allocate memory.");
		return DC_STATUS_NOMEMORY;
	}

	// Initialize the base class.
	device_init (&device->base, context, &uwatec_meridian_device_vtable);

	// Set the default values.
	device->port = NULL;
	device->timestamp = 0;
	device->systime = (dc_ticks_t) -1;
	device->devtime = 0;

	// Open the device.
	int rc = serial_open (&device->port, context, name);
	if (rc == -1) {
		ERROR (context, "Failed to open the serial port.");
		free (device);
		return DC_STATUS_IO;
	}

	// Set the serial communication protocol (57600 8N1).
	rc = serial_configure (device->port, 57600, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE);
	if (rc == -1) {
		ERROR (context, "Failed to set the terminal attributes.");
		serial_close (device->port);
		free (device);
		return DC_STATUS_IO;
	}

	// Set the timeout for receiving data (3000ms).
	if (serial_set_timeout (device->port, 3000) == -1) {
		ERROR (context, "Failed to set the timeout.");
		serial_close (device->port);
		free (device);
		return DC_STATUS_IO;
	}

	// Make sure everything is in a sane state.
	serial_flush (device->port, SERIAL_QUEUE_BOTH);

	// Perform the handshaking.
	uwatec_meridian_handshake (device);

	*out = (dc_device_t*) device;

	return DC_STATUS_SUCCESS;
}
コード例 #27
0
void serial_close(serial_t *h) 
{
	assert(h && h->fd != INVALID_HANDLE_VALUE);

	serial_flush(h);
	SetCommState(h->fd, &h->oldtio);
	CloseHandle(h->fd);
	free(h);
}
コード例 #28
0
ファイル: prm80.c プロジェクト: airween/hamlib
/*
 * prm80_transaction
 * We assume that rig!=NULL, rig->state!= NULL, data!=NULL, data_len!=NULL
 * Otherwise, you'll get a nice seg fault. You've been warned!
 * TODO: error case handling
 */
static int prm80_transaction(RIG *rig, const char *cmd, int cmd_len, char *data, int *data_len)
{
	int retval, i;
	struct rig_state *rs;

	rs = &rig->state;

	serial_flush(&rs->rigport);

	retval = write_block(&rs->rigport, cmd, cmd_len);
	if (retval != RIG_OK)
		return retval;

	/* no data wanted, but flush it anyway */
	if (!data || !data_len) {
	    char retbuf[BUFSZ+1];

		retval = read_string(&rs->rigport, retbuf, BUFSZ, LF, strlen(LF));
		if (retval < 0)
			return retval;

		retbuf[retval] = '\0';
#if 0
		/*
	 	 * Does transceiver sends back ">" ?
	 	 */
		if (strstr(retbuf, PROMPT))
			return RIG_OK;
		else
			return -RIG_ERJCTED;
#else
		return RIG_OK;
#endif
	}

	retval = read_string(&rs->rigport, data, BUFSZ, LF, strlen(LF));
	if (retval == -RIG_ETIMEOUT)
		retval = 0;
	if (retval < 0)
		return retval;

    /* Clear possible MSB, because of 7S1 */
    for (i=0; i<retval; i++)
        data[i] &= 0x7f;

    *data_len = retval;

    /* chomp CR/LF from string */
    if (*data_len >= 2 && data[*data_len-1] == '\x0a')
	    *data_len -= 2;

    data[*data_len] = '\0';

    return RIG_OK;
}
コード例 #29
0
ファイル: echodemo.c プロジェクト: eseawind/touchCabinet
static int send_pack(char *send_packet_buf)
{
	int send_len;
     send_len = send_packet_buf[2]*256 + send_packet_buf[3];//读取指令长度
     send_len = send_len+6;//根据协议,添加上头尾的长度	
     if (!serial_flush(serial_id, SERIAL_FLUSH_TX))
        return SERIAL_STATUS_PORT_ERR;

    if (serial_write(serial_id, send_packet_buf, send_len) != send_len)
        return SERIAL_STATUS_PORT_ERR;    
}
コード例 #30
0
static int lua_serial_flush(lua_State *L) {
    serial_t *serial;
    int ret;

    serial = luaL_checkudata(L, 1, "periphery.Serial");

    if ((ret = serial_flush(serial)) < 0)
        return lua_serial_error(L, ret, serial_errno(serial), "Error: %s", serial_errmsg(serial));

    return 0;
}