Exemplo n.º 1
0
 public LazerTagProtocol(HostGun hostGun, string portName)
 {
     _hostGun              = hostGun;
     _serial               = new LazerTagSerial();
     _serial.IoError      += Serial_IoError;
     _serial.DataReceived += Serial_DataReceived;
     _serial.Connect(portName);
     _serial.Enqueue(new[] { (byte)'\r', (byte)'\n' });
 }
Exemplo n.º 2
0
 public LazerTagProtocol(HostGun hostGun, string portName)
 {
     _hostGun = hostGun;
     _serial = new LazerTagSerial();
     _serial.IoError += Serial_IoError;
     _serial.DataReceived += Serial_DataReceived;
     _serial.Connect(portName);
     _serial.Enqueue(new[] {(byte) '\r', (byte) '\n'});
 }
Exemplo n.º 3
0
 public HostGun(string device, HostChangedListener l)
 {
     ltz = new LazerTagSerial(device);
     this.listener = l;
 }
Exemplo n.º 4
0
 public bool SetDevice(string device)
 {
     if (ltz != null) {
         ltz.Stop();
         ltz = null;
     }
     if (device != null) {
         try {
             ltz = new LazerTagSerial(device);
         } catch (Exception ex) {
             HostDebugWriteLine(ex.ToString());
             return false;
         }
     }
     return true;
 }
Exemplo n.º 5
0
 protected void OnIoError(LazerTagSerial.IoErrorEventArgs e)
 {
     if (IoError != null) IoError(this, e);
 }
Exemplo n.º 6
0
 private void Serial_IoError(object sender, LazerTagSerial.IoErrorEventArgs e)
 {
     OnIoError(e);
 }
Exemplo n.º 7
0
        private void Serial_DataReceived(object sender, LazerTagSerial.DataReceivedEventArgs e)
        {
            if (e.Data == null) return;

            var parts = e.Data.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length < 1)
            {
                Log.Add(Log.Severity.Debug, "Received empty response.");
                return;
            }

            switch (parts[0].ToUpperInvariant())
            {
                case "RCV":
                    if (parts.Length < 4)
                    {
                        Log.Add(Log.Severity.Debug, "Received truncated response, '{0}'.", e.Data);
                        break;
                    }

                    UInt16 data;
                    byte bitCount;
                    bool isBeacon;

                    try
                    {
                        data = Convert.ToUInt16(parts[1], 16);
                        bitCount = Convert.ToByte(parts[2], 16);
                        isBeacon = (Convert.ToUInt16(parts[3], 16) != 0);
                    }
                    catch (FormatException formatException)
                    {
                        Log.Add($"Could not parse number in response, '{e.Data}'.", formatException);
                        break;
                    }
                    catch (OverflowException overflowException)
                    {
                        Log.Add($"Number overflow in response, '{e.Data}'.", overflowException);
                        break;
                    }

                    try
                    {
                        ProcessSignature(data, bitCount, isBeacon);
                    }
                    catch (Exception ex)
                    {
                        Log.Add($"ProcessSignature() failed for response, '{e.Data}'.", ex);
                    }
                    break;
                case "ERROR":
                    Log.Add(Log.Severity.Debug, e.Data);
                    break;
                default:
                    Log.Add(Log.Severity.Debug, $"Received unrecognized response, '{e.Data}'.");
                    break;
            }
        }