SerialPort.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import Foundation
  2. public class SerialPort {
  3. var path: String
  4. var fileDescriptor: Int32?
  5. private var isOpen: Bool { fileDescriptor != nil }
  6. private var pollSource: DispatchSourceRead?
  7. private var readDataStream: AsyncStream<Data>?
  8. private var readBytesStream: AsyncStream<UInt8>?
  9. private var readLinesStream: AsyncStream<String>?
  10. public init(path: String) {
  11. self.path = path
  12. }
  13. public func openPort() throws {
  14. try openPort(toReceive: true, andTransmit: true)
  15. }
  16. public func openPort(toReceive receive: Bool, andTransmit transmit: Bool) throws {
  17. guard !path.isEmpty else { throw PortError.invalidPath }
  18. guard isOpen == false else { throw PortError.instanceAlreadyOpen }
  19. guard receive || transmit else { throw PortError.mustReceiveOrTransmit }
  20. var readWriteParam : Int32
  21. if receive && transmit {
  22. readWriteParam = O_RDWR
  23. } else if receive {
  24. readWriteParam = O_RDONLY
  25. } else if transmit {
  26. readWriteParam = O_WRONLY
  27. } else {
  28. fatalError()
  29. }
  30. #if os(Linux)
  31. fileDescriptor = open(path, readWriteParam | O_NOCTTY)
  32. #elseif os(OSX)
  33. fileDescriptor = open(path, readWriteParam | O_NOCTTY | O_EXLOCK)
  34. #endif
  35. // Throw error if open() failed
  36. if fileDescriptor == PortError.failedToOpen.rawValue {
  37. throw PortError.failedToOpen
  38. }
  39. guard
  40. receive,
  41. let fileDescriptor
  42. else { return }
  43. let pollSource = DispatchSource.makeReadSource(fileDescriptor: fileDescriptor, queue: .global(qos: .default))
  44. let stream = AsyncStream<Data> { continuation in
  45. pollSource.setEventHandler {
  46. let bufferSize = 1024
  47. let buffer = UnsafeMutableRawPointer
  48. .allocate(byteCount: bufferSize, alignment: 8)
  49. let bytesRead = read(fileDescriptor, buffer, bufferSize)
  50. guard bytesRead > 0 else { return }
  51. let bytes = Data(bytes: buffer, count: bytesRead)
  52. continuation.yield(bytes)
  53. }
  54. pollSource.setCancelHandler {
  55. continuation.finish()
  56. }
  57. }
  58. pollSource.resume()
  59. self.pollSource = pollSource
  60. self.readDataStream = stream
  61. }
  62. public func setSettings(
  63. receiveRate: BaudRate,
  64. transmitRate: BaudRate,
  65. minimumBytesToRead: Int,
  66. timeout: Int = 0, /* 0 means wait indefinitely */
  67. parityType: ParityType = .none,
  68. sendTwoStopBits: Bool = false, /* 1 stop bit is the default */
  69. dataBitsSize: DataBitsSize = .bits8,
  70. useHardwareFlowControl: Bool = false,
  71. useSoftwareFlowControl: Bool = false,
  72. processOutput: Bool = false
  73. ) throws {
  74. guard let fileDescriptor = fileDescriptor else {
  75. throw PortError.mustBeOpen
  76. }
  77. // Set up the control structure
  78. var settings = termios()
  79. // Get options structure for the port
  80. tcgetattr(fileDescriptor, &settings)
  81. // Set baud rates
  82. cfsetispeed(&settings, receiveRate.speedValue)
  83. cfsetospeed(&settings, transmitRate.speedValue)
  84. // Enable parity (even/odd) if needed
  85. settings.c_cflag |= parityType.parityValue
  86. // Set stop bit flag
  87. if sendTwoStopBits {
  88. settings.c_cflag |= tcflag_t(CSTOPB)
  89. } else {
  90. settings.c_cflag &= ~tcflag_t(CSTOPB)
  91. }
  92. // Set data bits size flag
  93. settings.c_cflag &= ~tcflag_t(CSIZE)
  94. settings.c_cflag |= dataBitsSize.flagValue
  95. //Disable input mapping of CR to NL, mapping of NL into CR, and ignoring CR
  96. settings.c_iflag &= ~tcflag_t(ICRNL | INLCR | IGNCR)
  97. // Set hardware flow control flag
  98. #if os(Linux)
  99. if useHardwareFlowControl {
  100. settings.c_cflag |= tcflag_t(CRTSCTS)
  101. } else {
  102. settings.c_cflag &= ~tcflag_t(CRTSCTS)
  103. }
  104. #elseif os(OSX)
  105. if useHardwareFlowControl {
  106. settings.c_cflag |= tcflag_t(CRTS_IFLOW)
  107. settings.c_cflag |= tcflag_t(CCTS_OFLOW)
  108. } else {
  109. settings.c_cflag &= ~tcflag_t(CRTS_IFLOW)
  110. settings.c_cflag &= ~tcflag_t(CCTS_OFLOW)
  111. }
  112. #endif
  113. // Set software flow control flags
  114. let softwareFlowControlFlags = tcflag_t(IXON | IXOFF | IXANY)
  115. if useSoftwareFlowControl {
  116. settings.c_iflag |= softwareFlowControlFlags
  117. } else {
  118. settings.c_iflag &= ~softwareFlowControlFlags
  119. }
  120. // Turn on the receiver of the serial port, and ignore modem control lines
  121. settings.c_cflag |= tcflag_t(CREAD | CLOCAL)
  122. // Turn off canonical mode
  123. settings.c_lflag &= ~tcflag_t(ICANON | ECHO | ECHOE | ISIG)
  124. // Set output processing flag
  125. if processOutput {
  126. settings.c_oflag |= tcflag_t(OPOST)
  127. } else {
  128. settings.c_oflag &= ~tcflag_t(OPOST)
  129. }
  130. //Special characters
  131. //We do this as c_cc is a C-fixed array which is imported as a tuple in Swift.
  132. //To avoid hardcoding the VMIN or VTIME value to access the tuple value, we use the typealias instead
  133. #if os(Linux)
  134. 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)
  135. 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
  136. #elseif os(OSX)
  137. 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)
  138. 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
  139. #endif
  140. specialCharacters.VMIN = cc_t(minimumBytesToRead)
  141. specialCharacters.VTIME = cc_t(timeout)
  142. settings.c_cc = specialCharacters
  143. // Commit settings
  144. tcsetattr(fileDescriptor, TCSANOW, &settings)
  145. }
  146. public func closePort() {
  147. pollSource?.cancel()
  148. pollSource = nil
  149. readDataStream = nil
  150. readBytesStream = nil
  151. readLinesStream = nil
  152. if let fileDescriptor = fileDescriptor {
  153. close(fileDescriptor)
  154. }
  155. fileDescriptor = nil
  156. }
  157. }
  158. // MARK: Receiving
  159. extension SerialPort {
  160. public func asyncData() throws -> AsyncStream<Data> {
  161. guard
  162. isOpen,
  163. let readDataStream
  164. else {
  165. throw PortError.mustBeOpen
  166. }
  167. return readDataStream
  168. }
  169. public func asyncBytes() throws -> AsyncStream<UInt8> {
  170. guard
  171. isOpen,
  172. let readDataStream
  173. else {
  174. throw PortError.mustBeOpen
  175. }
  176. if let existing = readBytesStream {
  177. return existing
  178. } else {
  179. let new = AsyncStream<UInt8> { continuation in
  180. Task {
  181. for try await data in readDataStream {
  182. for byte in data {
  183. continuation.yield(byte)
  184. }
  185. }
  186. continuation.finish()
  187. }
  188. }
  189. readBytesStream = new
  190. return new
  191. }
  192. }
  193. public func asyncLines() throws -> AsyncStream<String> {
  194. guard isOpen else { throw PortError.mustBeOpen }
  195. if let existing = readLinesStream {
  196. return existing
  197. } else {
  198. let byteStream = try asyncBytes()
  199. let new = AsyncStream<String> { continuation in
  200. Task {
  201. var accumulator = Data()
  202. for try await byte in byteStream {
  203. accumulator.append(byte)
  204. guard
  205. UnicodeScalar(byte) == "\n".unicodeScalars.first
  206. else { continue }
  207. defer { accumulator = Data() }
  208. guard
  209. let string = String(data: accumulator, encoding: .utf8)
  210. else {
  211. continuation.yield("Error: Non string data. Perhaps you wanted data or bytes output?")
  212. continue
  213. }
  214. continuation.yield(string)
  215. }
  216. continuation.finish()
  217. }
  218. }
  219. readLinesStream = new
  220. return new
  221. }
  222. }
  223. }
  224. // MARK: Transmitting
  225. extension SerialPort {
  226. public func writeBytes(from buffer: UnsafeMutablePointer<UInt8>, size: Int) throws -> Int {
  227. guard let fileDescriptor = fileDescriptor else {
  228. throw PortError.mustBeOpen
  229. }
  230. let bytesWritten = write(fileDescriptor, buffer, size)
  231. return bytesWritten
  232. }
  233. public func writeData(_ data: Data) throws -> Int {
  234. let size = data.count
  235. let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: size)
  236. defer {
  237. buffer.deallocate()
  238. }
  239. data.copyBytes(to: buffer, count: size)
  240. let bytesWritten = try writeBytes(from: buffer, size: size)
  241. return bytesWritten
  242. }
  243. public func writeString(_ string: String) throws -> Int {
  244. guard let data = string.data(using: String.Encoding.utf8) else {
  245. throw PortError.stringsMustBeUTF8
  246. }
  247. return try writeData(data)
  248. }
  249. public func writeChar(_ character: UnicodeScalar) throws -> Int{
  250. let stringEquiv = String(character)
  251. let bytesWritten = try writeString(stringEquiv)
  252. return bytesWritten
  253. }
  254. }