//定时器 public void CommTimerCall(object source, System.Timers.ElapsedEventArgs e) { if (!bOpen) { return; } if (CommStateE == ECommSatate.Unknown) { return; } try { DelayTime += 100; if (DelayTime >= Math.Max(LastSendMsg.Delay_Time, 400)) //超时 { SaveDebugMsg("延时"); DelayTime = 0; present_MsgFailRep++; //子站重发次数+1 if (present_MsgFailRep > 2) //超过最大重发次数 { present_MsgFailRep = 0; //子站重发次数置0 present_DevFailNum++; //子站失败次数置+1 if (present_DevFailNum > 3) //超过最大失败次数 { if (CommStateE != ECommSatate.Failure) { SaveDebugMsg("故障"); CommStateE = ECommSatate.Failure;//子站置通信故障 CAlarmMsgEventArgs ee = new CAlarmMsgEventArgs(); ee.Date_Time = DateTime.Now; ee.Recorder = Description + "通信故障"; ee.priority = EAlarmPriority.PRIORITY_1; ee.eAlarmType = EAlarmType.StationState; ee.StaName = Name; staAlarm.OnAlarmEvent(ee); } present_DevFailNum = 0; GetSendMsg();//失败次数没达到,继续发送下个子站报文 } else { GetSendMsg();//失败次数没达到,继续发送下个子站报文 } } else { Write(); } } } catch (Exception e1) { ListStrMsg.Add("CProtcolModbusTCP.CommTimerCall:" + Name + e1.Message); Debug.WriteLine("CProtcolModbusTCP.CommTimerCall:" + Name + e1.Message); } //Debug.WriteLine("Send"); }
public bool PortDataRecv(CMessage sSend, byte[] cRecv, int iRecvLen) { try { if (iRecvLen < 12) { ListStrMsg.Add("PortDataRecv:False:iRecvLen < 12"); return(false); } for (int i = 0; i < iRecvLen - 11; i++) { if (cRecv[i] == FINSTCPHeader[0] && cRecv[i + 1] == FINSTCPHeader[1] && cRecv[i + 2] == FINSTCPHeader[2] && cRecv[i + 3] == FINSTCPHeader[3]) { int iLen = cRecv[i + 4] * 256 * 256 * 256 + cRecv[i + 5] * 256 * 256 + cRecv[i + 6] * 256 + cRecv[i + 7]; if (iRecvLen - i < iLen + 8) { ListStrMsg.Add("PortDataRecv:False:iRecvLen - i < iLen + 8"); return(false); } byte[] Datebuff = new byte[iLen]; Array.Copy(cRecv, i + 8, Datebuff, 0, iLen); if (iLen + 8 > 25) { if (Datebuff[17] != SID) { ListStrMsg.Add("PortDataRecv:False:Datebuff[17] != SID"); return(false); } } GetVarValue(sSend, Datebuff); return(true); } } ListStrMsg.Add("PortDataRecv:False:for (int i = 0; i < iRecvLen - 11; i++)"); return(false); } catch (Exception ex) { ListStrMsg.Add("CProtcolFINS.PortDataRecv:" + ex.Message); Debug.WriteLine("CProtcolFINS.PortDataRecv:" + ex.Message); string sShow = ""; for (int i = 0; i < iRecvLen; i++) { sShow += cRecv[i].ToString("X2") + " "; } ListStrMsg.Add(DateTime.Now.ToLongTimeString() + "Err:" + iRecvLen.ToString() + ":" + sShow); Debug.WriteLine(DateTime.Now.ToLongTimeString() + "Err:" + iRecvLen.ToString() + ":" + sShow); return(false); } }
//写报文到以太网口 public virtual bool Write(SSend_Message sSend) { if (CommStateE != ECommSatate.Unknown) {//写串口数据 try { //n_SerialPort.WriteLine(sSendData); netstream.Write(sSend.DataBuffer, 0, sSend.Length); if (bDebug) { string sShow1 = ""; for (int i = 0; i < LastSendMsg.sSendMsg.Length; i++) { sShow1 += LastSendMsg.sSendMsg.DataBuffer[i].ToString("X2") + " "; } frmShow.InsertText(sShow1, "PLC;Send", LastSendMsg.sSendMsg.Length); } string sShow = ""; for (int i = 0; i < sSend.Length; i++) { sShow += sSend.DataBuffer[i].ToString("X2") + " "; } //if(sSend.DataBuffer[27]!=1) string strAdd = " "; if (sSend.Length > 30) { strAdd += ((sSend.DataBuffer[29] << 8) + sSend.DataBuffer[30]).ToString("0000"); } sShow = DateTime.Now.ToLongTimeString() + "PLCSend:" + sShow + " (" + sSend.Length.ToString() + ")" + strAdd; //Debug.WriteLine(sShow); ListStrMsg.Add(sShow); } catch (Exception) { return(false); } } return(true); }
public virtual bool Write() { if (CommStateE != ECommSatate.Unknown) {//写串口数据 try { if (LastSendMsg == null) { return(false); } //n_SerialPort.WriteLine(sSendData); netstream.Write(LastSendMsg.sSendMsg.DataBuffer, 0, LastSendMsg.sSendMsg.Length); if (bDebug) { string sShow1 = ""; for (int i = 0; i < LastSendMsg.sSendMsg.Length; i++) { sShow1 += LastSendMsg.sSendMsg.DataBuffer[i].ToString("X2") + " "; } frmShow.InsertText(sShow1, "ModbusTCP;Send", LastSendMsg.sSendMsg.Length); } string sShow = ""; for (int i = 0; i < LastSendMsg.sSendMsg.Length; i++) { sShow += LastSendMsg.sSendMsg.DataBuffer[i].ToString("X2") + " "; } sShow = DateTime.Now.ToLongTimeString() + "ModbusTCPSend:" + sShow + " (" + LastSendMsg.sSendMsg.Length.ToString() + ")"; //Debug.WriteLine(sShow); ListStrMsg.Add(sShow); } catch (Exception e1) { ListStrMsg.Add("CProtcolModbusTCP.Write:" + Name + e1.Message); return(false); } } return(true); }
//通信线程 private void CTPortThread() { while (true) { try { if (ListStrMsg.Count > ListStrMsgMax) { //SaveDebugMsg("正常"); ListStrMsg.RemoveRange(0, ListStrMsgMax / 5); } if (!client.Connected) { bOpen = false; CommStateE = ECommSatate.Unknown; bOpen = ConnectServer(); if (!bOpen) { continue; } } if (netstream.CanRead) { byte[] buf = new byte[1024]; int iNum = 0; int iLength = 0; do { iNum = netstream.Read(buf, iLength, buf.Length - iLength); iLength += iNum; }while (netstream.DataAvailable || iLength > 1024); if (iLength > 0) { if (bDebug) { string sShow1 = ""; for (int i = 0; i < iLength; i++) { sShow1 += buf[i].ToString("X2") + " "; } frmShow.InsertText(sShow1, "PLC;Recv", iLength); Thread.Sleep(50); } string sShow = ""; for (int i = 0; i < Math.Min(iLength, 34); i++) { sShow += buf[i].ToString("X2") + " "; } sShow = DateTime.Now.ToLongTimeString() + "PLCRecv:" + sShow + " (" + iLength.ToString() + ")"; // Debug.WriteLine(sShow); ListStrMsg.Add(sShow); if (PortDataRecv(LastSendMsg, buf, iLength)) { GetSendMsg(); } } } } catch (Exception e) { Debug.WriteLine("CProtcolFINS.CTPortThread:" + e.Message); } Thread.Sleep(100); } }
public bool PortDataRecv(CMessage sSend, byte[] cRecv, int iRecvLen) { // 00 01 02 03 04 05 06 07 08 09 10 11 //(012)00 07 00 00 00 06 03 03 00 41 00 40 //(137)00 07 00 00 00 83 03 03 80 00 55 00 72 00 77 ....... //(012)01 2F 00 00 00 06 06 02 00 00 00 99 //(029)01 2F 00 00 00 17 06 02 14 EC 9B CA 85 8E 35 EA 5D D7 5F 27 C3 A5 08 8E 3C F1 39 25 00 //(012)49 A4 00 00 00 06 05 05 00 6E FF 00 //(012)49 A4 00 00 00 06 05 05 00 6E FF 00 //(012)49 E5 00 00 00 06 05 06 00 11 00 00 //(012)49 E5 00 00 00 06 05 06 00 11 00 00 try { if (iRecvLen < 10) { ListStrMsg.Add("PortDataRecv:false:iRecvLen < 10"); return(false); } //Debug.WriteLine(iSendNum.ToString()); for (int i = 0; i < iRecvLen - 10; i++) { if (cRecv[i] * 256 + cRecv[i + 1] == iSendNum) { int iLen = cRecv[i + 5]; if (iRecvLen - i < iLen + 6) { ListStrMsg.Add("PortDataRecv:false:iRecvLen - i < iLen + 6"); return(false); } if (CommStateE == ECommSatate.Failure) { SaveDebugMsg("正常"); CommStateE = ECommSatate.Normal;//子站置通信恢复 CAlarmMsgEventArgs ee = new CAlarmMsgEventArgs(); ee.Date_Time = DateTime.Now; ee.Recorder = Description + "通信恢复"; ee.priority = EAlarmPriority.PRIORITY_1; ee.eAlarmType = EAlarmType.StationState; ee.StaName = Name; staAlarm.OnAlarmEvent(ee); } else if (CommStateE != ECommSatate.Normal) { SaveDebugMsg("正常"); CommStateE = ECommSatate.Normal;//子站置通信恢复 } DelayTime = 0; present_MsgFailRep = 0; present_DevFailNum = 0; byte[] Datebuff = new byte[iLen]; Array.Copy(cRecv, i + 6, Datebuff, 0, iLen); GetVarValue(sSend, Datebuff); return(true); } } ListStrMsg.Add("PortDataRecv:false:cRecv[i] * 256 + cRecv[i + 1] == iSendNum"); return(false); } catch (Exception ex) { ListStrMsg.Add("CProtcolModbusTCP.PortDataRecv:" + ex.Message); Debug.WriteLine("CProtcolModbusTCP.PortDataRecv:" + ex.Message); string sShow = ""; for (int i = 0; i < iRecvLen; i++) { sShow += cRecv[i].ToString("X2") + " "; } ListStrMsg.Add(DateTime.Now.ToLongTimeString() + "Err:" + iRecvLen.ToString() + ":" + sShow); Debug.WriteLine(DateTime.Now.ToLongTimeString() + "Err:" + iRecvLen.ToString() + ":" + sShow); return(false); } }