Beispiel #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' });
 }
 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'});
 }
Beispiel #3
0
 public HostGun(string device, HostChangedListener l)
 {
     ltz = new LazerTagSerial(device);
     this.listener = l;
 }
Beispiel #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;
 }
 protected void OnIoError(LazerTagSerial.IoErrorEventArgs e)
 {
     if (IoError != null) IoError(this, e);
 }
 private void Serial_IoError(object sender, LazerTagSerial.IoErrorEventArgs e)
 {
     OnIoError(e);
 }
        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;
            }
        }