Serial Port Class

From SolarStrike wiki
Jump to: navigation, search

isConnected[edit]

boolean serialPort:isConnected

Returns a boolean describing whether or not the serial port is connected. Unless you've closed the port, this should probably be true.


Example:

if( not port:isConnected() ) then
	print("The port has been closed.");
end


read[edit]

string,boolean serialPort:read() string,boolean 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 should 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


write[edit]

boolean serialPort:write(string data)

Writes data to a serial port. Returns true if the function succeeds, otherwise it returns false.


Example:

if( not port:write("Hello World") ) then
	print("Failed to write data");
end


close[edit]

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:

port:close();
port = nil; -- Set it to nil to delete the object so that it can be removed from memory