/// <summary> /// 通讯状态观察 /// </summary> private void CommunicationObser(object sender, System.Timers.ElapsedEventArgs e) { try { communicationobserve_timer.Enabled = false; //接收超时,进行重新连接 if (LastConnectLong > 3) { LogHelper.WriteLog("重连" + DeviceID.ToString() + "号IO设备"); ReConnect(); } if (LastRecLong > 2) { IODeviceInfo IOInfo = new IODeviceInfo(); IOInfo.ID = this.DeviceID; DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "号IO设备掉线,重新尝试连接..."); //通知调度程序 小车已经掉线 IOInfo.bIsCommBreak = true; DelegateState.InvokeIOFeedBackEvent(IOInfo); } } catch (Exception ex) { DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "号IO设备,观察线程异常"); LogHelper.WriteErrorLog(ex); } finally { if (!IsStop) { communicationobserve_timer.Enabled = true; } } }
/// <summary> /// 通讯状态观察 /// </summary> private void CommunicationObser(object sender, System.Timers.ElapsedEventArgs e) { try { communicationobserve_timer.Enabled = false; //先判断小车是否已经掉线 if (LastRecLong > 2) { //如果接受消息时间已经大于2秒,则认为车子掉线了。 DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "车,已经掉线,将在1秒后重新尝试连接..."); //通知调度程序 小车已经掉线 CarBaseStateInfo car = new CarBaseStateInfo(); car.bIsCommBreak = true; car.AgvID = this.DeviceID; DelegateState.InvokeCarFeedbackEvent(car); if (LastConnectLong > 3) { //如果车子掉线且连接时间超过3秒则需要重连 LogHelper.WriteLog("重连小车" + DeviceID.ToString()); ReConnect(); } } } catch (Exception ex) { DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "车,观察线程异常"); LogHelper.WriteErrorLog(ex); } finally { { communicationobserve_timer.Enabled = true; } } }
private void timer_Tick(object sender, EventArgs e) { if (!msgReceived) { Log.Debug("Message Not Received"); if (devID == DeviceID.CPAR) { Disconnect(); } } if (initComm) { try { Log.Debug("Initializing connection"); DeviceIdentification devFunction = (DeviceIdentification)FunctionFactory.GetFunction(typeof(DeviceIdentification)); Execute(devFunction, true); devID = devFunction.Identity; SetupFunctions(devID); DeviceType.Text = "Device: " + devID.ToString(); Log.Debug(DeviceType.Text); initComm = false; Log.Status("Connected"); } catch { Disconnect(); } } else { msgReceived = false; if (mAutoKick.Text == mAutoKick.Items[1].ToString()) { var kickWatchdog = new KickWatchdog(); Execute(kickWatchdog, false); if (wdCounter > kickWatchdog.Counter) { Log.Error("CPAR Device has reset"); wdCounter = kickWatchdog.Counter; } } } }
/// <summary> /// 通讯观察线程 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void CommunicationObser(object sender, System.Timers.ElapsedEventArgs e) { try { communicationobserve_timer.Enabled = false; if (LastRecLong > 2) { //如果接受消息时间已经大于2秒,则认为车子掉线了。 //DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "充电桩,已经掉线,将重新尝试连接..."); //通知调度程序 充电桩已经掉线 ChargeStationInfo ChargeInfo = new ChargeStationInfo(); ChargeInfo.ID = this.DeviceID; ChargeInfo.IsCommBreak = true; DelegateState.InvokeChargeChangeEvent(ChargeInfo); if (LastConnectLong > 3) { //如果车子掉线且连接时间超过3秒则需要重连 LogHelper.WriteLog("重连充电桩" + DeviceID.ToString()); ReConnect(); } } //if (LastConnectLong > 4 && LastRecLong > 3) //{ // ReConnect();//重新尝试连接 //} //if (LastRecLong > 3) //{ // //通知调度程序 充电桩已经掉线 // ChargeStationInfo ChargeInfo = new ChargeStationInfo(); // ChargeInfo.ID = this.DeviceID; // ChargeInfo.IsCommBreak = true; // DelegateState.InvokeChargeChangeEvent(ChargeInfo); //} } catch (Exception ex) { LogHelper.WriteErrorLog(ex); //DelegateState.InvokeDispatchStateEvent(this.DeviceID.ToString() + "车,观察线程异常"); } finally { communicationobserve_timer.Enabled = true; } }
public void Execute(IJobExecutionContext context) { Device D; if (Tools.CurrentDeviceList.TryGetValue(DeviceID, out D)) { D.SendMagnitude(Magnitude); } else { Console.WriteLine("Cannot execute scheduled task! Device {0} is not connected.", DeviceID.ToString()); } }
/// <summary> /// 获取未知设备信息 /// </summary> /// <param name="DeviceIndex"></param> /// <param name="ClassName"></param> /// <param name="DeviceName"></param> /// <returns></returns> public static int EnumerateDevices(List <string> NameList, List <string> IDList, List <string> MfgList, List <string> TypeList, List <string> IsInstallDriversList) { Guid myGUID = System.Guid.Empty; IntPtr hDevInfo = SetupDiGetClassDevsA(ref myGUID, 0, IntPtr.Zero, DIGCF_ALLCLASSES); if (hDevInfo.ToInt32() == -1) { //设备不可用 return(-3); } SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA(); DeviceInfoData.cbSize = 28; //正常状态 DeviceInfoData.DevInst = 0; DeviceInfoData.ClassGuid = System.Guid.Empty; DeviceInfoData.Reserved = 0; UInt32 i; for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++) { //设备名称 StringBuilder DeviceName = new StringBuilder(""); //设备ID StringBuilder DeviceID = new StringBuilder(""); //设备供应商 StringBuilder Mfg = new StringBuilder(""); //设备类型 StringBuilder DeviceType = new StringBuilder(""); //设备类型 StringBuilder IsInstallDrivers = new StringBuilder(""); DeviceName.Capacity = MAX_DEV_LEN; DeviceID.Capacity = MAX_DEV_LEN; DeviceType.Capacity = MAX_DEV_LEN; Mfg.Capacity = MAX_DEV_LEN; IsInstallDrivers.Capacity = MAX_DEV_LEN; bool resName = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_DEVICEDESC, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero); if (!resName) { //设备名称未知 DeviceName = new StringBuilder(""); } bool resClass = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_CLASS, 0, DeviceType, MAX_DEV_LEN, IntPtr.Zero); if (!resClass) { //设备类型未知 DeviceType = new StringBuilder(""); } //设备ID bool resHardwareID = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_HARDWAREID, 0, DeviceID, MAX_DEV_LEN, IntPtr.Zero); if (!resHardwareID) { //设备ID未知 DeviceID = new StringBuilder(""); } //设备供应商 bool resMfg = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_MFG, 0, Mfg, MAX_DEV_LEN, IntPtr.Zero); if (!resMfg) { //设备供应商未知 Mfg = new StringBuilder(""); } bool resIsInstallDrivers = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_DRIVER, 0, IsInstallDrivers, MAX_DEV_LEN, IntPtr.Zero); if (!resIsInstallDrivers) { //设备是否安装驱动 IsInstallDrivers = new StringBuilder(""); } if (string.IsNullOrEmpty(DeviceType.ToString())) { if (!string.IsNullOrEmpty(DeviceName.ToString()) && !string.IsNullOrEmpty(DeviceID.ToString())) { TypeList.Add("其它设备"); NameList.Add(DeviceName.ToString()); IDList.Add(DeviceID.ToString()); MfgList.Add(Mfg.ToString()); IsInstallDriversList.Add(IsInstallDrivers.ToString()); } } } //释放当前设备占用内存 SetupDiDestroyDeviceInfoList(hDevInfo); return(0); }
String Command.execute(string instruction) { string deviceID; DeviceID dID = null; try { dID = new DeviceID(); } catch( Exception ex ) { } deviceID = dID.ToString(); if( deviceID.Length == 0 ) { deviceID = ""; } string retVal = ";device.platform = '" + OSInfo.GetPlatform() + "';"; retVal += "device.version = '" + OSInfo.GetVersion() + "';"; retVal += "device.name = '" + PInvoke.GetOemInfo() + "';"; retVal += "device.gap = '" + version + "';"; retVal += "device.uuid = '" + deviceID + "';"; retVal += "deviceInfo();"; return retVal; }
/// <summary> /// 命令反馈 /// </summary> public bool GetCallBack() { try { int offlinecount = 0; int allheadleftlengh = 3; int receivedlengh = 0; byte[] bufferhead = new byte[3]; while (allheadleftlengh - receivedlengh > 0) { byte[] buffertemp = new byte[allheadleftlengh - receivedlengh]; if (Tcpsocket.Available <= 0) { continue; } int lengh = Tcpsocket.Receive(buffertemp); if (lengh <= 0) { if (offlinecount == 3) { LogHelper.WriteReciveChargeMessLog("接受的充电桩" + DeviceID.ToString() + "反馈命令超时"); return(false); } offlinecount += 1; Thread.Sleep(50); } Buffer.BlockCopy(buffertemp, 0, bufferhead, receivedlengh, lengh); receivedlengh += lengh; } if (bufferhead[0] == 0x01 && bufferhead[1] == 0x01) { offlinecount = 0; receivedlengh = 0; int allcontentleftlengh = Convert.ToInt32(bufferhead[2]) + 2; byte[] buffercontent = new byte[allcontentleftlengh]; while (allcontentleftlengh - receivedlengh > 0) { byte[] buffertemp = new byte[allcontentleftlengh - receivedlengh]; if (Tcpsocket.Available <= 0) { continue; } int lengh = Tcpsocket.Receive(buffertemp); if (lengh <= 0) { if (offlinecount == 3) { LogHelper.WriteReciveChargeMessLog("接受的充电桩" + DeviceID.ToString() + "反馈命令超时"); return(false); } offlinecount += 1; Thread.Sleep(50); } Buffer.BlockCopy(buffertemp, 0, buffercontent, receivedlengh, lengh); receivedlengh += lengh; } List <byte> msg = new List <byte>(); string SenDLog = ""; msg.AddRange(bufferhead); msg.AddRange(buffercontent); foreach (byte item in msg) { SenDLog += ((int)item).ToString("X") + " "; } LogHelper.WriteReciveChargeMessLog("接受的充电桩" + this.DeviceID.ToString() + "反馈命令:" + SenDLog); //List<byte> dd = new List<byte>(); //dd.AddRange(new byte[] { 0x01, 0x01, 0x03, 0x06, 0x01, 0x01, 0x1C, 0x1F }); //var rr = BitConverter.GetBytes(CRC.CRC16(dd.Take(dd.Count - 2).ToArray(), 0, dd.Count - 3)); var r = BitConverter.GetBytes(CRC.CRC16(msg.Take(msg.Count - 2).ToArray(), 0, msg.Count - 3)); if (msg[msg.Count - 2] != r[1] || msg[msg.Count - 1] != r[0]) { LogHelper.WriteReciveChargeMessLog("接受的充电桩" + this.DeviceID.ToString() + "校验位错误!"); return(false); } //分析充电桩状态 0待机 1故障 2进行 3完成 ChargeStationInfo chargestation = new ChargeStationInfo(); chargestation.ID = this.DeviceID; string StateStr = Convert.ToString(msg[3], 2).PadLeft(8, '0'); if (StateStr.Substring(7, 1) == "1") { chargestation.ChargeState = 0; } else if (StateStr.Substring(6, 1) == "1") { chargestation.ChargeState = 1; } else if (StateStr.Substring(5, 1) == "1") { chargestation.ChargeState = 2; } else if (StateStr.Substring(4, 1) == "1") { chargestation.ChargeState = 3; } else { chargestation.ChargeState = -1; } chargestation.IsCommBreak = false; DelegateState.InvokeChargeChangeEvent(chargestation); LastRecTime = DateTime.Now; return(true); } else if (bufferhead[0] == 0x01 && bufferhead[1] == 0x05) { return(true); } return(false); } catch (Exception ex) { LogHelper.WriteLog("充电桩解析编解码错误!" + ex.Message); } return(false); }
public static List <DeviceEntity> GetNomarlDevice() { UInt32 RequiredSize = 0; Guid guid = Guid.Empty; Guid[] guids = new Guid[1]; SP_DEVINFO_DATA DeviceInfoData = new SP_DEVINFO_DATA(); bool res = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize); if (res == false) { guids = new Guid[RequiredSize]; res = SetupDiClassGuidsFromNameA("ports", ref guids[0], RequiredSize, ref RequiredSize); if (!res || RequiredSize == 0) { Debug.WriteLine("类型不正确"); } } if (res == true) { List <DeviceEntity> list = new List <Hardware.DeviceEntity>(); Guid myGUID = System.Guid.Empty; IntPtr hDevInfo = SetupDiGetClassDevs(guids[0], 0, IntPtr.Zero, DIGCF_PRESENT); if (hDevInfo.ToInt64() == -1) { Debug.WriteLine("SetupDiGetClassDevs: 获取设备错误。"); } else { DeviceInfoData.cbSize = Marshal.SizeOf(DeviceInfoData); DeviceInfoData.devInst = 0; DeviceInfoData.classGuid = System.Guid.Empty; DeviceInfoData.reserved = 0; UInt32 i; for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++) { DeviceEntity entity = new DeviceEntity(); StringBuilder DeviceName = new StringBuilder("", 254); bool resName = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_FRIENDLYNAME, 0, DeviceName, MAX_DEV_LEN, IntPtr.Zero); if (!resName) { } else { entity.DeviceName = DeviceName.ToString(); } StringBuilder DeviceID = new StringBuilder("", 254); bool resHardwareID = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_HARDWAREID, 0, DeviceID, MAX_DEV_LEN, IntPtr.Zero); if (!resHardwareID) { //设备ID未知 DeviceID = new StringBuilder(""); } else { entity.DeviceID = DeviceID.ToString(); } StringBuilder Mfg = new StringBuilder("", 254); bool resHardwareMfg = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_MFG, 0, Mfg, MAX_DEV_LEN, IntPtr.Zero); if (!resHardwareMfg) { } else { entity.DevicePIDVID = Mfg.ToString(); } StringBuilder realPath = new StringBuilder("", 2047); bool resHardwareRealPath = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_LOCATION_PATHS, 0, realPath, MAX_DEV_LEN, IntPtr.Zero); if (!resHardwareRealPath) { } else { entity.RealTimePath = realPath.ToString(); } StringBuilder installState = new StringBuilder("", 64); bool resHardwareInstallState = SetupDiGetDeviceRegistryPropertyA(hDevInfo, DeviceInfoData, SPDRP_INSTALL_STATE, 0, installState, MAX_DEV_LEN, IntPtr.Zero); if (!resHardwareInstallState) { } else { entity.InstallState = installState.ToString(); } list.Add(entity); } SetupDiDestroyDeviceInfoList(hDevInfo); } return(list); } else { return(null); } }
/// <summary> /// Returns a friendly description of the device /// </summary> /// <returns></returns> public override string ToString() { return($"Name:{DeviceName} ID:{DeviceID.ToString().Substring(0, 8)} SenderType:{SenderType}"); }