This repository has been archived by the owner on Jan 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
SwiftSerial.swift
526 lines (452 loc) · 15.3 KB
/
SwiftSerial.swift
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
import Foundation
#if os(Linux)
public enum BaudRate {
case baud0
case baud50
case baud75
case baud110
case baud134
case baud150
case baud200
case baud300
case baud600
case baud1200
case baud1800
case baud2400
case baud4800
case baud9600
case baud19200
case baud38400
case baud57600
case baud115200
case baud230400
case baud460800
case baud500000
case baud576000
case baud921600
case baud1000000
case baud1152000
case baud1500000
case baud2000000
case baud2500000
case baud3500000
case baud4000000
var speedValue: speed_t {
switch self {
case .baud0:
return speed_t(B0)
case .baud50:
return speed_t(B50)
case .baud75:
return speed_t(B75)
case .baud110:
return speed_t(B110)
case .baud134:
return speed_t(B134)
case .baud150:
return speed_t(B150)
case .baud200:
return speed_t(B200)
case .baud300:
return speed_t(B300)
case .baud600:
return speed_t(B600)
case .baud1200:
return speed_t(B1200)
case .baud1800:
return speed_t(B1800)
case .baud2400:
return speed_t(B2400)
case .baud4800:
return speed_t(B4800)
case .baud9600:
return speed_t(B9600)
case .baud19200:
return speed_t(B19200)
case .baud38400:
return speed_t(B38400)
case .baud57600:
return speed_t(B57600)
case .baud115200:
return speed_t(B115200)
case .baud230400:
return speed_t(B230400)
case .baud460800:
return speed_t(B460800)
case .baud500000:
return speed_t(B500000)
case .baud576000:
return speed_t(B576000)
case .baud921600:
return speed_t(B921600)
case .baud1000000:
return speed_t(B1000000)
case .baud1152000:
return speed_t(B1152000)
case .baud1500000:
return speed_t(B1500000)
case .baud2000000:
return speed_t(B2000000)
case .baud2500000:
return speed_t(B2500000)
case .baud3500000:
return speed_t(B3500000)
case .baud4000000:
return speed_t(B4000000)
}
}
}
#elseif os(OSX)
public enum BaudRate {
case baud0
case baud50
case baud75
case baud110
case baud134
case baud150
case baud200
case baud300
case baud600
case baud1200
case baud1800
case baud2400
case baud4800
case baud9600
case baud19200
case baud38400
case baud57600
case baud115200
case baud230400
var speedValue: speed_t {
switch self {
case .baud0:
return speed_t(B0)
case .baud50:
return speed_t(B50)
case .baud75:
return speed_t(B75)
case .baud110:
return speed_t(B110)
case .baud134:
return speed_t(B134)
case .baud150:
return speed_t(B150)
case .baud200:
return speed_t(B200)
case .baud300:
return speed_t(B300)
case .baud600:
return speed_t(B600)
case .baud1200:
return speed_t(B1200)
case .baud1800:
return speed_t(B1800)
case .baud2400:
return speed_t(B2400)
case .baud4800:
return speed_t(B4800)
case .baud9600:
return speed_t(B9600)
case .baud19200:
return speed_t(B19200)
case .baud38400:
return speed_t(B38400)
case .baud57600:
return speed_t(B57600)
case .baud115200:
return speed_t(B115200)
case .baud230400:
return speed_t(B230400)
}
}
}
#endif
public enum DataBitsSize {
case bits5
case bits6
case bits7
case bits8
var flagValue: tcflag_t {
switch self {
case .bits5:
return tcflag_t(CS5)
case .bits6:
return tcflag_t(CS6)
case .bits7:
return tcflag_t(CS7)
case .bits8:
return tcflag_t(CS8)
}
}
}
public enum ParityType {
case none
case even
case odd
var parityValue: tcflag_t {
switch self {
case .none:
return 0
case .even:
return tcflag_t(PARENB)
case .odd:
return tcflag_t(PARENB | PARODD)
}
}
}
public enum PortError: Int32, Error {
case failedToOpen = -1 // refer to open()
case invalidPath
case mustReceiveOrTransmit
case mustBeOpen
case stringsMustBeUTF8
case unableToConvertByteToCharacter
case deviceNotConnected
}
public class SerialPort {
var path: String
var fileDescriptor: Int32?
public init(path: String) {
self.path = path
}
public func openPort() throws {
try openPort(toReceive: true, andTransmit: true)
}
public func openPort(toReceive receive: Bool, andTransmit transmit: Bool) throws {
guard !path.isEmpty else {
throw PortError.invalidPath
}
guard receive || transmit else {
throw PortError.mustReceiveOrTransmit
}
var readWriteParam : Int32
if receive && transmit {
readWriteParam = O_RDWR
} else if receive {
readWriteParam = O_RDONLY
} else if transmit {
readWriteParam = O_WRONLY
} else {
fatalError()
}
#if os(Linux)
fileDescriptor = open(path, readWriteParam | O_NOCTTY)
#elseif os(OSX)
fileDescriptor = open(path, readWriteParam | O_NOCTTY | O_EXLOCK)
#endif
// Throw error if open() failed
if fileDescriptor == PortError.failedToOpen.rawValue {
throw PortError.failedToOpen
}
}
public func setSettings(receiveRate: BaudRate,
transmitRate: BaudRate,
minimumBytesToRead: Int,
timeout: Int = 0, /* 0 means wait indefinitely */
parityType: ParityType = .none,
sendTwoStopBits: Bool = false, /* 1 stop bit is the default */
dataBitsSize: DataBitsSize = .bits8,
useHardwareFlowControl: Bool = false,
useSoftwareFlowControl: Bool = false,
processOutput: Bool = false) {
guard let fileDescriptor = fileDescriptor else {
return
}
// Set up the control structure
var settings = termios()
// Get options structure for the port
tcgetattr(fileDescriptor, &settings)
// Set baud rates
cfsetispeed(&settings, receiveRate.speedValue)
cfsetospeed(&settings, transmitRate.speedValue)
// Enable parity (even/odd) if needed
settings.c_cflag |= parityType.parityValue
// Set stop bit flag
if sendTwoStopBits {
settings.c_cflag |= tcflag_t(CSTOPB)
} else {
settings.c_cflag &= ~tcflag_t(CSTOPB)
}
// Set data bits size flag
settings.c_cflag &= ~tcflag_t(CSIZE)
settings.c_cflag |= dataBitsSize.flagValue
//Disable input mapping of CR to NL, mapping of NL into CR, and ignoring CR
settings.c_iflag &= ~tcflag_t(ICRNL | INLCR | IGNCR)
// Set hardware flow control flag
#if os(Linux)
if useHardwareFlowControl {
settings.c_cflag |= tcflag_t(CRTSCTS)
} else {
settings.c_cflag &= ~tcflag_t(CRTSCTS)
}
#elseif os(OSX)
if useHardwareFlowControl {
settings.c_cflag |= tcflag_t(CRTS_IFLOW)
settings.c_cflag |= tcflag_t(CCTS_OFLOW)
} else {
settings.c_cflag &= ~tcflag_t(CRTS_IFLOW)
settings.c_cflag &= ~tcflag_t(CCTS_OFLOW)
}
#endif
// Set software flow control flags
let softwareFlowControlFlags = tcflag_t(IXON | IXOFF | IXANY)
if useSoftwareFlowControl {
settings.c_iflag |= softwareFlowControlFlags
} else {
settings.c_iflag &= ~softwareFlowControlFlags
}
// Turn on the receiver of the serial port, and ignore modem control lines
settings.c_cflag |= tcflag_t(CREAD | CLOCAL)
// Turn off canonical mode
settings.c_lflag &= ~tcflag_t(ICANON | ECHO | ECHOE | ISIG)
// Set output processing flag
if processOutput {
settings.c_oflag |= tcflag_t(OPOST)
} else {
settings.c_oflag &= ~tcflag_t(OPOST)
}
//Special characters
//We do this as c_cc is a C-fixed array which is imported as a tuple in Swift.
//To avoid hardcoding the VMIN or VTIME value to access the tuple value, we use the typealias instead
#if os(Linux)
typealias specialCharactersTuple = (VINTR: cc_t, VQUIT: cc_t, VERASE: cc_t, VKILL: cc_t, VEOF: cc_t, VTIME: cc_t, VMIN: cc_t, VSWTC: cc_t, VSTART: cc_t, VSTOP: cc_t, VSUSP: cc_t, VEOL: cc_t, VREPRINT: cc_t, VDISCARD: cc_t, VWERASE: cc_t, VLNEXT: cc_t, VEOL2: cc_t, spare1: cc_t, spare2: cc_t, spare3: cc_t, spare4: cc_t, spare5: cc_t, spare6: cc_t, spare7: cc_t, spare8: cc_t, spare9: cc_t, spare10: cc_t, spare11: cc_t, spare12: cc_t, spare13: cc_t, spare14: cc_t, spare15: cc_t)
var specialCharacters: specialCharactersTuple = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) // NCCS = 32
#elseif os(OSX)
typealias specialCharactersTuple = (VEOF: cc_t, VEOL: cc_t, VEOL2: cc_t, VERASE: cc_t, VWERASE: cc_t, VKILL: cc_t, VREPRINT: cc_t, spare1: cc_t, VINTR: cc_t, VQUIT: cc_t, VSUSP: cc_t, VDSUSP: cc_t, VSTART: cc_t, VSTOP: cc_t, VLNEXT: cc_t, VDISCARD: cc_t, VMIN: cc_t, VTIME: cc_t, VSTATUS: cc_t, spare: cc_t)
var specialCharacters: specialCharactersTuple = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) // NCCS = 20
#endif
specialCharacters.VMIN = cc_t(minimumBytesToRead)
specialCharacters.VTIME = cc_t(timeout)
settings.c_cc = specialCharacters
// Commit settings
tcsetattr(fileDescriptor, TCSANOW, &settings)
}
public func closePort() {
if let fileDescriptor = fileDescriptor {
close(fileDescriptor)
}
fileDescriptor = nil
}
}
// MARK: Receiving
extension SerialPort {
public func readBytes(into buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int {
guard let fileDescriptor = fileDescriptor else {
throw PortError.mustBeOpen
}
var s: stat = stat()
fstat(fileDescriptor, &s)
if s.st_nlink != 1 {
throw PortError.deviceNotConnected
}
let bytesRead = read(fileDescriptor, buffer, size)
return bytesRead
}
public func readData(ofLength length: Int) throws -> Data {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: length)
defer {
buffer.deallocate()
}
let bytesRead = try readBytes(into: buffer, size: length)
var data : Data
if bytesRead > 0 {
data = Data(bytes: buffer, count: bytesRead)
} else {
//This is to avoid the case where bytesRead can be negative causing problems allocating the Data buffer
data = Data(bytes: buffer, count: 0)
}
return data
}
public func readString(ofLength length: Int) throws -> String {
var remainingBytesToRead = length
var result = ""
while remainingBytesToRead > 0 {
let data = try readData(ofLength: remainingBytesToRead)
if let string = String(data: data, encoding: String.Encoding.utf8) {
result += string
remainingBytesToRead -= data.count
} else {
return result
}
}
return result
}
public func readUntilChar(_ terminator: CChar) throws -> String {
var data = Data()
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
defer {
buffer.deallocate()
}
while true {
let bytesRead = try readBytes(into: buffer, size: 1)
if bytesRead > 0 {
if ( buffer[0] > 127) {
throw PortError.unableToConvertByteToCharacter
}
let character = CChar(buffer[0])
if character == terminator {
break
} else {
data.append(buffer, count: 1)
}
}
}
if let string = String(data: data, encoding: String.Encoding.utf8) {
return string
} else {
throw PortError.stringsMustBeUTF8
}
}
public func readLine() throws -> String {
let newlineChar = CChar(10) // Newline/Line feed character `\n` is 10
return try readUntilChar(newlineChar)
}
public func readByte() throws -> UInt8 {
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 1)
defer {
buffer.deallocate()
}
while true {
let bytesRead = try readBytes(into: buffer, size: 1)
if bytesRead > 0 {
return buffer[0]
}
}
}
public func readChar() throws -> UnicodeScalar {
let byteRead = try readByte()
let character = UnicodeScalar(byteRead)
return character
}
}
// MARK: Transmitting
extension SerialPort {
public func writeBytes(from buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int {
guard let fileDescriptor = fileDescriptor else {
throw PortError.mustBeOpen
}
let bytesWritten = write(fileDescriptor, buffer, size)
return bytesWritten
}
public func writeData(_ data: Data) throws -> Int {
let size = data.count
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
defer {
buffer.deallocate()
}
data.copyBytes(to: buffer, count: size)
let bytesWritten = try writeBytes(from: buffer, size: size)
return bytesWritten
}
public func writeString(_ string: String) throws -> Int {
guard let data = string.data(using: String.Encoding.utf8) else {
throw PortError.stringsMustBeUTF8
}
return try writeData(data)
}
public func writeChar(_ character: UnicodeScalar) throws -> Int{
let stringEquiv = String(character)
let bytesWritten = try writeString(stringEquiv)
return bytesWritten
}
}