Module serial
USB serial device interface
Functions
add_handler (args) | add a new serial device handler |
send (dev, data) | send a message to a serial device |
Tables
cc | CC (special character) constants |
iflag | iflag constants |
oflag | oflag constants |
cflag | cflag constants |
lflag | lflag constants |
speed | speed constants |
Functions
- add_handler (args)
-
add a new serial device handler
Parameters:
- args
- id unique identifier for the handler
- match function(attrs) returning true if the handler should accept the device with the given attributes, otherwise false
- configure function(term) receiving the device's initial terminal settings and returning the new terminal settings. The callback should modify the input table using bitwise operations and then return the input table
- add function(id, name, dev) called when a new device for this handler has been connected and initialized
- remove function(id) called when a connected device for this handler has been disconnected
- event function(id, data) called when a message is received from a connected device for this handler. The data argument will be no more than 255 bytes in length. Messages longer than 255 bytes will be spread out over multiple event callbacks. It's possible for multiple messages shorter than 255 bytes to appear in a single event callback.
Usage:
friends = {} serial.add_handler({ id = "example_id", -- can be number or string match = function(attrs) local is_match = ( (attrs["vendor"] == "FTDI") and (attrs["model"] == "FT232R") and (attrs["serial"] == "ABC123") and (attrs["interface"] == "00") ) return is_match end, configure = function(term) -- set baud rate. term.ispeed = term.ispeed | serial.speed.B115200 term.ospeed = term.ospeed | serial.speed.B115200 -- enable some flags. term.iflag = term.iflag | serial.iflag.INCLR term.oflag = term.oflag | serial.oflag.ONLCR term.cflag = term.cflag | serial.cflag.CS8 -- disable some flags. term.lflag = term.lflag & (~serial.lflag.ECHO) -- return the copied and modified table. return term end, add = function(id, name, dev) print("saying hi...") serial.send(dev, "hello " .. name .. "!") tab.insert(friends, id, dev) end, remove = function(id) print("it's too late to say goodbye.") tab.remove(friends, id) end, event = function(id, data) print(friends[id] .. " says:", data) end })
- args
- send (dev, data)
-
send a message to a serial device
Parameters:
- dev opaque device pointer
- data string message to send
Tables
- cc
-
CC (special character) constants
Fields:
- VINTR INTR character
- VQUIT QUIT character
- VERASE ERASE character
- VKILL KILL character
- VEOF EOF character
- VTIME TIME value
- VMIN MIN value
- VSTART START character
- VSTOP STOP character
- VSUSP SUSP character
- VEOL EOL character
- iflag
-
iflag constants
Fields:
- IGNBREAK Ignore break condition.
- BRKINT Signal interrupt on break.
- IGNPAR Ignore characters with parity errors.
- PARMRK Mark parity and framing errors.
- INPCK Enable input parity check.
- ISTRIP Strip 8th bit off characters.
- INCLR Map NL to CR on input.
- IGNCR Ignore CR.
- ICRNL Map CT to NL on input.
- IUCLC Map uppercase characters to lowercase on input (not in POSIX).
- IXON Enable start/stop output control.
- IXANY Enable any character to restart output.
- IXOFF Enable start/stop input control.
- IMAXBEL Enable start/stop input control.
- IUTF8 Input is UTF8 (not in POSIX).
- oflag
-
oflag constants
Fields:
- OPOST Post-process output.
- OLCUC Map lowercase characters to uppercase on output. (not in POSIX).
- ONLCR Map NL to CR-NL on output.
- OCRNL Map CR to NL on output.
- ONOCR No CR output at column 0.
- ONLRET NL performs CR function.
- OFILL Use fill characters for delay.
- OFDEL Fill is DEL.
- VTDLY Select vertical-tab delays:
- VT0 Vertical-tab delay type 0.
- VT1 Vertical-tab delay type 1.
- cflag
-
cflag constants
Fields:
- CSIZE Character size
- CS5 5-bits per character / symbol.
- CS6 6-bits per character / symbol.
- CS7 7-bits per character / symbol.
- CS8 8-bits per character / symbol.
- CSTOPB Send two stop bits, else one.
- CREAD Enable receiver.
- PARENB Parity enable.
- PARODD Odd parity, else even.
- HUPCL Hang up on last close.
- CLOCAL Ignore modem status lines.
- lflag
-
lflag constants
Fields:
- ISIG Enable signals.
- ICANON Canonical input (erase and kill processing).
- ECHO Enable echo.
- ECHOE Echo erase character as error-correcting backspace.
- ECHOK Echo KILL.
- ECHONL Echo NL.
- NOFLSH Disable flush after interrupt or quit.
- TOSTOP Send SIGTTOU for background output.
- IEXTEN Enable implementation-defined input processing.
- speed
-
speed constants
Fields:
- B50 50 baud
- B75 75 baud
- B110 110 baud
- B134 134 baud
- B150 150 baud
- B200 200 baud
- B300 300 baud
- B600 600 baud
- B1200 1200 baud
- B1800 1800 baud
- B2400 2400 baud
- B4800 4800 baud
- B9600 9600 baud
- B19200 19200 baud
- B38400 38400 baud
- B57600 57600 baud
- B115200 115200 baud
- B230400 230400 baud
- B460800 460800 baud
- B500000 500000 baud
- B576000 576000 baud
- B921600 921600 baud
- B1000000 1000000 baud
- B1152000 1152000 baud
- B1500000 1500000 baud
- B2000000 2000000 baud
- B2500000 2500000 baud
- B3000000 3000000 baud
- B3500000 3500000 baud
- B4000000 4000000 baud