Ejemplo n.º 1
0
        /// <summary>
        /// Processes the strike received for version 1 receiver (EIDSS 2.0)
        /// </summary>
        /// <param name="e">The <see cref="LaJust.PowerMeter.Communications.Hardware.DataPacketReceivedEventArgs"/> instance containing the event data.</param>
        #if EIDSS2
        private DeviceEventData Process_HoguDataReceivedV1(DataPacketReceivedEventArgs e)
        {
            // Generate acknowledgement packet and send to receiver
            _recvrComm.SendDataPacket(new byte[3] {
                e.DataPacket[(byte)HoguDataFieldsV1.GameNumber], 
                (byte)OpCodeCmds.Acknowledgement, 
                e.DataPacket[(byte)HoguDataFieldsV1.SequenceNumber] });

            // Parse data packet
            DeviceEventData deviceData = new DeviceEventData()
            {
                Receiver = this,
                GameNumber = e.DataPacket[(byte)HoguDataFieldsV1.GameNumber],
                OpCode = (OpCodes)e.DataPacket[(byte)HoguDataFieldsV1.OpCode],
                RegSequence = e.DataPacket[(byte)HoguDataFieldsV1.RegSequence],
                VestHitValue = e.DataPacket[(byte)HoguDataFieldsV1.VestHitValue],
                HeadHitValue = e.DataPacket[(byte)HoguDataFieldsV1.HeadHitValue],
                TouchStatus = (TouchSensorStatusEnum)e.DataPacket[(byte)HoguDataFieldsV1.TouchStatus],
                DeviceStatus = (DeviceStatusEnum)e.DataPacket[(byte)HoguDataFieldsV1.HoguStatus],
                SequenceNumber = e.DataPacket[(byte)HoguDataFieldsV1.SequenceNumber]
            };

            // Insert device Id information we stored during registration as V1 data packet does not include it
            switch (deviceData.OpCode)
            {
                case OpCodes.ChungDataV1: deviceData.DeviceId = _ChungDeviceId; break;
                case OpCodes.HongDataV1: deviceData.DeviceId = _HongDeviceId; break;
            }

            return deviceData;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Processes the strike received for V2 receiver (EIDSS 3.0)
        /// </summary>
        /// <param name="e">The <see cref="LaJust.PowerMeter.Communications.Hardware.DataPacketReceivedEventArgs"/> instance containing the event data.</param>
        private DeviceEventData Process_HoguDataReceivedV2(DataPacketReceivedEventArgs e)
        {
            // Generate acknowledgement packet and send to receiver
            _recvrComm.SendDataPacket(new byte[3] {
                e.DataPacket[(byte)HoguDataFieldsV2.GameNumber], 
                (byte)OpCodeCmds.Acknowledgement, 
                e.DataPacket[(byte)HoguDataFieldsV2.SequenceNumber] });

            // Parse data packet
            DeviceEventData deviceData = new DeviceEventData()
            {
                Receiver = this,
                GameNumber = e.DataPacket[(byte)HoguDataFieldsV2.GameNumber],
                OpCode = (OpCodes)e.DataPacket[(byte)HoguDataFieldsV2.OpCode],
                RegSequence = e.DataPacket[(byte)HoguDataFieldsV2.RegSequence],
                VestHitValue = e.DataPacket[(byte)HoguDataFieldsV2.VestHitValue],
                HeadHitValue = e.DataPacket[(byte)HoguDataFieldsV2.HeadHitValue],
                TouchStatus = (TouchSensorStatusEnum)e.DataPacket[(byte)HoguDataFieldsV2.TouchStatus],
                DeviceStatus = (DeviceStatusEnum)e.DataPacket[(byte)HoguDataFieldsV2.HoguStatus],
                DeviceId = new DeviceId(
                    e.DataPacket[(byte)HoguDataFieldsV2.ID1],
                    e.DataPacket[(byte)HoguDataFieldsV2.ID2],
                    e.DataPacket[(byte)HoguDataFieldsV2.ID3]
                ),
                SequenceNumber = e.DataPacket[(byte)HoguDataFieldsV2.SequenceNumber]
            };

            return deviceData;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Fired when valid data packet received.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The e.</param>
        private void RecvrComm_PacketReceivedHandler(object sender, DataPacketReceivedEventArgs e)
        {
            if (e.DataLength < 3) return;

            // Identify the packet and generate appropriate high level event
            OpCodes opCode = (OpCodes)e.DataPacket[1];

            Trace.TraceInformation("{0}.PacketReceivedHandler CommPort={1} OpCode={2} Length={3}", CLASSNAME, e.CommPort, opCode, e.DataLength);
            Trace.Indent();

            switch (opCode)
            {
                case OpCodes.PanelButtonPressed:
                    Publish_PanelButtonPress(e.DataPacket[2]);
                    break;

                #if EIDSS2
                case OpCodes.HongNotResponding:
                case OpCodes.ChungNotResponding:
                    Process_HoguNotResponding(opCode);
                    break;

                case OpCodes.ChungDataV1:
                case OpCodes.HongDataV1:
                    Publish_EventData( Process_HoguDataReceivedV1(e) );
                    break;
                #endif

                case OpCodes.ChungDataV2:
                case OpCodes.HongDataV2:
                    Publish_EventData ( Process_HoguDataReceivedV2(e) );
                    break;

                case OpCodes.TargetDataV2:
                    Publish_EventData ( Process_TargetDataReceivedV2(e) );
                    break;

                case OpCodes.HongRegistered:
                case OpCodes.ChungRegistered:
                case OpCodes.TargetRegistered:
                case OpCodes.HongPreRegistered:
                case OpCodes.ChungPreRegistered:
                    Publish_DeviceRegistration( Process_DeviceRegistration(e) );
                    break;

                default:
                    Trace.TraceWarning("{0}.RecvrComm_PacketReceivedHandler: Unknown packet type! Length={1} OpCode=0x{2:x2} Data={3}",
                        CLASSNAME, e.DataLength, (byte)opCode, BitConverter.ToString(e.DataPacket));
                    break;
            }
            Trace.Unindent();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Raises the <see cref="E:DataPacketReceived"/> event.
        /// </summary>
        /// <param name="e">The <see cref="LaJust.PowerMeter.Communications.DataPacketReceivedEventArgs"/> instance containing the event data.</param>
        protected virtual void OnDataPacketReceived(DataPacketReceivedEventArgs e)
        {
            EventHandler <DataPacketReceivedEventArgs> handler = DataPacketReceived;

            try
            {
                handler(this, e);
            }
            catch (Exception ex)
            {
                Trace.TraceError("{0}.OnDataPacketReceived: {1}", CLASSNAME, ex.GetBaseException());
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Processes the device registration.
        /// </summary>
        /// <param name="e">The <see cref="LaJust.EIDSS.Communications.Hardware.DataPacketReceivedEventArgs"/> instance containing the event data.</param>
        /// <returns>Device Registration Event Data</returns>
        private DeviceRegistrationEventArgs Process_DeviceRegistration(DataPacketReceivedEventArgs e)
        {
            // Parse data packet into registration data structure
            DeviceRegistrationEventArgs registrationData = new DeviceRegistrationEventArgs()
            {
                ReceiverId           = this.Id,
                GameNumber           = e.DataPacket[(byte)RegistrationDataFields.GameNumber],
                OperationCode        = (OpCodes)e.DataPacket[(byte)RegistrationDataFields.OpCode],
                CourtNumber          = e.DataPacket[(byte)RegistrationDataFields.CourtNumber],
                RegistrationSequence = e.DataPacket[(byte)RegistrationDataFields.RegSequence],
                MinimumPressure      = e.DataPacket[(byte)RegistrationDataFields.MinimumImpact],
                TouchSensorMode      = (TouchSensorStatusEnum)e.DataPacket[(byte)RegistrationDataFields.TouchSensorMode],
                Id = new DeviceId(e.DataPacket[(byte)RegistrationDataFields.Id1], e.DataPacket[(byte)RegistrationDataFields.Id2], e.DataPacket[(byte)RegistrationDataFields.Id3])
            };

            return(registrationData);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Processes the strike received for V2 receiver (EIDSS 3.0)
        /// </summary>
        /// <param name="e">The <see cref="LaJust.PowerMeter.Communications.Hardware.DataPacketReceivedEventArgs"/> instance containing the event data.</param>
        private DeviceEventData Process_TargetDataReceivedV2(DataPacketReceivedEventArgs e)
        {
            // Generate acknowledgement packet and send to receiver
            _recvrComm.SendDataPacket(new byte[3] {
                e.DataPacket[(int)TargetDataFieldsV2.GameNumber], 
                (byte)OpCodeCmds.Acknowledgement, 
                e.DataPacket[(int)TargetDataFieldsV2.SequenceNumber] });

            // Parse data packet
            DeviceEventData deviceData = new DeviceEventData()
            {
                Receiver = this,
                GameNumber = e.DataPacket[(byte)TargetDataFieldsV2.GameNumber],
                OpCode = (OpCodes)e.DataPacket[(byte)TargetDataFieldsV2.OpCode],
                RegSequence = e.DataPacket[(byte)TargetDataFieldsV2.RegSequence],
                VestHitValue = e.DataPacket[(byte)TargetDataFieldsV2.VestHitValue],
                HeadHitValue = e.DataPacket[(byte)TargetDataFieldsV2.HeadHitValue],
                // Remove "touch sensor" bit from wet bag panel byte by AND with 0x77 (0111 0111)
                WetBagPanel = (WetBagPanelEnum)(e.DataPacket[(byte)TargetDataFieldsV2.PanelValue] & 0x77),
                // Determine "touch sensor" bit from wet bag panel byte by checking 0x08 or 0x80 bits
                TouchStatus = (((e.DataPacket[(byte)TargetDataFieldsV2.PanelValue] & 0x08) == 0x08) || 
                               ((e.DataPacket[(byte)TargetDataFieldsV2.PanelValue] & 0x80) == 0x80)) ? 
                               TouchSensorStatusEnum.Required : TouchSensorStatusEnum.NotRequired,
                DeviceStatus = (DeviceStatusEnum)e.DataPacket[(byte)TargetDataFieldsV2.HoguStatus],
                DeviceId = new DeviceId(
                    e.DataPacket[(byte)TargetDataFieldsV2.ID1],
                    e.DataPacket[(byte)TargetDataFieldsV2.ID2],
                    e.DataPacket[(byte)TargetDataFieldsV2.ID3]
                ),
                SequenceNumber = e.DataPacket[(byte)TargetDataFieldsV2.SequenceNumber],
                TargetNumber = e.DataPacket[(byte)TargetDataFieldsV2.TargetNumber],
                TargetTotal = e.DataPacket[(byte)TargetDataFieldsV2.TargetTotal]
            };

            return deviceData;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Processes the device registration.
 /// </summary>
 /// <param name="e">The <see cref="LaJust.PowerMeter.Communications.Hardware.DataPacketReceivedEventArgs"/> instance containing the event data.</param>
 private DeviceRegistrationEventData Process_DeviceRegistration(DataPacketReceivedEventArgs e)
 {
     // Parse data packet into registration data structure
     DeviceRegistrationEventData registrationData = new DeviceRegistrationEventData()
     {
         Receiver = this,
         GameNumber = e.DataPacket[(byte)RegistrationDataFields.GameNumber],
         OpCode = (OpCodes)e.DataPacket[(byte)RegistrationDataFields.OpCode],
         CourtNumber = e.DataPacket[(byte)RegistrationDataFields.CourtNumber],
         RegistrationSequence = e.DataPacket[(byte)RegistrationDataFields.RegSequence],
         MinimumPressure = e.DataPacket[(byte)RegistrationDataFields.MinimumPressure],
         TouchSensorMode = (TouchSensorStatusEnum)e.DataPacket[(byte)RegistrationDataFields.TouchSensorMode],
         Id = new DeviceId(
             e.DataPacket[(byte)RegistrationDataFields.Id1],
             e.DataPacket[(byte)RegistrationDataFields.Id2],
             e.DataPacket[(byte)RegistrationDataFields.Id3]
         )
     };
     return registrationData;
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Handles the DataReceived event of the serialPort control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.IO.Ports.SerialDataReceivedEventArgs"/> instance containing the event data.</param>
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Data format: STX COUNT DATA ... CHECKSUM ETX
            DataPacketReceivedEventArgs eventData = null;

            lock (_lock)
            {
                try
                {
                    while (_serialPort.BytesToRead > 0)
                    {
                        byte data = (byte)_serialPort.ReadByte();

                        switch (_state)
                        {
                        case RecvrState.Waiting:
                            if (data == 0x02)
                            {
                                _state = RecvrState.PacketLength;
                            }
                            break;

                        case RecvrState.PacketLength:
                            _checksum   = data;
                            _dataLength = data - 1;
                            _bufPos     = 0;
                            _buffer.Initialize();
                            _state = RecvrState.PacketData;
                            break;

                        case RecvrState.PacketData:
                            if (_bufPos < _dataLength)
                            {
                                _buffer[_bufPos++] = data;
                                _checksum         ^= data;
                            }

                            if (_bufPos == _dataLength)
                            {
                                _state = RecvrState.Checksum;
                            }
                            break;

                        case RecvrState.Checksum:
                            if (_checksum != data)
                            {
                                Trace.TraceWarning("{0}.SerialPort_DataReceived: Checksum mismatch. Expected={1} Actual={2}", CLASSNAME, _checksum, data);
                                _state = RecvrState.Waiting;
                            }
                            else
                            {
                                _state = RecvrState.Received;
                            }
                            break;

                        case RecvrState.Received:
                            if (data == 0x03)
                            {
                                // We got a message!
                                eventData = new DataPacketReceivedEventArgs()
                                {
                                    CommPort   = _serialPort.PortName,
                                    DataLength = _dataLength,
                                    DataPacket = new byte[_bufPos]
                                };
                                Array.Copy(_buffer, 0, eventData.DataPacket, 0, _bufPos);
                                _state = RecvrState.Waiting;
                            }
                            break;

                        default:
                            Trace.TraceError("{0}.SerialPort_DataReceived: Unknown receive state, resetting state");
                            _state = RecvrState.Waiting;
                            break;
                        }
                    }
                }
                catch (InvalidOperationException ex)
                {
                    Trace.TraceError("{0}.SerialPort_DataReceived: CommPort={1} Exception={2}", CLASSNAME, _serialPort.PortName, ex.GetBaseException());
                }
            } // end lock

            // Fire event if we have data (we are outside our lock here)
            if (eventData != null)
            {
                OnDataPacketReceived(eventData);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Handles the DataReceived event of the serialPort control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.IO.Ports.SerialDataReceivedEventArgs"/> instance containing the event data.</param>
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Data format: STX COUNT DATA ... this.checkSum ETX
            DataPacketReceivedEventArgs eventData = null;

            lock (this.portLock)
            {
                try
                {
                    while (this.serialPort.BytesToRead > 0)
                    {
                        byte data = (byte)this.serialPort.ReadByte();

                        switch (this.receiverState)
                        {
                            case ReceiverState.Waiting:
                                if (data == 0x02)
                                {
                                    this.receiverState = ReceiverState.PacketLength;
                                }

                                break;

                            case ReceiverState.PacketLength:
                                this.checkSum = data;
                                this.dataLength = data - 1;
                                this.bufferPosition = 0;
                                this.serialBuffer.Initialize();
                                this.receiverState = ReceiverState.PacketData;
                                break;

                            case ReceiverState.PacketData:
                                if (this.bufferPosition < this.dataLength)
                                {
                                    this.serialBuffer[this.bufferPosition++] = data;
                                    this.checkSum ^= data;
                                }

                                if (this.bufferPosition == this.dataLength)
                                {
                                    this.receiverState = ReceiverState.checkSum;
                                }

                                break;

                            case ReceiverState.checkSum:
                                if (this.checkSum != data)
                                {
                                    Trace.TraceWarning("{0}.SerialPort_DataReceived: this.checkSum mismatch. Expected={1} Actual={2}", CLASSNAME, this.checkSum, data);
                                    this.receiverState = ReceiverState.Waiting;
                                }
                                else
                                {
                                    this.receiverState = ReceiverState.Received;
                                }

                                break;

                            case ReceiverState.Received:
                                if (data == 0x03)
                                {
                                    // We got a message!
                                    eventData = new DataPacketReceivedEventArgs()
                                    {
                                        CommPort = this.serialPort.PortName,
                                        DataLength = this.dataLength,
                                        DataPacket = new byte[this.bufferPosition]
                                    };
                                    Array.Copy(this.serialBuffer, 0, eventData.DataPacket, 0, this.bufferPosition);
                                    this.receiverState = ReceiverState.Waiting;
                                }

                                break;

                            default:
                                Trace.TraceError("{0}.SerialPort_DataReceived: Unknown receive state, resetting state");
                                this.receiverState = ReceiverState.Waiting;
                                break;
                        }
                    }
                }
                catch (InvalidOperationException ex)
                {
                    Trace.TraceError("{0}.SerialPort_DataReceived: CommPort={1} Exception={2}", CLASSNAME, this.serialPort.PortName, ex.GetBaseException());
                }
            } // end lock

            // Fire event if we have data (we are outside our lock here)
            if (eventData != null)
            {
                this.OnDataPacketReceived(eventData);
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// Raises the <see cref="E:DataPacketReceived"/> event.
 /// </summary>
 /// <param name="e">The <see cref="LaJust.EIDSS.Communications.Hardware.DataPacketReceivedEventArgs"/> instance containing the event data.</param>
 protected virtual void OnDataPacketReceived(DataPacketReceivedEventArgs e)
 {
     EventHandler<DataPacketReceivedEventArgs> handler = this.DataPacketReceived;
     try
     {
         handler(this, e);
     }
     catch (Exception ex)
     {
         Trace.TraceError("{0}.OnDataPacketReceived: {1}", CLASSNAME, ex.GetBaseException());
         throw;
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Handles the DataReceived event of the serialPort control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.IO.Ports.SerialDataReceivedEventArgs"/> instance containing the event data.</param>
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Data format: STX COUNT DATA ... CHECKSUM ETX
            DataPacketReceivedEventArgs eventData = null;

            lock (_lock)
            {
                try
                {
                    while (_serialPort.BytesToRead > 0)
                    {
                        byte data = (byte)_serialPort.ReadByte();

                        switch (_state)
                        {
                            case RecvrState.Waiting:
                                if (data == 0x02)
                                {
                                    _state = RecvrState.PacketLength;
                                }
                                break;

                            case RecvrState.PacketLength:
                                _checksum = data;
                                _dataLength = data - 1;
                                _bufPos = 0;
                                _buffer.Initialize();
                                _state = RecvrState.PacketData;
                                break;

                            case RecvrState.PacketData:
                                if (_bufPos < _dataLength)
                                {
                                    _buffer[_bufPos++] = data;
                                    _checksum ^= data;
                                }

                                if (_bufPos == _dataLength)
                                {
                                    _state = RecvrState.Checksum;
                                }
                                break;

                            case RecvrState.Checksum:
                                if (_checksum != data)
                                {
                                    Trace.TraceWarning("{0}.SerialPort_DataReceived: Checksum mismatch. Expected={1} Actual={2}", CLASSNAME, _checksum, data);
                                    _state = RecvrState.Waiting;
                                }
                                else
                                {
                                    _state = RecvrState.Received;
                                }
                                break;

                            case RecvrState.Received:
                                if (data == 0x03)
                                {
                                    // We got a message!
                                    eventData = new DataPacketReceivedEventArgs() 
                                    {
                                        CommPort = _serialPort.PortName,
                                        DataLength = _dataLength,
                                        DataPacket = new byte[_bufPos]
                                    };
                                    Array.Copy(_buffer, 0, eventData.DataPacket, 0, _bufPos);
                                    _state = RecvrState.Waiting;
                                }
                                break;

                            default:
                                Trace.TraceError("{0}.SerialPort_DataReceived: Unknown receive state, resetting state");
                                _state = RecvrState.Waiting;
                                break;
                        }
                    }
                }
                catch (InvalidOperationException ex)
                {
                    Trace.TraceError("{0}.SerialPort_DataReceived: CommPort={1} Exception={2}", CLASSNAME, _serialPort.PortName, ex.GetBaseException());
                }
            } // end lock

            // Fire event if we have data (we are outside our lock here)
            if (eventData != null) OnDataPacketReceived(eventData);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Handles the DataReceived event of the serialPort control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.IO.Ports.SerialDataReceivedEventArgs"/> instance containing the event data.</param>
        private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // Data format: STX COUNT DATA ... this.checkSum ETX
            DataPacketReceivedEventArgs eventData = null;

            lock (this.portLock)
            {
                try
                {
                    while (this.serialPort.BytesToRead > 0)
                    {
                        byte data = (byte)this.serialPort.ReadByte();

                        switch (this.receiverState)
                        {
                        case ReceiverState.Waiting:
                            if (data == 0x02)
                            {
                                this.receiverState = ReceiverState.PacketLength;
                            }

                            break;

                        case ReceiverState.PacketLength:
                            this.checkSum       = data;
                            this.dataLength     = data - 1;
                            this.bufferPosition = 0;
                            this.serialBuffer.Initialize();
                            this.receiverState = ReceiverState.PacketData;
                            break;

                        case ReceiverState.PacketData:
                            if (this.bufferPosition < this.dataLength)
                            {
                                this.serialBuffer[this.bufferPosition++] = data;
                                this.checkSum ^= data;
                            }

                            if (this.bufferPosition == this.dataLength)
                            {
                                this.receiverState = ReceiverState.checkSum;
                            }

                            break;

                        case ReceiverState.checkSum:
                            if (this.checkSum != data)
                            {
                                Trace.TraceWarning("{0}.SerialPort_DataReceived: this.checkSum mismatch. Expected={1} Actual={2}", CLASSNAME, this.checkSum, data);
                                this.receiverState = ReceiverState.Waiting;
                            }
                            else
                            {
                                this.receiverState = ReceiverState.Received;
                            }

                            break;

                        case ReceiverState.Received:
                            if (data == 0x03)
                            {
                                // We got a message!
                                eventData = new DataPacketReceivedEventArgs()
                                {
                                    CommPort   = this.serialPort.PortName,
                                    DataLength = this.dataLength,
                                    DataPacket = new byte[this.bufferPosition]
                                };
                                Array.Copy(this.serialBuffer, 0, eventData.DataPacket, 0, this.bufferPosition);
                                this.receiverState = ReceiverState.Waiting;
                            }

                            break;

                        default:
                            Trace.TraceError("{0}.SerialPort_DataReceived: Unknown receive state, resetting state");
                            this.receiverState = ReceiverState.Waiting;
                            break;
                        }
                    }
                }
                catch (InvalidOperationException ex)
                {
                    Trace.TraceError("{0}.SerialPort_DataReceived: CommPort={1} Exception={2}", CLASSNAME, this.serialPort.PortName, ex.GetBaseException());
                }
            } // end lock

            // Fire event if we have data (we are outside our lock here)
            if (eventData != null)
            {
                this.OnDataPacketReceived(eventData);
            }
        }