Serial Port Class
#serialport:isConnected
boolean serialport:isConnected()
Returns a boolean describing whether or not the serial port is connected.
Unless you've closed the port or some error occured, this should probably be
true.
Example:
local myPort,errMsg = serial.open("COM7"); -- Open COM7 with a default baud rate of 9600
if( not myPort ) then
printf("Failed to open COM7: %s\n", errMsg);
end
-- ...some time later...
if( not myPort:isConnected() ) then
print("The port has been closed.");
end
#serialport:read
string data, boolean more serialport:read()
string data, boolean
more serialport:read(number length)
Reads queued data out of a serial port. If 'length' is supplied, this function will only read up to
that many bytes. If 'length' is not supplied, it will read up to the maximum buffer size
(10240) in bytes.
This function returns two variables: a string containing the data read and
a boolean describing whether or not there is more data left in the queue.
If this function fails, it will return nil.
Example:
local data,more = myPort:read(); -- Read as much as our buffer can hold (10240)
-- or --
repeat -- Keep reading until there's no more left in the queue
data,more = myPort:read(4); -- Read 4 bytes at a time
until not more
#serialport:write
boolean serialport:write(string data)
Writes data to a serial port. Returns true if the function succeeds,
otherwise it returns false.
Example:
if( not myPort:write("Hello World") ) then
print("Failed to write data");
end
#serialport:close
serialport:close()
Closes a serial port. Does not accept any parameters nor does it return anything. A serial port
becomes useless after being closed and you must re-call serial.open()
if you wish to re-open it.
Example:
myPort:close();
myPort = nil; -- Set it to nil to delete the object so that it can be removed from memory
Page last updated at 2018-09-25 20:48:29