public async Task <int> SendCommand(UInt16 intSendNum, byte[] bytSendData, UInt16 intInceptNum, byte[] bytInceptData, int intOverTimeIdx) { await Stream.Write(bytSendData, 0, intSendNum); if (intInceptNum == 0) { return(0); } await Task.Delay(TimeSpan.FromMilliseconds(1000)); return(await Stream.Read(bytInceptData, 0, intInceptNum)); }
private async Task <int> DataDispose(FrameType type, UInt16 addr, byte[] inBuffer, int inOffset, int inCount, byte[] outBuffer, int outOffset) { UInt16 crc16 = 0; FrameType frameType; if (inCount < 1) { return(-1); } //===================================================================================== //命令发送 //===================================================================================== SendBuffer[0] = FrameID; //帧头 SendBuffer[1] = FrameNum++; //帧计数 SendBuffer[2] = (byte)type; //功能模式 SendBuffer[3] = (byte)(addr & 0xFF); //设备地址低位 SendBuffer[4] = (byte)(addr >> 8); //设备地址高位 if (type == FrameType.CommandFrame) { SendBuffer[5] = inBuffer[0]; SendBuffer[6] = inBuffer[1]; SendCount = 8; } else { SendBuffer[5] = (byte)((inCount + 2) & 0xFF); //数据长度低位 SendBuffer[6] = (byte)((inCount + 2) >> 8); //数据长度高位 Array.Copy(inBuffer, 0, SendBuffer, 8, inCount); crc16 = GetCRC16(SendBuffer, 8, inCount); SendCount = 8 + inCount + 2; SendBuffer[SendCount - 2] = (byte)(crc16 & 0xFF); //CRC校验低位 SendBuffer[SendCount - 1] = (byte)((crc16 >> 8) & 0xFF); //CRC校验高位 } SendBuffer[7] = GetCRC(SendBuffer, 0, 7); //清空接收和发生缓冲区 //Stream.DiscardInBuffer(); //Stream.DiscardOutBuffer(); //发送数据 await Stream.Write(SendBuffer, 0, SendCount); //广播命令,没有数据返回 if (addr == 0) { return(0); } //===================================================================================== //信息接收 //===================================================================================== //延时 直到数据接收完毕 //for (int t = 0; t < 8 * 2 + TimeOver; t++) //{ // ReceiveCount = Stream.BytesToRead().Result; // if (ReceiveCount >= 8) break; // Task.Delay(10); //} //找帧头 //if (ReceiveCount >= 8) //{ //return await Stream.Read(ReceiveBuffer, 0, 8).ContinueWith((res) => { //int res = 0; await Stream.Read(ReceiveBuffer, 0, 8); //Debug.WriteLine("get data from serial port , result is {0}", res.Result); //不是对应的响应帧 if (ReceiveBuffer[1] != SendBuffer[1]) { return(-2); } frameType = (FrameType)ReceiveBuffer[2]; //不是响应帧 if (frameType != FrameType.ResponseCommandFrame && frameType != FrameType.ResponseDataFrame) { return(-3); } //不是自己的帧 if (addr != (UInt16)(ReceiveBuffer[4] << 8 | ReceiveBuffer[3])) { return(-4); } //是命令帧 if (frameType == FrameType.ResponseCommandFrame) { return(ReceiveBuffer[6] << 8 | ReceiveBuffer[5]); } else { //数据包长度(含校验) ReceiveCount = (ReceiveBuffer[6] << 8 | ReceiveBuffer[5]); //延时 直到数据接收完毕 // for (int t = 0; t < ReceiveCount * 2 + TimeOver; t++) //{ // if (Stream.BytesToRead().Result >= ReceiveCount) break; // Task.Delay(10); //} //数据没有接收到 // if (Stream.BytesToRead().Result < ReceiveCount) //{ // return -5; //} //接收数据 //Stream.Read(ReceiveBuffer, 0, ReceiveCount); Array.Copy(ReceiveBuffer, 8, ReceiveBuffer, 0, ReceiveCount); crc16 = GetCRC16(ReceiveBuffer, 0, ReceiveCount - 2); if (ReceiveBuffer[ReceiveCount - 2] == (crc16 & 0xFF) && ReceiveBuffer[ReceiveCount - 1] == ((crc16 >> 8) & 0xff)) { Array.Copy(ReceiveBuffer, 0, outBuffer, outOffset, ReceiveCount - 2); return(ReceiveCount - 2); } else { return(-6); } } //}); //} //return -7; }
private int SendCommand(MeterType type, byte[] addr, byte cmd, byte[] inData, out byte[] outData) { byte[] SendBuffer = new byte[256]; //发送数据缓冲区 byte[] ReceiveBuffer = new byte[256]; //接收数据缓冲区 outData = null; if (inData.Length < 1) { return(-3); } //===================================================================================== //命令发送 //===================================================================================== SendBuffer[0] = 0x68; //帧头 SendBuffer[1] = (byte)type; //表类型 Array.Copy(addr, 0, SendBuffer, 2, 7); //地址 SendBuffer[9] = cmd; //控制码 SendBuffer[10] = (byte)inData.Length; //数据长度 Array.Copy(inData, 0, SendBuffer, 11, inData.Length); SendBuffer[11 + inData.Length] = GetCheckCode(SendBuffer, 0, 11 + inData.Length); //校验和 SendBuffer[11 + inData.Length + 1] = 0x16; //帧尾 //发送数据 //Stream.Write( new byte[] { 0xFE, 0xFE }, 0, 2); //Stream.Write( SendBuffer, 0, 11 + inData.Length + 1 + 1); byte[] _buffer = new byte[256]; _buffer[0] = 0xFE; _buffer[1] = 0xFE; Array.Copy(SendBuffer, 0, _buffer, 2, 11 + inData.Length + 1 + 1); Stream.Write(_buffer, 0, 11 + inData.Length + 1 + 1 + 2); //------------------------------ Task.Delay(1000); //----------------------------- int num = Stream.Read(ReceiveBuffer, 0, 14).Result; //数据接收完毕 if (num >= 11 + 3) { Array.Copy(ReceiveBuffer, 3, ReceiveBuffer, 0, num - 3); bool IsOK = true; if (ReceiveBuffer[0] != 0x68) { IsOK = false; } if (!MeterAddr.IsBroadcastAddress(SendBuffer, 2)) //广播地址 { if (!MeterAddr.IsMate(SendBuffer, 2, ReceiveBuffer, 2)) { IsOK = false; } if (SendBuffer[1] != 0xAA) { if (ReceiveBuffer[9] != SendBuffer[9] + 0x80) { IsOK = false; } } } if (IsOK) { int count = ReceiveBuffer[10]; //Stream.Read( ReceiveBuffer, 11, count + 2); byte crc = GetCheckCode(ReceiveBuffer, 0, 11 + count); if (crc == ReceiveBuffer[11 + count] && ReceiveBuffer[11 + count + 1] == 0x16) { if (SendBuffer[1] == 0xAA) { outData = new byte[7]; Array.Copy(ReceiveBuffer, 2, outData, 0, 7); } else { outData = new byte[count]; Array.Copy(ReceiveBuffer, 11, outData, 0, count); } return(count); } } return(-2); } return(-1); }