1
0

main.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import Foundation
  2. import SwiftSerial
  3. let arguments = CommandLine.arguments
  4. guard arguments.count >= 2 else {
  5. print("Need serial port name, e.g. /dev/ttyUSB0 as the first argument.")
  6. exit(1)
  7. }
  8. print("Connect a null modem serial cable between two machines before you continue to use this program")
  9. let portName = arguments[1]
  10. let serialPort: SerialPort = SerialPort(path: portName)
  11. var myturn = true
  12. func getKeyPress () -> UnicodeScalar {
  13. let valueRead: Int = Int(getchar())
  14. guard let charRead = UnicodeScalar(valueRead) else{
  15. return UnicodeScalar("")!
  16. }
  17. return charRead
  18. }
  19. func printToScreenFrom(myself: Bool, characterToPrint: UnicodeScalar){
  20. if(myturn && !myself){
  21. myturn = false
  22. print("\nOther: ", terminator:"")
  23. } else if (!myturn && myself){
  24. myturn = true
  25. print("\nMe: ", terminator:"")
  26. }
  27. print(characterToPrint, terminator:"")
  28. }
  29. do {
  30. print("Attempting to open port: \(portName)")
  31. try serialPort.openPort()
  32. print("Serial port \(portName) opened successfully.")
  33. defer {
  34. serialPort.closePort()
  35. print("Port Closed")
  36. }
  37. serialPort.setSettings(receiveRate: .baud9600,
  38. transmitRate: .baud9600,
  39. minimumBytesToRead: 1)
  40. /* These prepares the stdin so we can getchar() without echoing */
  41. // Set up the control structure
  42. var settings = termios()
  43. // Get options structure for stdin
  44. tcgetattr(STDIN_FILENO, &settings)
  45. //Turn off ICANON and ECHO
  46. settings.c_lflag &= ~tcflag_t(ICANON | ECHO)
  47. tcsetattr(STDIN_FILENO, TCSANOW, &settings)
  48. //Turn off output buffering if not multiple threads will have problems printing
  49. setbuf(stdout, nil);
  50. //Run the serial port reading function in another thread
  51. var readingThread = pthread_t()
  52. let pthreadFunc: @convention(c) (UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? = {
  53. observer in
  54. while true{
  55. do{
  56. var readCharacter = try serialPort.readChar()
  57. printToScreenFrom(myself: false, characterToPrint: readCharacter)
  58. } catch {
  59. print("Error: \(error)")
  60. }
  61. }
  62. }
  63. pthread_create(&readingThread, nil, pthreadFunc, nil)
  64. print("\nReady to send and receive messages in realtime!")
  65. print("\nMe: ", terminator:"")
  66. while true {
  67. var enteredKey = getKeyPress()
  68. printToScreenFrom(myself: true, characterToPrint: enteredKey)
  69. var _ = try serialPort.writeChar(enteredKey)
  70. }
  71. } catch PortError.failedToOpen {
  72. print("Serial port \(portName) failed to open. You might need root permissions.")
  73. } catch {
  74. print("Error: \(error)")
  75. }