NOTE: The serial object has been deprecated and may not appear in newer versions of the instrument-control toolbox. Instead new code should use the serialport object. |
The serial port can be opened using the serial function:
s = serial("/dev/ttyUSB1", 115200)
The first parameter is the device name and is OS specific. The second parameter is the baudrate.
A list of available serial ports can be retrieved using the function:
seriallist
After creating the interface object, properties of the device can be set or retrieved using get or set functions or as property access.
s = serial("/dev/ttyUSB1", 115200) br = get(s, "baudrate") # gets the baudrate br = s.baudrate # also gets the baudrate set(s, "baudrate", 9600) # set the baudrate s.baudrate = 9600 # also sets the baudrate
The device can be written and read from using fread, fwrite and srl_read and slr_write functions.
srl_write(s, "hello world") # write hello world fprintf(s, "hello again") val = srl_read(s, 10) # attempt to read val = fread(s, 10)
The device can be closed using fclose or srl_close.
fclose(s)
The recommended method of accessing serial ports is through the serialport object.
The serial port can be opened using the serialport function:
s = serialport("/dev/ttyUSB1", 115200)
The first parameter is the device name and is OS specific. The second parameter is the baudrate.
A list of available serial ports can be retrieved using the function:
serialportlist
After creating the interface object, properties of the device can be set or retrieved using get or set functions or as property access.
s = serialport("/dev/ttyUSB1", 115200) br = get(s, "BaudRate") # gets the baudrate br = s.BaudRate # also gets the baudrate set(s, "BaudRate", 9600) # set the baudrate s.BaudRate = 9600 # also sets the baudrate
The device can be written and read from using read and write functions.
write(s, "hello world") # write hello world val = read(s, 10)
The device can be closed by clearing the serialport object.
clear s