private static void GM02_ListenClientConnect() { while (true && socket_GM02 != null && socket_GM02.Connected) { int receiveLength = socket_GM02.Receive(result_GM02); if (receiveLength <= 0) { continue; } XDCMessage msgContent = XDCUnity.MessageFormat.Format(result_GM02, receiveLength, TcpHead.L2L1); string msg = msgContent.MsgASCIIString; if (!string.IsNullOrEmpty(msg.TrimEnd('\0'))) { while (true) { //与eCAT连接了,并且,GM01已经进入服务了 if (myClientSocket != null && myClientSocket.Connected && GM01_HostState == HostState.InService) { SendMsgToeCATEvent_2(myClientSocket, msgContent); break; } else { ReceiveMsg_Unknow("GM02-Connectting to eCAT..."); Thread.Sleep(1000); } } } } }
private static void GM01_ListenClientConnect() { while (true && socket_GM01 != null && socket_GM01.Connected) { int receiveLength = socket_GM01.Receive(result_GM01); XDCMessage msgContent = XDCUnity.MessageFormat.Format(result_GM01, receiveLength, TcpHead.L2L1); string msg = msgContent.MsgASCIIString; if (!string.IsNullOrEmpty(msg.TrimEnd('\0'))) { while (true) { if (myClientSocket != null) { if (msgContent.MsgCommandType == MessageCommandType.GoInService && GM01_HostState != HostState.InService) { //这条1消息代表GM01要进入服务了,但是此时不能进入,要等GM02就位了才能进入。 GM01_HostState = HostState.WaitForReadyToInservice; } SendMsgToeCATEvent(myClientSocket, msgContent); break; } else { ReceiveMsg_Unknow("GM01-Connectting to eCAT..."); Thread.Sleep(1000); } } } } }
private void BeginPars() { isAlreadyLoad = false; XDCProtocolType pType = rb_NDC.Checked ? XDCProtocolType.NDC : XDCProtocolType.DDC; DataType dType = rb_State.Checked ? DataType.State : (rb_Screen.Checked ? DataType.Screen : (rb_Fit.Checked ? DataType.Fit : DataType.Message)); BaseFunction.Intial(pType, dType); string parsText = rtb_Msg.Text;//.Trim(); if (string.IsNullOrEmpty(parsText)) { ResetFields(); return; } List <ParsRowView> view = null; switch (XDCUnity.CurrentDataType) { case DataType.State: { view = XDCUnity.StateOperator.GetView(parsText); } break; case DataType.Screen: break; case DataType.Fit: { view = XDCUnity.FitOperator.GetView(parsText); } break; case DataType.Message: { byte[] msgByteArray = Encoding.ASCII.GetBytes(parsText); XDCMessage XDCmsg = XDCUnity.MessageFormat.Format(msgByteArray, parsText.Length, TcpHead.NoHead, true); view = XDCUnity.MessageOperator.GetView(XDCmsg); } break; default: break; } if (null != view) { dgv_Fileds.DataSource = view; } else { ResetFields(); } dgv_Fileds.Columns[0].FillWeight = 35; dgv_Fileds.Columns[1].FillWeight = 30; dgv_Fileds.Columns[2].FillWeight = 35; dgv_Fileds.Columns[1].HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; dgv_Fileds.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; dgv_Fileds.ClearSelection(); }
void Program_SendMsgToeCATEvent(object socket, XDCMessage msg) { Socket tempSock = socket as Socket; if (tempSock != null) { string headContext = string.Empty; byte[] msgBytes = XDCUnity.EnPackageMsg(msg.MsgASCIIString, TcpHead.L2L1, ref headContext); tempSock.Send(msgBytes); this.lsb_Log_GM01.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + " :" + "Send(" + (msg.MsgASCIIString.Length).ToString().PadLeft(4, '0') + ") : " + msg.MsgASCIIString); } this.lsb_Log_GM01.TopIndex = lsb_Log_GM01.Items.Count - (int)(lsb_Log_GM01.Height / lsb_Log_GM01.ItemHeight); }
/// <summary> /// 根据消息类型获取对应的消息格式配置 /// </summary> /// <param name="xdcMsg"></param> /// <returns></returns> private static string GetXMLConfig(XDCMessage xdcMsg) { string result = string.Empty; string xmlName = string.Empty; string xmlPath = string.Empty; string xx = xdcMsg.MsgType.ToString(); switch (xdcMsg.MsgType) { case MessageType.DataCommand: break; case MessageType.TerminalComman: { xmlName = "MessagePars"; xmlPath = "MessagePars.TerminalCommand.[*].{*}"; result = XMLHelper.instance.XMLFiles[xmlName].GetXmlAttributeValue(xmlPath); } break; case MessageType.SolicitedMessage_Status: break; case MessageType.UnSolicitedMessage_Status: { xmlName = "MessagePars"; xmlPath = "MessagePars.UnSolicitedMessage." + xdcMsg.Identification + ".[*].{*}"; result = XMLHelper.instance.XMLFiles[xmlName].GetXmlAttributeValue(xmlPath); } break; case MessageType.SolicitedMessage_Encryptor: break; case MessageType.Unknow: break; default: break; } return(result); }
/// <summary> /// 根据消息格式配置获取显示视图 /// </summary> /// <param name="RowXml"></param> /// <param name="XDCmsg"></param> /// <returns></returns> public static List <MessageParsRowView> GetView(XDCMessage XDCmsg) { string RowXml = GetXMLConfig(XDCmsg); if (string.IsNullOrEmpty(RowXml)) { return(null); } string[] fields = RowXml.Split(';'); List <MessageParsRowView> mprvlist = new List <MessageParsRowView>(); for (int i = 0; i < XDCmsg.MsgASCIIStringFields.Length; i++) { foreach (string item in fields) { string[] itemArray = item.TrimStart('[').TrimEnd(']').Split(','); int FSIndex = int.Parse(itemArray[2]); int CharIndex = int.Parse(itemArray[3]); int Length = int.Parse(itemArray[4]); if (FSIndex == i) { MessageParsRowView mprv = new MessageParsRowView(); mprv.FieldName = itemArray[0]; try { if (Length > -1) { mprv.FieldValue = XDCmsg.MsgASCIIStringFields[FSIndex].Substring(CharIndex, Length); } else { //var的情况,不确定长度的 string tempStr = XDCmsg.MsgASCIIStringFields[FSIndex]; mprv.FieldValue = tempStr.Substring(CharIndex, tempStr.Length - CharIndex); // XDCmsg.MsgASCIIStringFields[FSIndex].Substring(CharIndex, Length); } } catch { //错误则不处理。 } #region 关联备注 mprv.FieldComment = itemArray[1]; if (mprv.FieldComment.Length > 0) { if (mprv.FieldComment.StartsWith("[") && mprv.FieldComment.EndsWith("]")) { string key = mprv.FieldComment.TrimStart('[').TrimEnd(']'); if (GlobalConfig.CommentDic.ContainsKey(key)) { Dictionary <string, string> CurStatus = GlobalConfig.CommentDic[key]; if (null != mprv.FieldValue && CurStatus.ContainsKey(mprv.FieldValue)) { mprv.FieldComment = CurStatus[mprv.FieldValue]; } } } else if (mprv.FieldComment.StartsWith("{") && mprv.FieldComment.EndsWith("}")) { //内关联情况 string key = mprv.FieldComment.TrimStart('{').TrimEnd('}') + "-" + mprvlist[mprvlist.Count - 1].FieldValue; if (GlobalConfig.CommentDic.ContainsKey(key)) { Dictionary <string, string> CurStatus = GlobalConfig.CommentDic[key]; if (null != mprv.FieldValue && CurStatus.ContainsKey(mprv.FieldValue)) { mprv.FieldComment = CurStatus[mprv.FieldValue]; } else if (null == mprv.FieldValue && CurStatus.ContainsKey("")) { mprv.FieldComment = CurStatus[""]; } else { mprv.FieldComment = "Error Data"; } } else { mprv.FieldComment = ""; } } } #endregion mprvlist.Add(mprv); } } if (i != XDCmsg.MsgASCIIStringFields.Length - 1) { mprvlist.Add(new MessageParsRowView("FS", "", "")); } } return(mprvlist); }
public static XDCMessage MessageFormat(byte[] msgByte, int msgLength) { XDCMessage result = new XDCMessage(); //0.原字节数组 result.MsgByteArray = msgByte; //1.Base64字符串 result.MsgBase64String = Convert.ToBase64String(msgByte, 0, msgLength); //2.ASCII字符串 result.MsgASCIIString = Encoding.ASCII.GetString(msgByte, 0, msgLength); char MsgFS = '\u001C';//域分隔符 string[] msgFields = result.MsgASCIIString.Split(MsgFS); //3.分解消息得各域的字符数组 result.MsgASCIIStringFields = msgFields; //4.标识 if (msgFields.Length > 3 && msgFields[3].Length > 0) { result.Identification = msgFields[3].Substring(0, 1); } //5.消息类别 string tempField_0 = msgFields[0].Length > 3 ? msgFields[0].Substring(2, msgFields[0].Length - 2) : msgFields[0]; result.MsgType = ParsMessageClass(tempField_0); string msgResult = string.Empty; result.MsgCommandType = MessageCommandType.Unknow; #region 5.判断消息类型 switch (result.Identification) { case "1": { if (result.MsgType == MessageType.TerminalComman) { result.MsgCommandType = MessageCommandType.GoInService; } } break; case "2": { if (result.MsgType == MessageType.TerminalComman) { result.MsgCommandType = MessageCommandType.GoOutOfService; } } break; case "9": { if (result.MsgType == MessageType.SolicitedMessage_Status) { result.MsgCommandType = MessageCommandType.ReadyB; } } break; case "B": { if (result.MsgType == MessageType.UnSolicitedMessage_Status) { if (msgFields[3].Equals("B0000")) { result.MsgCommandType = MessageCommandType.FullDownLoad; } else { result.MsgCommandType = MessageCommandType.NotFullDownLoad; } NeedSendToBothHost.Enqueue(result.MsgBase64String); } } break; case "E": { if (result.MsgType == MessageType.UnSolicitedMessage_Status) { //cash handle result.MsgCommandType = MessageCommandType.CashHandler; NeedSendToBothHost.Enqueue(result.MsgBase64String); } } break; case "": { result.MsgCommandType = MessageCommandType.TransactionMessage; } break; default: break; } #endregion //6.LUNO if (msgFields[1].Length > 0) { if (msgFields[1].Length > 6) { result.LUNO = msgFields[1].Substring(0, 6); } else { result.LUNO = msgFields[1]; } } return(result); }
private void ReadLogs(string path) { StreamReader sr = new StreamReader(path, Encoding.Default); String line; string preMsgHead = string.Empty; OperationCodeObject opcObject = null; string tempLine = string.Empty; OperationDic.Clear(); while ((line = sr.ReadLine()) != null) { tempLine = ""; //3类消息,交易请求、交互响应、交易回复 if (!IsMsgWeNeed(preMsgHead) && !line.StartsWith("TRANSMIT: 11")) { continue; } byte[] msgByteArray = null; XDCMessage XDCmsg = null; if (line.StartsWith("TRANSMIT: 11")) { tempLine = line.Replace("TRANSMIT: ", "").TrimStart(); msgByteArray = Encoding.ASCII.GetBytes(tempLine); XDCmsg = XDCUnity.MessageFormat.Format(msgByteArray, tempLine.Length); if (!string.IsNullOrEmpty(XDCmsg.OperationCode)) { preMsgHead = "11"; } else { //上发的交互响应消息,跳过 continue; } } else if (line.StartsWith("RECEIVE: 3") && IsMsgWeNeed(preMsgHead)) { //交互响应消息 tempLine = line.Replace("RECEIVE: ", "").TrimStart(); } else if (line.StartsWith("RECEIVE: 4") && IsMsgWeNeed(preMsgHead)) { tempLine = line.Replace("RECEIVE: ", "").TrimStart(); } // if (line.StartsWith("TRANSMIT: 11")) { if (opcObject != null && !string.IsNullOrEmpty(opcObject.Opc) && !string.IsNullOrEmpty(opcObject.ReplyMsg)) { if (OperationDic.ContainsKey(opcObject.Opc)) { OperationDic[opcObject.Opc] = opcObject; } else { OperationDic.Add(opcObject.Opc, opcObject); } } //交易请求 opcObject = null; opcObject = new OperationCodeObject(); XDCmsg = XDCUnity.MessageFormat.Format(msgByteArray, tempLine.Length); opcObject.Opc = XDCmsg.OperationCode; } else if (line.StartsWith("RECEIVE: 3")) { if (opcObject != null && string.IsNullOrEmpty(opcObject.InteractiveMsg)) { tempLine = line.Replace("RECEIVE: ", "").TrimStart(); opcObject.InteractiveMsg = tempLine; } preMsgHead = "3"; } else if (line.StartsWith("RECEIVE: 4")) { if (opcObject != null) { tempLine = line.Replace("RECEIVE: ", "").TrimStart(); msgByteArray = Encoding.ASCII.GetBytes(tempLine); XDCmsg = XDCUnity.MessageFormat.Format(msgByteArray, tempLine.Length); XDCmsg.MsgASCIIStringFields[6] = "0" + XDCmsg.MsgASCIIStringFields[6].Substring(1); tempLine = ""; foreach (string item in XDCmsg.MsgASCIIStringFields) { tempLine += item + XDCSplictorChar.FS.ToString(); } opcObject.ReplyMsg = tempLine.Substring(0, tempLine.Length - 1); preMsgHead = "4"; } } else { } } if (opcObject != null && !string.IsNullOrEmpty(opcObject.Opc)) { //最后一条有可能没有保存进去。 if (OperationDic.ContainsKey(opcObject.Opc)) { OperationDic[opcObject.Opc] = opcObject; } else { OperationDic.Add(opcObject.Opc, opcObject); } } }
/// <summary> /// 接收消息 /// </summary> /// <param name="clientSocket"></param> private static void ReceiveMessage(object clientSocket) { myClientSocket = (Socket)clientSocket; while (true) { try { result_eCAT = new byte[2048]; int receiveNumber = myClientSocket.Receive(result_eCAT); object obj = new object(); lock (obj) { if (IsLostConnectFromTerminal) { ConnectToGMEvent(); } XDCMessage msgContent = XDCUnity.MessageFormat.Format(result_eCAT, receiveNumber, TcpHead.L2L1); string msg = msgContent.MsgASCIIString; if (!string.IsNullOrEmpty(msg.TrimEnd('\0'))) { #region 解析消息,判断LUNO号 //签到消息,还没有带terminalID,只有配置文件中的LUNO if (msgContent.LUNO.Equals(LUNO_eCATBase)) { if (GM01_HostState != HostState.InService) { //签到消息,分别发送给GM01和GM02 if (socket_GM01.Connected) { ReceiveMsg_GM01(msgContent); SendMsgToGM01_Event(socket_GM01, msg); } if (GM01_HostState == HostState.WaitForReadyToInservice && msgContent.MsgCommandType == MessageCommandType.ReadyB) { GM01_HostState = HostState.InService; } } else { //发送给GM02 if (socket_GM02.Connected) { if (XDCUnity.MessageFormat.NeedSendToBothHost != null && XDCUnity.MessageFormat.NeedSendToBothHost.Count > 0) { string QueueMsg = ""; for (int i = 0; i < XDCUnity.MessageFormat.NeedSendToBothHost.Count; i++) { //消息队列里的消息是需要发送到各个主机的,如fulldownload消息,故障消息等。 QueueMsg = XDCUnity.MessageFormat.NeedSendToBothHost.Dequeue(); if (!string.IsNullOrEmpty(QueueMsg)) { XDCMessage mssg = XDCUnity.MessageFormat.Format(Encoding.ASCII.GetBytes(QueueMsg), Encoding.ASCII.GetBytes(QueueMsg).Length, TcpHead.L2L1); ReceiveMsg_GM02(mssg); SendMsgToGM02_Event(socket_GM02, QueueMsg); } } } ReceiveMsg_GM02(msgContent); SendMsgToGM02_Event(socket_GM02, msg); } } } else if (msgContent.LUNO.Equals(LUNO_GM01)) { ReceiveMsg_GM01(msgContent); SendMsgToGM01_Event(socket_GM01, msg); } else if (msgContent.LUNO.Equals(LUNO_GM02)) { ReceiveMsg_GM02(msgContent); SendMsgToGM02_Event(socket_GM02, msg); } else { //捕获到未知去向消息 ReceiveMsg_Unknow("Receive Message :" + msg + " ,But unknow whice host to send."); } #endregion } } } catch (Exception ex) { ReceiveMsg_Unknow("Disconnect From Termnal."); GM01_HostState = HostState.Unknow; DisConnectToGMEvent(); break; } } }
void Form1_ReceiveMsg_GM01(XDCMessage msg) { this.lsb_Log_GM01.Items.Add(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + " :" + "Recv(" + (msg.MsgASCIIString.Length).ToString().PadLeft(4, '0') + ") : " + msg.MsgASCIIString); this.lsb_Log_GM01.TopIndex = lsb_Log_GM01.Items.Count - (int)(lsb_Log_GM01.Height / lsb_Log_GM01.ItemHeight); }