public static void initDevice(SdcSoftDevice device, byte[] bytes, int byteStartIndex) { int endIndex = byteStartIndex + device.getDeviceBytesLength(); /** * 校验数据长度有效性 */ if (bytes.Length < endIndex) { return; } /** * byte 数组裁剪 */ byte[] current = new byte[device.getDeviceBytesLength()]; for (int i = 0, j = byteStartIndex; j < endIndex; i++, j++) { current[i] = bytes[j]; } /** * 填充设备信息 */ DevicePointMap devicePointMap = maps[device.getDeviceType()]; device.handleDeviceNo(current); foreach (string key in devicePointMap.getPointMap().Keys) { ByteField f = devicePointMap.getPointMap()[key]; device.handleByteField(f, current); } }
/** * 获得plc数据信息 * * @param bytes * @return **/ public static SdcSoftDevice getDeviceFromByteArray(byte[] bytes, string typeName) { SdcSoftDevice device = null; try { //DevicePointMap devicePointMap = Activator.CreateInstance(null, string.Format("GLMonitoringSystem.Devices.Map.DevicePointMap__{0}", typeName)).Unwrap() as DevicePointMap; var devicePointMap = MapHelper.GetDevicePointMap(typeName); if (devicePointMap == null) { return(null); } device = Activator.CreateInstance(null, string.Format("GLMonitoringSystem.Devices.Device_{0}", typeName)).Unwrap() as SdcSoftDevice; if (!device.validate(bytes.Length)) { return(null); } device.handleDeviceNo(bytes); foreach (string key in devicePointMap.getPointMap().Keys) { ByteField f = devicePointMap.getPointMap()[key]; device.handleByteFileds(f, bytes); } } catch (Exception e) { Console.WriteLine(e.Message); } return(device); }
/// <summary> /// 获得设备数据信息 /// </summary> /// <param name="bytes"></param> /// <param name="typeName"></param> /// <returns></returns> public static SdcSoftDevice getDeviceByByte(byte[] bytes, String typeName) { SdcSoftDevice device = Activator.CreateInstance(null, string.Format(STRING_FORMAT_DEVICE_PATH, typeName)).Unwrap() as SdcSoftDevice; if (device.validateFalse(bytes.Length)) { return(null); } device.handleDeviceNo(bytes); var map = Activator.CreateInstance(null, string.Format(STRING_FORMAT_DEVICE_MAP_PATH, typeName)).Unwrap() as DevicePointMap; if (map == null) { return(null); } var pointMap = map.getPointMap(); foreach (string key in pointMap.Keys) { ByteField f = pointMap[key]; device.handleByteField(f, bytes); } return(device); }