File: SerialPort.sc

package info (click to toggle)
supercollider 1%3A3.10.0%2Brepack-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,496 kB
  • sloc: cpp: 283,513; lisp: 74,040; ansic: 72,252; sh: 23,016; python: 7,175; makefile: 1,087; perl: 766; java: 677; yacc: 314; lex: 175; ruby: 136; objc: 65; xml: 15
file content (213 lines) | stat: -rw-r--r-- 4,710 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
SerialPort {
	classvar <>devicePattern, allPorts;
	var dataptr, semaphore, <isOpen = false;

	var <>doneAction;

	*initClass {
		allPorts = Array[];
		ShutDown.add {
			this.closeAll;
		};
	}

	// device listing
	*devices {
		var regQueryResult, devices;
		var regexp;

		// Backward compatibility
		if(devicePattern.notNil) {
			^devicePattern.pathMatch;
		};

		if(thisProcess.platform.name == \windows) {
			// There are somewhere around a dozen ways you can get a list of serial port devices
			// on Windows. We here duplicate the method used in JSSC (Java Simple Serial Connector),
			// which is in turn used in the Arduino IDE. If it's good enough for Arduino, it's good
			// enough for us.

			// The registry key HKLM\HARDWARE\DEVICEMAP\SERIALCOMM can be queried using the below
			// "reg query" command. The next step is to parse its output, which looks like this
			// (note the use of 4 spaces rather than tabs):

			// HKEY_LOCAL_MACHINE\Hardware\DeviceMap\SerialComm
			//     \Device\Serial0    REG_SZ    COM3
			//     \Device\LSIModem5    REG_SZ    COM4
			//     \Device\USBSER000    REG_SZ    COM5

			// The regexp "\\bREG_SZ {4}(.*)$" matches our desired port names (COM3/COM4/COM5).

			regQueryResult = "reg query HKLM\\HARDWARE\\DEVICEMAP\\SERIALCOMM".unixCmdGetStdOut;
			devices = [];
			regQueryResult.split($\n).do { |line|
				var match;
				match = line.findRegexp("\\bREG_SZ {4}(.*)$");
				if(match.notEmpty) {
					devices = devices.add(match[1][1]);
				};
			};
			^devices;
		} {
			// These regexps are also taken from the Arduino IDE:
			// https://github.com/arduino/Arduino/blob/ec2e9a642a085b32701cf81297ee7c1570177195/arduino-core/src/processing/app/SerialPortList.java#L48
			regexp = thisProcess.platform.name.switch(
				\linux,   "^(ttyS|ttyUSB|ttyACM|ttyAMA|rfcomm|ttyO)[0-9]{1,3}",
				\osx,     "^(tty|cu)\\..*"
			);
			devices = [];
			PathName("/dev/").files.do { |path|
				if(regexp.matchRegexp(path.fileName)) {
					devices = devices.add(path.fullPath);
				};
			};
			^devices;
		};
	}
	*listDevices {
		this.devices.do(_.postln);
	}

	*new {
		| port,
			baudrate(9600),
			databits(8),
			stopbit(true),
			parity(nil),
			crtscts(false),
			xonxoff(false)
			exclusive(false) |

		if (port.isNumber) {
			"Using integers to identify serial ports is deprecated. Please pass strings instead.".warn;
			port = this.devices[port] ?? {
				Error("invalid port index").throw;
			};
		}

		^super.new.prInit(
			port,
			exclusive,
			baudrate,
			databits,
			stopbit,
			( even: 1, odd: 2 ).at(parity) ? 0,
			crtscts,
			xonxoff
		)
	}

	prInit { | ... args |
		semaphore = Semaphore(0);
		if ( isOpen.not ){
			this.prOpen(*args);
			allPorts = allPorts.add(this);
			isOpen = true;
		}
	}

	close {
		if (this.isOpen) {
			this.prClose;
			isOpen = false;
		}
	}

	*closeAll {
		var ports = allPorts;
		allPorts = Array[];
		ports.do(_.close);
	}

	*cleanupAll {
		this.deprecated(thisMethod, SerialPort.findMethod('closeAll'));
		this.closeAll
	}

	// non-blocking read
	next {
		_SerialPort_Next
		^this.primitiveFailed
	}
	// blocking read
	read {
		var byte;
		while { (byte = this.next).isNil } {
			semaphore.wait;
		};
		^byte
	}
	// rx errors since last query
	rxErrors {
		_SerialPort_RXErrors
		^this.primitiveFailed;
	}

	// always blocks
	put { | byte, timeout=0.005 |
		if (timeout != 0.005) {
			"SerialPort:-put: the timeout argument is deprecated and will be removed in a future release".warn
		};

		if (isOpen) {
			^this.prPut(byte);
		}{
			"SerialPort not open".warn;
			^false;
		}
	}

	putAll { | bytes, timeout=0.005 |
		if (timeout != 0.005) {
			"SerialPort:-putAll: the timeout argument is deprecated and will be removed in a future release".warn
		};

		bytes.do { |byte|
			this.put(byte);
		}
	}

	// PRIMITIVE
	prOpen { | ... args |
		_SerialPort_Open
		^this.primitiveFailed
	}

	// Close the port; triggers doneAction
	prClose {
		_SerialPort_Close
		^this.primitiveFailed
	}

	prPut { | byte |
		_SerialPort_Put
		^this.primitiveFailed
	}

	// Deletes the C++ object for this port.
	prCleanup {
		_SerialPort_Cleanup
		^this.primitiveFailed
	}

	// callback from C++ read thread
	prDataAvailable {
		semaphore.signal;
	}

	// callback from C++ read thread when done
	prDoneAction {
		// Catches case where connection is closed unexpectedly.
		isOpen = false;

		// Ensure memory is freed even if doneAction throws.
		protect {
			this.doneAction.value
		} {
			// Needs to run after this callback; otherwise crash when
			// we try to wait for this thread to join, from itself.
			// Remove reference as last act, otherwise we risk GC running early.
			{ this.prCleanup; allPorts.remove(this) }.defer(0);
		}
	}
}