/// <summary> /// /// </summary> private OutputModule Send(InputModule input, bool bIsRet = false) { m_output.nStartAddr = input.nStartAddr; m_output.bySlaveID = input.bySlaveID; m_output.nDataLength = input.nDataLength; m_output.byRecvData = null; OutputModule output = null; #region 功能码检测 bool bReadOrWriteReg; if ((input.byFunction == byREAD_COIL) || (input.byFunction == byREAD_DISCRETE_INPUTS) || (input.byFunction == byREAD_HOLDING_REG) || (input.byFunction == byREAD_INPUT_REG)) { bReadOrWriteReg = true; } else if ((input.byFunction == byWRITE_SINGLE_COIL) || (input.byFunction == byWRITE_MULTI_HOLDING_REG) || (input.byFunction == byWRITE_MULTI_COILS) || (input.byFunction == byWRITE_SINGLE_HOLDING_REG)) { bReadOrWriteReg = false; } else { if (event_MessageText != null) { event_MessageText("检测到不支持的功能码!", emMsgType.Error); } if (!bIsRet) { m_Event_SendMsg.Set(); } return(output); } #endregion try { switch (m_nRunMode) { #region RTU case emCommMode.RTU: if (bReadOrWriteReg) { output = SendMessage_SP(CreateReadHeader_RTU(input), bIsRet); } else { output = SendMessage_SP(CreateWritrHeader_RTU(input), bIsRet); } break; #endregion #region TCP case emCommMode.TCP: if (bReadOrWriteReg) { output = SendMessage_TCP_Sync(CreateReadMsg_TCP(input), bIsRet); } else { output = SendMessage_TCP_Sync(CreateWriteMsg_TCP(input), bIsRet); } break; #endregion default: break; } } catch (System.Exception ex) { if (event_MessageText != null) { event_MessageText(ex.Message, emMsgType.Error); } } if (!bIsRet) { m_Event_SendMsg.Set(); } return(output); }
//private string SendMessage_TCP_Async(byte[] bySend) //{ // try // { // SocketError socketErr; // m_socketTcp.BeginSend(bySend, 0, bySend.Length, SocketFlags.None, out socketErr, new AsyncCallback(OnSend), null); // if (event_MessageText != null) // { // //string str = BitConverter.ToString(bySend); // event_MessageText(BytesToStr(bySend), emMsgType.Send); // } // m_socketTcp.BeginReceive(m_byTCPDataRecv, 0, m_byTCPDataRecv.Length, SocketFlags.None, new AsyncCallback(OnReceive), m_socketTcp); // } // catch (Exception ex) // { // return ex.Message; // } // return null; //} //private void OnSend(IAsyncResult result) //{ // if (result.IsCompleted == false) // { // if (event_MessageText != null) // { // event_MessageText("发送失败!", emMsgType.Error); // } // } //} //private void OnReceive(IAsyncResult result) //{ // if (result.IsCompleted == false) // { // if (event_MessageText != null) // { // event_MessageText("接收失败!", emMsgType.Error); // } // } // else // { // m_output.byRecvData = m_byTCPDataRecv; // if (event_DataReceive != null) // { // event_DataReceive(m_output); // } // if (event_MessageText != null) // { // //string str = BitConverter.ToString(m_byTCPDataRecv); // event_MessageText(BytesToStr(m_byTCPDataRecv), emMsgType.Recv); // } // m_Event_SendMsg.Set(); // } //} #endregion /// <summary> /// TCP 同步发送数据 /// </summary> private OutputModule SendMessage_TCP_Sync(byte[] bySend, bool bIsSync = false) { OutputModule output = null; if (m_socketTcp == null) { if (event_MessageText != null) { event_MessageText("检测到套接字处于关闭状态,正在尝试重连...", emMsgType.Error); } if (!TCPconnect(m_IP, m_nPort)) { if (event_MessageText != null) { event_MessageText("重连失败!无法发送消息!", emMsgType.Error); } return(output); } } int nCount = 1; //string strRet = null; do { try { Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + "\t发送:" + BitConverter.ToString(bySend)); m_socketTcp.Send(bySend); if (event_MessageText != null) { event_MessageText(BytesToStr(bySend), emMsgType.Send); } m_socketTcp.Receive(m_byTCPDataRecv); Console.WriteLine(DateTime.Now.ToString("HH:mm:ss:fff") + "\t接收:" + BitConverter.ToString(m_byTCPDataRecv) + "\r\n"); if (bySend[7] != m_byTCPDataRecv[7]) { Console.WriteLine("异常时间点:" + DateTime.Now.ToString("HH:mm:ss:fff")); throw new FunctionErr(string.Format("功能码不一致 发送功能码:{0} 接收功能码:{1}", bySend[7], m_byTCPDataRecv[7])); } m_output.byFunction = m_byTCPDataRecv[7]; if ((m_output.byFunction == (byte)emFunction.ReadCoil) || (m_output.byFunction == (byte)emFunction.ReadDiscreteInputs) || (m_output.byFunction == (byte)emFunction.ReadHoldReg) || (m_output.byFunction == (byte)emFunction.ReadInputReg) ) { m_output.nDataLength = (int)m_byTCPDataRecv[8]; } m_output.byRecvData = m_byTCPDataRecv; if ((event_DataReceive != null) && (!bIsSync)) { event_DataReceive(m_output); } if (event_MessageText != null) { event_MessageText(BytesToStr(m_byTCPDataRecv), emMsgType.Recv); } output = m_output; break; } #region ObjectDisposedException catch (ObjectDisposedException ex)//socket 关闭 { if (event_MessageText != null) { event_MessageText(ex.Message, emMsgType.Error); } if (m_socketTcp != null) { m_socketTcp.Dispose(); m_socketTcp = null; } if (TCPconnect(m_IP, m_nPort)) { continue; } else { if (event_MessageText != null) { event_MessageText("消息发送失败,已断开连接!", emMsgType.Error); } break; } } #endregion #region ArgumentNullException catch (ArgumentNullException ex)//buffer为空 { if (event_MessageText != null) { event_MessageText(ex.Message, emMsgType.Error); } break; } #endregion #region FunctionException catch (FunctionErr fe) { if (event_MessageText != null) { event_MessageText(fe.Message, emMsgType.Error); } } #endregion #region SocketException catch (SocketException ex)//访问socket出错 { if (event_MessageText != null) { event_MessageText(ex.Message, emMsgType.Error); } if (m_socketTcp != null) { m_socketTcp.Dispose(); m_socketTcp = null; } if (isTcpConnected && TCPconnect(m_IP, m_nPort)) { continue; } else { if (event_MessageText != null) { event_MessageText("消息发送失败!已断开连接!", emMsgType.Error); } break; } } #endregion #region ArgumentOutOfRangeException catch (ArgumentOutOfRangeException ex)//越界 { if (event_MessageText != null) { event_MessageText(ex.Message, emMsgType.Error); } break; } #endregion }while (++nCount <= m_nReSendTimes); return(output); }