Example #1
0
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);
}
Example #2
0
static int lua_serial_index(lua_State *L) {
    serial_t *serial;
    const char *field;

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

    field = lua_tostring(L, 2);

    /* Look up method in metatable */
    lua_getmetatable(L, 1);
    lua_getfield(L, -1, field);
    if (!lua_isnil(L, -1))
        return 1;

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

    if (strcmp(field, "fd") == 0) {
        lua_pushinteger(L, serial_fd(serial));
        return 1;
    } else if (strcmp(field, "baudrate") == 0) {
        uint32_t baudrate;
        int ret;

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

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

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

        lua_pushunsigned(L, databits);
        return 1;
    } else if (strcmp(field, "parity") == 0) {
        serial_parity_t parity;
        int ret;

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

        switch (parity) {
            case PARITY_NONE: lua_pushstring(L, "none"); break;
            case PARITY_ODD: lua_pushstring(L, "odd"); break;
            case PARITY_EVEN: lua_pushstring(L, "even"); break;
            default: lua_pushstring(L, "unknown"); break;
        }
        return 1;
    } else if (strcmp(field, "stopbits") == 0) {
        int stopbits;
        int ret;

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

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

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

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

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

        lua_pushboolean(L, rtscts);
        return 1;
    }

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