public static string forshow(string msg)//消息展示 { string tmp; byte[] tmp2; tmp2 = StringByteHelper.StringToBytes(msg); tmp = Encoding.GetEncoding("gb2312").GetString(tmp2, 5, tmp2.Length - 6); return(tmp); }
public override void SendMsg(String msg) { if (mySocket != null) { try { mySocket.Send(StringByteHelper.StringToBytes(msg)); deviceManager.sendMsg(this, msg); } catch (Exception ex) { Console.WriteLine("{0} Exception caught.", ex); } } }
public static ModbusMessage decodeModbusMessage(String msg)//读取消息 { ModbusMessage res = new ModbusMessage(); byte[] cmd = StringByteHelper.StringToBytes(msg); if (!check_modbus_message(cmd)) { return(null); } res.Dev = cmd[3]; res.MsgType = ModbusMessage.byteToMessageType(cmd[4]); int len = cmd[2] - 3; byte[] data = new byte[len]; for (int i = 0; i < len; i++) { data[i] = cmd[i + 5]; } String s = Encoding.Default.GetString(data); Hashtable map = new Hashtable(); String[] words = s.Split(';'); String[] parts; for (int i = 0; i < words.Length; i++) { if (words[i].Length == 0) { continue; } parts = words[i].Split('='); if (parts.Length != 2) { return(null); } map.Add(parts[0], parts[1]); } res.Data = map; return(res); }