1 module puppeteer.serial.ox_serial_port_wrapper; 2 3 import puppeteer.serial.iserial_port; 4 import puppeteer.serial.baud_rate; 5 import puppeteer.serial.parity; 6 7 import onyx.serial : OnyxParity = Parity, OnyxBaudRate = Speed; 8 import onyx.serial : OxSerialPort, SerialPortTimeOutException; 9 10 class OxSerialPortWrapper : ISerialPort 11 { 12 private OxSerialPort port; 13 14 this(string filename, Parity parity, BaudRate baudRate, uint timeOutMilliseconds) 15 { 16 OnyxParity mapParity(Parity parity) 17 { 18 final switch (parity) 19 { 20 case Parity.none: 21 return OnyxParity.none; 22 23 case Parity.odd: 24 return OnyxParity.odd; 25 26 case Parity.even: 27 return OnyxParity.even; 28 } 29 } 30 31 OnyxBaudRate mapBaudRate(BaudRate baudRate) 32 { 33 final switch (baudRate) 34 { 35 case BaudRate.B9600: 36 return OnyxBaudRate.S9600; 37 } 38 } 39 40 port = OxSerialPort(filename, mapBaudRate(baudRate), mapParity(parity), timeOutMilliseconds); 41 } 42 43 public bool open() 44 { 45 if(!port.isOpen) 46 port.open(); 47 48 return port.isOpen(); 49 } 50 51 public void close() 52 { 53 if(port.isOpen) 54 port.close(); 55 } 56 57 public void write(ubyte[] bytes) 58 { 59 port.write(bytes); 60 } 61 62 public ubyte[] read(int maxBytes) 63 { 64 try 65 { 66 return port.read(maxBytes); 67 } 68 catch(SerialPortTimeOutException) 69 { 70 return null; 71 } 72 } 73 }