/// <summary> /// 设置器件信息 /// </summary> /// <param name="list">器件列表</param> /// <param name="type">控制器类型</param> /// <returns>是否成功</returns> public bool SetDeviceSetup(List <DeviceInfoBase> list, int deviceCount, ControllerType type) { bool isSuccess = false; try { if (!IsConnected) { return(false); } List <NTP> ntpList = new List <NTP>(); foreach (var item in list) { NTP ntp = NTPBuildRequest.GetSetDeviceInfoRequest(item, deviceCount, type); if (ntp != null) { ntpList.Add(ntp); } } isSuccess = SerialClient.ExecuteSetTask(ntpList, type); } catch (Exception ex) { logger.Error(ex.Message, ex); } return(isSuccess); }
/// <summary> /// 执行步骤 /// </summary> private static void ExecuteStep() { NTP request = null; switch (CurrentExecuteState) { case ExecuteState.SetInvalidType: //获取控制器类型 request = NTPBuildRequest.GetControllerTypeRequest(); SendCommand(request); break; case ExecuteState.SetOver: //单步完成 发送 发送完毕 指令 request = NTPBuildRequest.GetSendOverRequest(); SendCommand(request); break; case ExecuteState.SetState: //发送请求指令 如果没有请求 结束 if (CurrentNTPList != null && CurrentNTPList.Count > 0) { request = CurrentNTPList[0]; SendCommand(request); } else { CurrentFinishState = FinishState.Succeessed; CurrentExecuteState = ExecuteState.Untreated; } break; } }
/// <summary> /// 接受到消息回应 /// </summary> /// <param name="ntp"></param> private static void conn_ReceiveMsgEvent(NTP ntp) { try { lock (SerialLocker) { switch (CurrentExecuteState) { case ExecuteState.SetInvalidType: case ExecuteState.SetState: case ExecuteState.SetOver: ExecuteSet(ntp); break; case ExecuteState.GetInvalidType: case ExecuteState.GetState: ExecuteGet(ntp); break; case ExecuteState.Untreated: default: //不处理任何事情 break; } } } catch (Exception ex) { logger.Error(ex.Message, ex); } }
/// <summary> /// 设置手控盘信息 /// </summary> /// <param name="list">手控盘列表</param> /// <param name="type">控制器类型</param> /// <returns>是否成功</returns> public bool SetManualBoardSetup(List <ManualControlBoard> list, ControllerType type) { if (!IsConnected) { return(false); } List <NTP> ntpList = new List <NTP>(); foreach (var item in list) { NTP ntp = NTPBuildRequest.GetSetManualBoardRequest(item, type); if (ntp != null) { ntpList.Add(ntp); } } return(SerialClient.ExecuteSetTask(ntpList, type)); }
/// <summary> /// 设置控制器信息 /// </summary> /// <param name="model">控制器信息</param> /// <param name="type">控制器类型</param> /// <returns>是否成功</returns> public bool SetMachineSetup(ControllerModel model, ControllerType type) { if (!IsConnected) { return(false); } List <NTP> ntpList = new List <NTP>(); //器件 int deviceCount = 0; foreach (var loop in model.Loops) { deviceCount = loop.DeviceAmount; List <DeviceInfo8053> list = loop.GetDevices <DeviceInfo8053>(); foreach (var item in list) { NTP ntp = NTPBuildRequest.GetSetDeviceInfoRequest(item, deviceCount, type); if (ntp != null) { ntpList.Add(ntp); } } } //标准组态 foreach (var standard in model.StandardConfig) { NTP ntp = NTPBuildRequest.GetSetStandardConfigRequest(standard, type); if (ntp != null) { ntpList.Add(ntp); } } //混合组态 foreach (var mixed in model.MixedConfig) { NTP ntp = NTPBuildRequest.GetSetMixedConfigRequest(mixed, type); if (ntp != null) { ntpList.Add(ntp); } } //通用组态 foreach (var general in model.GeneralConfig) { NTP ntp = NTPBuildRequest.GetSetGeneralConfigRequest(general, type); if (ntp != null) { ntpList.Add(ntp); } } //手控盘 foreach (var manual in model.ControlBoard) { NTP ntp = NTPBuildRequest.GetSetManualBoardRequest(manual, type); if (ntp != null) { ntpList.Add(ntp); } } return(SerialClient.ExecuteSetTask(ntpList, type)); }
/// <summary> /// 接收到消息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ReadComplete(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { if (sender.GetType() != typeof(System.IO.Ports.SerialPort)) { return; } System.IO.Ports.SerialPort comPort = (System.IO.Ports.SerialPort)sender; try { comPort.ReceivedBytesThreshold = comPort.ReadBufferSize; int bytesRead = comPort.BytesToRead; if (bytesRead == 0) { comPort.ReceivedBytesThreshold = 1; return; } else { byte[] byteMessage = new byte[bytesRead]; comPort.Read(byteMessage, 0, byteMessage.Length); logger.Warn(string.Format("Receive Cmd:{0}", BytesHelper.BytesToHexStr(byteMessage))); if (receiveMessageInfo.OverReceiveBytes != null && receiveMessageInfo.OverReceiveBytes.Length > 0) { receiveMessageInfo.TempReceiveBytes = new byte[receiveMessageInfo.OverReceiveBytes.Length + bytesRead]; Array.Copy(receiveMessageInfo.OverReceiveBytes, 0, receiveMessageInfo.TempReceiveBytes, 0, receiveMessageInfo.OverReceiveBytes.Length); Array.Copy(byteMessage, 0, receiveMessageInfo.TempReceiveBytes, receiveMessageInfo.OverReceiveBytes.Length, bytesRead); } else { receiveMessageInfo.TempReceiveBytes = new byte[bytesRead]; Array.Copy(byteMessage, receiveMessageInfo.TempReceiveBytes, bytesRead); } receiveMessageInfo.IsCanRunNext = true; while (receiveMessageInfo.IsCanRunNext) { if (receiveMessageInfo.TempReceiveBytes == null) { receiveMessageInfo.TempReceiveBytes = receiveMessageInfo.OverReceiveBytes; receiveMessageInfo.OverReceiveBytes = null; } NTPParseReceiveMsg.ParseResponseMessage(receiveMessageInfo); if (receiveMessageInfo.IsReadNTPFinished) { NTP ntp = receiveMessageInfo.ntp; receiveMessageInfo.ntp = new NTP(); receiveMessageInfo.IsReadNTPFinished = false; receiveMessageInfo.IsReadNTPHeaderFinished = false; bool check = ntp.CheckXORSUM(); if (check) { if (ReceiveMsgEvent != null) { byte[] bytes = ntp.ToBytes(); ReceiveMsgEvent(ntp); } } else { logger.Error("Receive message checked error!"); } } } } } catch (Exception ex) { logger.Error(ex.Message, ex); } finally { comPort.ReceivedBytesThreshold = 1; } }
/// <summary> /// 检查类型是否正确 /// FT8000 FT8003 都是老版本7位 /// NT8001 存在老板本7位 与 新版本7位 与 新版本8位 /// NT8007 NT8021 NT8036 NT8053 都是新版本8位 /// </summary> /// <returns></returns> private static bool CheckIsValidType(NTP ntp) { bool result = false; switch (CurrentContrllerType) { case ControllerType.FT8000: if (ntp.Content != null && ntp.Content.Length >= 2) { if (ntp.Content[0] == 0x80 && ntp.Content[1] == 0x00) { result = true; } } break; case ControllerType.FT8003: if (ntp.Content != null && ntp.Content.Length >= 2) { if (ntp.Content[0] == 0x80 && ntp.Content[1] == 0x03) { result = true; } } break; case ControllerType.NT8001: if (ntp.Content != null && ntp.Content.Length >= 2) { if (ntp.Content[0] == 0x80 && (ntp.Content[1] == 0x06 || ntp.Content[1] == 0x01)) { if (ntp.Content[1] == 0x06) { CurrentControllerVersion = ControllerVersion.NewVersion8; } else if (ntp.Content[1] == 0x01 && ntp.Content.Length > 2) { CurrentControllerVersion = ControllerVersion.NewVersion7; } else { CurrentControllerVersion = ControllerVersion.OldVersion7; //老版本中不存在 最后的版本号 } result = true; } } break; case ControllerType.NT8007: if (ntp.Content != null && ntp.Content.Length >= 2) { if (ntp.Content[0] == 0x80 && ntp.Content[1] == 0x07) { result = true; } } break; case ControllerType.NT8021: if (ntp.Content != null && ntp.Content.Length >= 2) { if (ntp.Content[0] == 0x80 && ntp.Content[1] == 0x21) { result = true; } } break; case ControllerType.NT8036: if (ntp.Content != null && ntp.Content.Length >= 2) { if (ntp.Content[0] == 0x80 && ntp.Content[1] == 0x36) { result = true; } } break; case ControllerType.NT8053: if (ntp.Content != null && ntp.Content.Length >= 2) { if (ntp.Content[0] == 0x80 && ntp.Content[1] == 0x53) { result = true; } } break; } return(result); }
/// <summary> /// 发送指令 /// </summary> /// <param name="ntp">NTP协议</param> private static void SendCommand(NTP ntp) { byte[] bytes = ntp.ToBytes(); logger.Info(string.Format("Send Cmd:{0}", BytesHelper.BytesToHexStr(bytes))); conn.SendMessage(bytes); }
/// <summary> /// 执行获取指令 /// </summary> private static void ExecuteGet(NTP ntp) { NTP request = null; switch (ntp.PState) { case NTPState.Successed: case NTPState.Failed: break; case NTPState.Normal: switch (ntp.Command) { case CommandType.MachineTypeResponose: //控制器类型 if (CheckIsValidType(ntp)) { logger.Debug("machine type matched!"); CurrentExecuteState = ExecuteState.GetState; request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); } else { CurrentExecuteState = ExecuteState.Untreated; CurrentFinishState = FinishState.Failed; logger.Error("machine type unmatched!"); } break; case CommandType.Patrol: //巡检 break; case CommandType.Ready: //准备发送 logger.Debug("ready!"); CurrentExecuteState = ExecuteState.GetState; request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); break; case CommandType.DeviceUp: //器件设置 logger.Debug("device up!"); if (CurrentNTPList != null) { CurrentNTPList.Add(ntp); } CurrentExecuteState = ExecuteState.GetState; request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); break; case CommandType.StandardUp: //标准组态 logger.Debug("Standard up!"); if (CurrentNTPList != null) { CurrentNTPList.Add(ntp); } CurrentExecuteState = ExecuteState.GetState; request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); break; case CommandType.MixedUp: //混合组态 logger.Debug("Mixed up!"); if (CurrentNTPList != null) { CurrentNTPList.Add(ntp); } CurrentExecuteState = ExecuteState.GetState; request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); break; case CommandType.GeneralUp: //通用组态 logger.Debug("General up!"); if (CurrentNTPList != null) { CurrentNTPList.Add(ntp); } CurrentExecuteState = ExecuteState.GetState; request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); break; case CommandType.ManualUp: //手控盘 logger.Debug("Manual up!"); if (CurrentNTPList != null) { CurrentNTPList.Add(ntp); } CurrentExecuteState = ExecuteState.GetState; request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); break; case CommandType.Over: //发送完毕 case CommandType.Over8003: logger.Debug("Get over!"); CurrentExecuteState = ExecuteState.Untreated; CurrentFinishState = FinishState.Succeessed; request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); break; } break; } }
/// <summary> /// 执行设置指令 /// </summary> private static void ExecuteSet(NTP ntp) { switch (ntp.PState) { case NTPState.Successed: switch (CurrentExecuteState) { case ExecuteState.SetState: //移除已经发送的指令 等待巡检 发送 发送完毕 CurrentExecuteState = ExecuteState.SetOver; break; case ExecuteState.SetOver: //等待巡检 发送下一条指令 if (CurrentNTPList != null && CurrentNTPList.Count > 0) { CurrentNTPList.RemoveAt(0); } if (CurrentNTPList != null && CurrentNTPList.Count > 0) { CurrentExecuteState = ExecuteState.SetState; } else { CurrentFinishState = FinishState.Succeessed; CurrentExecuteState = ExecuteState.Untreated; } break; } break; case NTPState.Failed: if (ErrorMaxCount <= 0) //暂未使用 { CurrentExecuteState = ExecuteState.Untreated; CurrentFinishState = FinishState.Failed; logger.Error("控制器验证错误数量太多!"); } break; case NTPState.Normal: switch (ntp.Command) { case CommandType.MachineTypeResponose: //控制器类型 if (CheckIsValidType(ntp)) { logger.Debug("machine type matched!"); CurrentExecuteState = ExecuteState.SetState; NTP request = NTPBuildRequest.GetConfirmRequest(true); SendCommand(request); } else { CurrentExecuteState = ExecuteState.Untreated; CurrentFinishState = FinishState.Failed; logger.Error("machine type unmatched!"); } break; case CommandType.Patrol: //巡检 ExecuteStep(); break; } break; } }