public static byte[] BuildSensorMsg(string mac, int port, SensorType sensorType, byte[] coreData) { var buffer = Unpooled.Buffer(1024); try { var settingCoreSize = 1; var detectInterval = 5; var coreSize = coreData.Length / 21; buffer.WriteByte(settingCoreSize); buffer.WriteShort(detectInterval); buffer.WriteByte(coreSize); var macBytes = BytesUtil.Hex2Bytes(mac); buffer.WriteBytes(macBytes); buffer.WriteByte(port); buffer.WriteShort((int)sensorType); buffer.WriteInt(TimestampUtil.ConvertDateTime2Seconds(DateTime.Now)); buffer.WriteBytes(coreData); var msg = new byte[buffer.ReadableBytes]; buffer.ReadBytes(msg); return(msg); } finally { buffer.Release(); } }
private void SendRealMsg2Server(IChannelHandlerContext context, string msg) { Logger.Info($"发送消息到Server, msg:{msg}"); msg = msg.Replace("-", ""); var bytes = BytesUtil.Hex2Bytes(msg); var buffer = Unpooled.WrappedBuffer(bytes); context.WriteAndFlushAsync(buffer); }
public static byte[] BuildMessage(int taskId, TransportType transportType, FunType funType, string mac, byte[] content) { var buffer = Unpooled.Buffer(100); try { // start sign buffer.WriteByte(LoraConst.StartSign); // task sn buffer.WriteByte(taskId); // msg sn buffer.WriteByte(SearialUtil.GetSerial(mac)); // ttl buffer.WriteByte(0); // 通信方式 // 0x81 - 以太网, 0x82 - GPRS , 0x83 - NBIOT buffer.WriteByte((int)transportType); // 时间戳 buffer.WriteInt(TimestampUtil.GetUtcSecondsStamp()); // 控制命令 心跳 - 0x4064 buffer.WriteShort((int)funType); // src mac - 6B var macBytes = BytesUtil.Hex2Bytes(mac); buffer.WriteBytes(macBytes); // dst mac buffer.WriteBytes(macBytes); // content length if (content != null) { buffer.WriteShort(content.Length); buffer.WriteBytes(content); } else { buffer.WriteShort(0); } var validatingData = new byte[buffer.ReadableBytes]; buffer.GetBytes(buffer.ReaderIndex, validatingData); // crc var crc = CRC16.calcCrc16(validatingData); buffer.WriteShort(crc); var msg = new byte[buffer.ReadableBytes]; buffer.ReadBytes(msg); return(msg); } finally { buffer.Release(); } }
private byte[] GetRealMsg() { //线上数据 var hex = "00 b8 a5 01 a5 2e 5d 00 82 5e d7 03 13 40 76 00 01 06 00 00 14 00 16 3e 12 5c 98 00 19 01 02 58 01 00 01 06 00 00 14 01 0b bf 5e d7 03 13 43 72 54 90 ee 96 76 99 7a 05 a5 01"; var mac = hex.Substring(32, 18).Replace(" ", ""); Console.WriteLine($"Time: {DateTime.Now} Mac: {mac} => 发送消息:{hex}"); hex = hex.Replace(" ", ""); var msg = BytesUtil.Hex2Bytes(hex); return(msg); }
protected override void ChannelRead0(IChannelHandlerContext ctx, DatagramPacket msg) { Logger.Info("GkDevice收到server端消息"); var buffer = msg.Content; if (buffer == null) { return; } var serverMsg = Unpooled.Buffer(buffer.ReadableBytes); buffer.ReadBytes(serverMsg); var hexMsg = BytesUtil.BytesToHex(serverMsg.Array); Logger.Info($"收到server端消息, data:{hexMsg}"); if (hexMsg.Contains("7ffe0048")) { var deviceInfo = DeviceInfoMsg.Replace("-", ""); var bytes = BytesUtil.Hex2Bytes(deviceInfo); var deviceBuffer = Unpooled.WrappedBuffer(bytes); ctx.WriteAndFlushAsync(new DatagramPacket(deviceBuffer, msg.Sender)); } }
private void SendRealMsg2Server(IChannelHandlerContext context, string msg) { Logger.Info($"发送消息到Server, msg:{msg}"); msg = msg.Replace("-", ""); var bytes = BytesUtil.Hex2Bytes(msg); var buffer = Unpooled.WrappedBuffer(bytes); DatagramPacket packet = null; if (Index == 0) { packet = new DatagramPacket(buffer, new IPEndPoint(IPAddress.Parse((string)RemoteHost), RemotePort)); } else { packet = new DatagramPacket(buffer, context.Channel.RemoteAddress); } context.WriteAndFlushAsync(packet); Index++; // var packet = new DatagramPacket(buffer,context.Channel.RemoteAddress); // context.WriteAndFlushAsync(packet); }