1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"949">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #a91b0e}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #0019b8}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #606060}
15 span
.s1
{color: #0019b8}
16 span
.s2
{color: #000000}
17 span
.s3
{color: #606060}
18 span
.s4
{color: #a91b0e}
19 span
.Apple-tab-span
{white-space:pre
}
23 <p class=
"p1">// =====================================================================
</p>
24 <p class=
"p1">// SerialPort
</p>
25 <p class=
"p1">//
<span class=
"Apple-converted-space"> </span>serial port interface
</p>
26 <p class=
"p2"><br></p>
27 <p class=
"p1">// device list
</p>
28 <p class=
"p1"><span class=
"s1">SerialPort
</span><span class=
"s2">.listDevices;
<span class=
"Apple-tab-span"> </span></span>// prints to postbuffer
</p>
29 <p class=
"p1"><span class=
"s1">SerialPort
</span><span class=
"s2">.devices;
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// returns array
</p>
30 <p class=
"p2"><br></p>
31 <p class=
"p1">// change device selection
</p>
32 <p class=
"p3"><span class=
"s1">SerialPort
</span>.devicePattern =
<span class=
"s3">"/dev/ttyUSB*"</span>;
<span class=
"s4">// linux usb serial
</span></p>
33 <p class=
"p4">SerialPort
<span class=
"s2">.devices;
</span></p>
34 <p class=
"p2"><br></p>
35 <p class=
"p3"><span class=
"s1">SerialPort
</span>.devicePattern =
<span class=
"s1">nil
</span>;
</p>
36 <p class=
"p4">SerialPort
<span class=
"s2">.devices;
</span></p>
37 <p class=
"p2"><br></p>
38 <p class=
"p1">// opening the port
</p>
39 <p class=
"p2"><br></p>
40 <p class=
"p1">// instance creation arguments
</p>
42 <p class=
"p1">//
<span class=
"Apple-converted-space"> </span>port
<span class=
"Apple-converted-space"> </span>device path or index
</p>
43 <p class=
"p1">//
<span class=
"Apple-tab-span"> </span>baudrate
<span class=
"Apple-converted-space"> </span>baudrate [
4800.
.230400]
</p>
44 <p class=
"p1">//
<span class=
"Apple-converted-space"> </span>databits
<span class=
"Apple-converted-space"> </span>5 |
6 |
7 |
8</p>
45 <p class=
"p1">//
<span class=
"Apple-converted-space"> </span>stopbits
<span class=
"Apple-converted-space"> </span>true | false
</p>
46 <p class=
"p1">//
<span class=
"Apple-converted-space"> </span>parity
<span class=
"Apple-converted-space"> </span>nil | 'even' | 'odd'
</p>
47 <p class=
"p1">//
<span class=
"Apple-converted-space"> </span>crtscts
<span class=
"Apple-converted-space"> </span>hardware flow control (true | false)
</p>
48 <p class=
"p1">//
<span class=
"Apple-converted-space"> </span>xonxoff
<span class=
"Apple-converted-space"> </span>software flow control (true | false)
</p>
49 <p class=
"p1">//
<span class=
"Apple-converted-space"> </span>exclusive
<span class=
"Apple-converted-space"> </span>open the device exclusively (true | false)
</p>
50 <p class=
"p2"><br></p>
52 <p class=
"p4"><span class=
"s2">p =
</span>SerialPort
<span class=
"s2">(
</span></p>
53 <p class=
"p5"><span class=
"s2"><span class=
"Apple-tab-span"> </span></span>"/dev/tty.usbserial-181"<span class=
"s2">,
</span></p>
54 <p class=
"p3"><span class=
"Apple-tab-span"> </span>baudrate:
9600,
</p>
55 <p class=
"p3"><span class=
"Apple-tab-span"> </span>crtscts:
<span class=
"s1">true
</span>);
</p>
57 <p class=
"p2"><br></p>
58 <p class=
"p1">// read a byte from the device
</p>
59 <p class=
"p2"><br></p>
60 <p class=
"p1"><span class=
"s2">p.next;
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// doesn't block
</p>
61 <p class=
"p1"><span class=
"s2">p.read;
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// may suspend thisThread
</p>
62 <p class=
"p2"><br></p>
63 <p class=
"p1">// write a byte to the device
</p>
64 <p class=
"p2"><br></p>
65 <p class=
"p1"><span class=
"s2">p.put(
42);
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// may suspend thisThread
</p>
66 <p class=
"p2"><br></p>
67 <p class=
"p1">// write multiple bytes to the device
</p>
68 <p class=
"p1">// collection may be Int8Array or String
</p>
69 <p class=
"p2"><br></p>
70 <p class=
"p5"><span class=
"s2">p.putAll(
</span>"whaddayawant"<span class=
"s2">);
</span></p>
71 <p class=
"p3">p.putAll(
<span class=
"s1">Int8Array
</span>[
13,
10]);
</p>
73 <p class=
"p2"><br></p>
74 <p class=
"p1">// you can set an function which will be evaluated if the port gets closed (maybe unexpectedly so, due to hardware failure or accidental disconnection). This allows you to for example to make a backup solution and activate it (like using fake input data for your algorithm, or trying to reopen the device). By default it will post a message to the post window.
</p>
75 <p class=
"p2"><br></p>
76 <p class=
"p3">p.doneAction = {
"my serial port got closed".postln; }
</p>
77 <p class=
"p2"><br></p>
79 <p class=
"p2"><br></p>
80 <p class=
"p1">// close the port
</p>
81 <p class=
"p2"><br></p>
82 <p class=
"p3">p.close;
</p>
83 <p class=
"p2"><br></p>
84 <p class=
"p1">// close all ports
</p>
85 <p class=
"p2"><br></p>
86 <p class=
"p3"><span class=
"s1">SerialPort
</span>.closeAll;
</p>
88 <p class=
"p2"><br></p>
89 <p class=
"p1">// EOF
</p>