public void Disconnect() { if (SerialPortAdapterRef != null) { _serialPortAdapter.Disconnect(SerialPortAdapterRef); SerialPortAdapterRef = null; Printer = null; } }
public void Connect(PrinterConfigurationModel printer) { if (SerialPortAdapterRef == null) { SerialPortAdapterRef = _serialPortAdapter.Connect( printer, new Action <SerialPortAdapterRef, string>(DataReceivedCallback)); Printer = printer; } }
public ISerialPort GetSerialPort(SerialPortAdapterRef portRef) { if (_portsByRef.TryGetValue(portRef, out var serialPort)) { return(serialPort); } else { return(null); } }
public bool Disconnect(SerialPortAdapterRef portRef) { if (_portsByRef.ContainsKey(portRef)) { var serialPort = _portsByRef[portRef]; serialPort.Close(); _portsByRef.TryRemove(portRef, out _); _refsByPort.TryRemove(serialPort, out _); serialPort.Dispose(); return(true); } else { return(false); } }
public SerialPortAdapterRef Connect( PrinterConfigurationModel config, Action <SerialPortAdapterRef, string> dataReceivedCallback) { var portRef = new SerialPortAdapterRef( config, dataReceivedCallback); var serialPort = (SerialPortType)Activator.CreateInstance(typeof(SerialPortType), config.Port, config.BaudRate); serialPort.DataReceived += SerialPort_DataReceived; serialPort.Open(); _portsByRef.TryAdd(portRef, serialPort); _refsByPort.TryAdd(serialPort, portRef); return(portRef); }
public bool Write( SerialPortAdapterRef portRef, byte[] data, int offset, int count) { if (_portsByRef.ContainsKey(portRef)) { var serialPort = _portsByRef[portRef]; serialPort.Write( data, offset, count); return(true); } else { return(false); } }
private void DataReceivedCallback( SerialPortAdapterRef portRef, string data) { _printerPacketParser.ReceiveData(data); }