Esempio n. 1
0
        public void AddUsage(string connectionId, string portName, SerialPort serialPort)
        {
            if (string.IsNullOrWhiteSpace(connectionId))
            {
                throw new ArgumentException($"{nameof(connectionId)} cannot be empty", nameof(connectionId));
            }

            if (string.IsNullOrWhiteSpace(portName))
            {
                throw new ArgumentException($"{nameof(portName)} cannot be empty", nameof(portName));
            }

            if (serialPort == null)
            {
                throw new ArgumentNullException(nameof(serialPort));
            }

            if (ExistsFor(connectionId, portName))
            {
                throw new ArgumentException($"{nameof(connectionId)} {connectionId} is already using port {portName}");
            }

            PortUsage newUsage = new PortUsage(connectionId, portName, serialPort);

            portUsages.Add(newUsage);
        }
        public void Send(string connectionId, string textToSend)
        {
            log.Trace($"SerialPortManager attempting send of \\{textToSend}\\.");

            if (!_portUsages.ExistsFor(connectionId))
            {
                log.Trace($"Connection {connectionId} has no open port, and therefore nothing can be sent.");
                throw new InvalidOperationException($"Connection {connectionId} has no open port, and therefore nothing can be sent.");
            }

            PortUsage pu = _portUsages.FromConnectionId(connectionId);

            log.Trace($"sending to port {pu.PortName}");

            pu.SerialPort.Write(textToSend);
            log.Trace($"SerialPortManager sent \\{textToSend}\\ to port {pu.PortName}.");
        }
        private void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp   = (SerialPort)sender;
            string     data = sp.ReadExisting();

            log.Trace("Received : " + data);
            PortUsage pu = _portUsages.FromPort(sp);

            pu.InputAccumulator += data;
            if (pu.InputAccumulator.Contains(sp.NewLine))
            {
                log.Trace("Sent : " + pu.InputAccumulator);
                int    eol  = pu.InputAccumulator.IndexOf(sp.NewLine);
                string line = pu.InputAccumulator.Substring(0, eol + 1);
                eol = pu.InputAccumulator.LastIndexOf(sp.NewLine);
                pu.InputAccumulator = pu.InputAccumulator.Substring(eol + 1);
                _hubContext.Clients.Client(pu.ConnectionId).dataReceived(sp.PortName, line);
            }
        }
Esempio n. 4
0
        public void RemoveUsage(string connectionId, string portName)
        {
            if (string.IsNullOrWhiteSpace(connectionId))
            {
                throw new ArgumentException($"{nameof(connectionId)} cannot be empty", nameof(connectionId));
            }

            if (string.IsNullOrWhiteSpace(portName))
            {
                throw new ArgumentException($"{nameof(portName)} cannot be empty", nameof(portName));
            }

            if (!ExistsFor(connectionId, portName))
            {
                throw new ArgumentException($"{nameof(connectionId)} {connectionId} is not using port {portName}");
            }

            PortUsage itemToRemove = portUsages.Single(pu => pu.ConnectionId == connectionId && pu.PortName == portName);

            itemToRemove.Dispose();
            portUsages.Remove(itemToRemove);
        }