vsergeev / lua-periphery

A Lua library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
MIT License
182 stars 38 forks source link

error object is not a string #12

Closed dannyrz closed 8 years ago

dannyrz commented 8 years ago

local Serial = require('periphery').Serial if Serial==nil then print("load sdk periphery faild."); os.exit(); end local serial = Serial("/dev/ttyUSB0", 9600)

[Finished in 0.0s with exit code 1]/usr/local/bin/lua: (error object is not a string)

vsergeev commented 8 years ago

lua-periphery throws a table object on an error, which can be formatted into a string with tostring(). Unfortunately, Lua 5.1 does not do this automatically (but LuaJIT, Lua 5.2, Lua 5.3 all do).

Try:

local serial, err = pcall(Serial, "/dev/ttyUSB0", 9600)
if not serial then
    print(err)
    os.exit()
end

Also see issue #4.

I'll add this to the documentation since it's come up a few times now.