private void Smanager_OnMsgReceived(byte[] info) { string str = BytesUtil.ToHexString(info); StringBuilder sb = new StringBuilder(); sb.Append("接收到数据:"); sb.Append("\r\n"); sb.Append(" 发送内容:" + str); sb.Append("\r\n"); SetMemoText(sb.ToString()); }
private void Listener_OnMsgReceived(AsyncUserToken token, byte[] info) { string str = BytesUtil.ToHexString(info); try { RecieveMessageDecode reader = new RecieveMessageDecode(info); RecieveMessage message = reader.Read(); StringBuilder sb = new StringBuilder(); sb.Append("接收到数据:"); sb.Append("\r\n"); sb.Append(" 来源IP:" + token.Remote.Address.ToString()); sb.Append("\r\n"); sb.Append(" 接收时间:" + DateTime.Now); sb.Append("\r\n"); sb.Append(" 数据类型:" + message.FunctionCode); sb.Append("\r\n"); sb.Append(" 接收内容:" + str); sb.Append("\r\n"); AppendLog(sb.ToString()); //更新设备缓存 UpdateCache(message.ClientCodeStr, token.UID); if (message.FunctionCode.Equals("F2")) { return; } IMessageHandler handler = HandlerFactory.Create(message.FunctionCode, token.UID, message); handler.Handle(); //如果是设备自报数据,向设备发送接收成功的报文 if (message.FunctionCode.Equals("C0")) { SendMessage res = new SendMessage(); res.ClientCode = message.ClientCode; res.CenterCode = message.CenterCode; res.SendTime = DateTime.Now; res.Serial = 0; res.FunctionCode = "C0"; listener.Send(token.UID, res.ToByte()); } } catch (Exception ex) { LogHelper.WriteLog("接收消息时出错", "接收到的消息:" + str + "\r\n" + ex.Message, ex.StackTrace); } }
private void btn_create_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(cb_type.Text)) { MessageBox.Show("请选择指令类型"); return; } if (string.IsNullOrEmpty(tb_device.Text)) { MessageBox.Show("请填入遥测站地址"); return; } string uid; Main.online.TryGetValue(tb_device.Text, out uid); if (uid == null) { MessageBox.Show("未找到在线的遥测站,请确认遥测站地址"); return; } if (!cb_type.Text.Equals("B0") && string.IsNullOrEmpty(edit_content.Text)) { MessageBox.Show("请输入修改内容"); return; } SendMessage msg = new SendMessage(); msg.CenterCode = 0x01; msg.ClientCode = BytesUtil.ToHexArray(tb_device.Text); msg.SendTime = DateTime.Now; msg.Serial = 0; msg.FunctionCode = cb_type.Text; if (!cb_type.Text.Equals("B0")) { msg.Body = BytesUtil.ToHexArray(edit_content.Text); } memoEdit1.Text = BytesUtil.ToHexString(msg.ToByte()); currentMsg = msg.ToByte(); currentUID = uid.ToString(); }
private void Listener_OnMsgReceived(AsyncUserToken token, byte[] info) { string str = BytesUtil.ToHexString(info); try { RecieveMessageDecode reader = new RecieveMessageDecode(info); RecieveMessage message = reader.Read(); MessageHandler msgHandler = new MessageHandler(token.UID, message); msgHandler.OnIPChanged += MsgHandler_OnIPChanged; msgHandler.OnMsgSend += MsgHandler_OnMsgSend; msgHandler.Set(); } catch (Exception ex) { LogHelper.WriteLog("接收消息时出错", "接收到的消息:" + str + "\r\n" + ex.Message, ex.StackTrace); } }
public SendMessage Handle(ref Operate opeate) { SendMessage msg = new SendMessage() { CenterCode = 0x01, ClientCode = BCDUtil.ConvertFrom(opeate.ClientCode, 6), SendTime = SendTime, Serial = 0, FunctionCode = opeate.FunctionCode }; msg.Body = GetBody(); opeate.State = OperateState.Sended; opeate.Content = BytesUtil.ToHexString(msg.ToByte()); opeate.SendTime = SendTime; return(msg); }