1 module puppeteer.serial.ox_serial_port_wrapper;
2 
3 import puppeteer.serial.i_serial_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     @property
15     public bool isOpen()
16     {
17         return port.isOpen;
18     }
19 
20 	this(string filename, Parity parity, BaudRate baudRate, uint timeOutMilliseconds)
21 	{
22 		OnyxParity mapParity(Parity parity)
23 		{
24 			final switch (parity)
25 			{
26 				case Parity.none:
27 					return OnyxParity.none;
28 
29 				case Parity.odd:
30 					return OnyxParity.odd;
31 
32 				case Parity.even:
33 					return OnyxParity.even;
34 			}
35 		}
36 
37 		OnyxBaudRate mapBaudRate(BaudRate baudRate)
38 		{
39 			final switch (baudRate)
40 			{
41 				case BaudRate.B9600:
42 					return OnyxBaudRate.S9600;
43 			}
44 		}
45 
46 		port = OxSerialPort(filename, mapBaudRate(baudRate), mapParity(parity), timeOutMilliseconds);
47 	}
48 
49 	public bool open()
50 	{
51 		if(!port.isOpen)
52 			port.open();
53 
54 		return port.isOpen();
55 	}
56 
57 	public void close()
58 	{
59 		if(port.isOpen)
60 			port.close();
61 	}
62 
63 	public void write(ubyte[] bytes)
64 	{
65 		port.write(bytes);
66 	}
67 
68 	public ubyte[] read(int maxBytes)
69 	{
70 		try
71 		{
72 			return port.read(maxBytes);
73 		}
74 		catch(SerialPortTimeOutException)
75 		{
76 			return null;
77 		}
78 	}
79 }