コード例 #1
0
ファイル: test_serial.c プロジェクト: HackLinux/c-periphery
void test_open_config_close(void) {
    serial_t serial;
    uint32_t baudrate;
    unsigned int databits;
    serial_parity_t parity;
    unsigned int stopbits;
    bool xonxoff;
    bool rtscts;

    ptest();

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

    /* Check default settings */
    passert(serial_get_baudrate(&serial, &baudrate) == 0);
    passert(baudrate == 115200);
    passert(serial_get_databits(&serial, &databits) == 0);
    passert(databits == 8);
    passert(serial_get_parity(&serial, &parity) == 0);
    passert(parity == PARITY_NONE);
    passert(serial_get_stopbits(&serial, &stopbits) == 0);
    passert(stopbits == 1);
    passert(serial_get_xonxoff(&serial, &xonxoff) == 0);
    passert(xonxoff == false);
    passert(serial_get_rtscts(&serial, &rtscts) == 0);
    passert(rtscts == false);

    /* Change some stuff around */
    passert(serial_set_baudrate(&serial, 4800) == 0);
    passert(serial_get_baudrate(&serial, &baudrate) == 0);
    passert(baudrate == 4800);
    passert(serial_set_baudrate(&serial, 9600) == 0);
    passert(serial_get_baudrate(&serial, &baudrate) == 0);
    passert(baudrate == 9600);
    passert(serial_set_databits(&serial, 7) == 0);
    passert(serial_get_databits(&serial, &databits) == 0);
    passert(databits == 7);
    passert(serial_set_parity(&serial, PARITY_ODD) == 0);
    passert(serial_get_parity(&serial, &parity) == 0);
    passert(parity == PARITY_ODD);
    passert(serial_set_stopbits(&serial, 2) == 0);
    passert(serial_get_stopbits(&serial, &stopbits) == 0);
    passert(stopbits == 2);
    passert(serial_set_xonxoff(&serial, true) == 0);
    passert(serial_get_xonxoff(&serial, &xonxoff) == 0);
    passert(xonxoff == true);
    #if 0
    passert(serial_set_rtscts(&serial, true) == 0);
    passert(serial_get_rtscts(&serial, &rtscts) == 0);
    passert(rtscts == true);
    #endif
    /* Test serial port may not support rtscts */

    passert(serial_close(&serial) == 0);
}
コード例 #2
0
ファイル: serial.c プロジェクト: theepot/esc64
int serial_open(serial_device* const dev, const char* const path, int char_size,
	int in_baudrate, int out_baudrate, int stopbits, int enable_parity,
	int odd_parity, int sync, int exit_on_error, int print_on_error)
{
	dev->exit_on_error = exit_on_error;
	dev->print_on_error = print_on_error;
	dev->path = path;
	
	dev->tios.c_cflag = 0;
	dev->tios.c_iflag = 0;

	if(!serial_set_character_size(dev, char_size))
		return 0;
	if(!serial_set_in_baudrate(dev, in_baudrate))
		return 0;
	if(!serial_set_out_baudrate(dev, out_baudrate))
		return 0;
	if(!serial_set_stopbits(dev, stopbits))
		return 0;
	serial_set_parity(dev, enable_parity, odd_parity);
	
	dev->tios.c_cflag |= CLOCAL | CREAD;
	dev->tios.c_oflag = 0;
	dev->tios.c_lflag = 0;
	
	dev->fd = open(path, O_RDWR | O_NOCTTY | (sync ? O_SYNC: 0));
	if(dev->fd < 0)
	{
		ERROR(dev, "opening failed: %s", strerror(errno)); return 0;
	}

	tcflush(dev->fd, TCIFLUSH);

	if(!serial_flush_settings(dev))
		return 0;

	return 1;
}
コード例 #3
0
static int lua_serial_newindex(lua_State *L) {
    serial_t *serial;
    const char *field;

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

    if (!lua_isstring(L, 2))
        return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: unknown property");

    field = lua_tostring(L, 2);

    if (strcmp(field, "fd") == 0)
        return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: immutable property");
    else if (strcmp(field, "baudrate") == 0) {
        uint32_t baudrate;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        baudrate = lua_tounsigned(L, 3);

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

        return 0;
    } else if (strcmp(field, "databits") == 0) {
        int databits;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        databits = lua_tounsigned(L, 3);

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

        return 0;
    } else if (strcmp(field, "parity") == 0) {
        const char *s;
        serial_parity_t parity;
        int ret;

        lua_serial_checktype(L, 3, LUA_TSTRING);
        s = lua_tostring(L, 3);

        if (strcmp(s, "none") == 0)
            parity = PARITY_NONE;
        else if (strcmp(s, "odd") == 0)
            parity = PARITY_ODD;
        else if (strcmp(s, "even") == 0)
            parity = PARITY_EVEN;
        else
            return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: invalid parity, should be 'none', 'even', or 'odd'");

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

        return 0;
    } else if (strcmp(field, "stopbits") == 0) {
        int stopbits;
        int ret;

        lua_serial_checktype(L, 3, LUA_TNUMBER);
        stopbits = lua_tounsigned(L, 3);

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

        return 0;
    } else if (strcmp(field, "xonxoff") == 0) {
        bool xonxoff;
        int ret;

        lua_serial_checktype(L, 3, LUA_TBOOLEAN);
        xonxoff = lua_toboolean(L, 3);

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

        return 0;
    } else if (strcmp(field, "rtscts") == 0) {
        bool rtscts;
        int ret;

        lua_serial_checktype(L, 3, LUA_TBOOLEAN);
        rtscts = lua_toboolean(L, 3);

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

        return 0;
    }

    return lua_serial_error(L, SERIAL_ERROR_ARG, 0, "Error: unknown property");
}