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.

local myPort,errMsg ="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.

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.

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 if you wish to re-open it.

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

Copyright 2024