예제 #1
0
        /// <summary>
        /// Sends a message to the device with the given
        /// DeviceId in order to try to identify it, and
        /// if successful, returns a DeviceBase object.
        /// The derived type of the returned object varies
        /// depending on the Device category discovered
        /// during the identification process.
        /// </summary>
        /// <returns>null if unsuccessful - check plm.Exception</returns>
        public bool TryConnectToDevice(DeviceId deviceId, out DeviceBase device)
        {
            if (deviceId == null)
            {
                throw new ArgumentNullException("deviceId");
            }
            DeviceBase result = null;

            if (this.deviceCache.ContainsKey(deviceId))
            {
                result = this.deviceCache[deviceId];
            }
            else
            {
                this.plm.exceptionHandler(() =>
                {
                    byte[] responseAck = this.plm.sendStandardLengthMessageAndWait4Response(
                        deviceId, Constants.MSG_FLAGS_DIRECT, 0x10, 0x00);
                    byte[] responseIdRequest = this.plm.waitForStandardMessageFrom(deviceId);
                    result = DeviceFactory.BuildDevice(this.plm, responseIdRequest);
                });
                if (result != null)
                {
                    this.deviceCache.Add(deviceId, result);
                }
            }
            device = result;
            return(result != null);
        }
예제 #2
0
        public static T BuildDeviceForTest <T>(byte deviceCategory, byte deviceSubcategory, ISerialPortController serialPortController,
                                               byte peerIdHi, byte peerIdMiddle, byte peerIdLo) where T : DeviceBase
        {
            const byte PEER_FIRMWARE = 0x00;
            const byte MSG_FLAGS     = Constants.MSG_FLAGS_BROADCAST;

            var plm = new Plm(serialPortController);

            byte[] idRequestResponse = new byte[] { 0x02, 0x50, peerIdHi, peerIdMiddle, peerIdLo,
                                                    deviceCategory, deviceSubcategory, PEER_FIRMWARE, MSG_FLAGS, 0x01, 0xFF };

            return(DeviceFactory.BuildDevice(plm, idRequestResponse) as T);
        }