1 /** 2 * Defines channel objects layer. 3 * 4 * A channel is a message delivery mechanism that forwards a message from a 5 * sender to one receiver. 6 */ 7 module dolina.channel; 8 9 import std.exception; 10 import serial.device; 11 /** 12 * Defines a basic HostLink channel 13 */ 14 interface IHostLinkChannel { 15 /** 16 * Reads a message from channel 17 * 18 * Returns: message read 19 */ 20 string read(); 21 22 /** 23 * Writes a message on channel 24 * 25 * Params: message = The message being sent on the channel. 26 */ 27 void write(string message); 28 } 29 30 /** 31 * Channel based on serial RS232 communication 32 */ 33 class HostLinkChannel: IHostLinkChannel { 34 private SerialPort serialPort; 35 this(SerialPort serialPort) { 36 enforce(serialPort !is null); 37 this.serialPort = serialPort; 38 } 39 40 string read() { 41 enum START = 0x40; // @ 42 enum END = 0x0D; // CR 43 bool inside; 44 45 ubyte[1] buffer; 46 ubyte[] reply; 47 ubyte b; 48 do { 49 immutable(size_t) length = serialPort.read(buffer); 50 if (length > 0) { 51 b = buffer[0]; 52 if (b == START) { 53 inside = true; 54 } 55 if (inside) { 56 reply ~= b; 57 } 58 } 59 60 } while (b != END); 61 return cast(string)(reply).idup; 62 } 63 64 void write(string message) { 65 serialPort.write(cast(void[])message); 66 } 67 }