Instrument control package: Difference between revisions
Jump to navigation
Jump to search
mNo edit summary |
(→Serial) |
||
Line 8: | Line 8: | ||
* If you do not have a Serial adapter then create a virtual port using: | * If you do not have a Serial adapter then create a virtual port using: | ||
<pre> | <pre> | ||
$ socat PTY,link=/dev/ttyS15 PTY,link=/dev/ttyS16 | |||
</pre> | </pre> | ||
Line 17: | Line 17: | ||
do not forget to change interface permissions if you want to use them with Octave/screen without root privileges. | do not forget to change interface permissions if you want to use them with Octave/screen without root privileges. | ||
=== Example === | |||
Here is a simple example to get started with the serial package. It tests the RxD and TxD lines. Make sure you have defined a loopback or connected the pins of your adapter. | |||
{{Code|Serial port example|<syntaxhighlight lang="octave" style="font-size:13px"> | |||
# Open default serial port ttyUSB0 in default configuration of 115200, 8-N-1 | |||
s0 = serial() | |||
# Opens serial port ttyUSB1 with baudrate of 115200 (config defaults to 8-N-1) | |||
s1 = serial("/dev/ttyUSB1", 115200) | |||
# Flush input and output buffers | |||
srl_flush(s1); | |||
# Blocking write call, currently only accepts strings | |||
srl_write(s1, "Hello world!") | |||
# Blocking read call, returns uint8 array of exactly 12 bytes read | |||
data = srl_read(s1, 12) | |||
# Convert uint8 array to string, | |||
char(data) | |||
</syntaxhighlight> | |||
}} | |||
Chaging some configurations is simple done by calling helper functions | |||
{{Code|Serial port example: helper functions|<syntaxhighlight lang="octave" style="font-size:13px"> | |||
srl_baudrate(s1, 9600) # Change baudrate | |||
srl_bytesize(s1, 5) # Change byte size (config becomes 5-N-1) | |||
srl_parity(s1, "E") # Changes parity checking (config becomes 5-E-1), | |||
# possible values [E]ven, [O]dd, [N]one. | |||
srl_stopbits(s1, 2) # Changes stop bits (config becomes 5-E-2), possible | |||
# values 1, 2. | |||
</syntaxhighlight> | |||
}} | |||
Most properties can be set at opening time | |||
{{Code|Serial port example: constructor call|<syntaxhighlight lang="octave" style="font-size:13px"> | |||
s2 = serial("/dev/ttyS0", 9600, 6, "odd", 2) # Opens serial port ttyS0 in | |||
# 9600, 6-O-2 configuration | |||
</syntaxhighlight> | |||
}} | |||
Do not forget to close the ports when you are done! | |||
{{Code||<syntaxhighlight lang="octave" style="font-size:13px"> | |||
srl_close(s0) # Closes and releases file descriptor | |||
srl_close(s1) # Closes and releases file descriptor | |||
srl_close(s2) # Closes and releases file descriptor | |||
</syntaxhighlight> | |||
}} | |||
== Parallel == | == Parallel == |