Beispiel #1
0
 public void Disconnect()
 {
     if (SerialPortAdapterRef != null)
     {
         _serialPortAdapter.Disconnect(SerialPortAdapterRef);
         SerialPortAdapterRef = null;
         Printer = null;
     }
 }
Beispiel #2
0
 public void Connect(PrinterConfigurationModel printer)
 {
     if (SerialPortAdapterRef == null)
     {
         SerialPortAdapterRef = _serialPortAdapter.Connect(
             printer,
             new Action <SerialPortAdapterRef, string>(DataReceivedCallback));
         Printer = printer;
     }
 }
Beispiel #3
0
 public ISerialPort GetSerialPort(SerialPortAdapterRef portRef)
 {
     if (_portsByRef.TryGetValue(portRef, out var serialPort))
     {
         return(serialPort);
     }
     else
     {
         return(null);
     }
 }
Beispiel #4
0
        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);
            }
        }
Beispiel #5
0
        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);
        }
Beispiel #6
0
 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);
     }
 }
Beispiel #7
0
 private void DataReceivedCallback(
     SerialPortAdapterRef portRef,
     string data)
 {
     _printerPacketParser.ReceiveData(data);
 }