Beispiel #1
0
        public static CP1616_NoAddr_Packet CP1616_NoAddr_ComProc(ref SerialPort serialPort, byte com, byte[] data, int retry)
        {
            CP1616_NoAddr_Packet ret = new CP1616_NoAddr_Packet(com);

            byte[] tx;
            tx = CP1616_NoAddr_Packet.MakeCP1616_NoAddr_Packet(com, data);
            while (retry != 0)
            {
                serialPort.Write(tx, 0, tx.Length);
                while (true)
                {
                    try
                    {
                        if (ret.DataPacketed((byte)serialPort.ReadByte()))
                        {
                            return(ret);
                        }
                    }
                    catch// (Exception ex)
                    {
                        //Debug.WriteLine("血糖通信失败" + ex.Message);
                        break;
                    }
                }
                retry--;
            }
            return(null);
        }
Beispiel #2
0
        public static object SaveDataProcessCallbackProc(byte[] tempbuffer, ref byte[] buffer, ref int dataOffset, int length)
        {
            //判断读取的字节数+缓冲区已有字节数是否超过缓冲区总大小
            if (length + dataOffset > buffer.Length)
            {
//                 if (dataOffset >= CP1616_NoAddr_PacketHead.HEAD_SIZE)//如果缓冲区数据满足一个包头数据大小,则可以计算出本次接收的包需要的缓冲区大小,从而实现一次调整大小
//                 {
//                     int PacketLen = BytesOP.MakeShort(buffer[5], buffer[6]);
//                     Array.Resize<Byte>(ref buffer, CP1616_NoAddr_PacketHead.HEAD_SIZE + PacketLen+2);
//                 }
//                 else //不满足一个完整的网络封包的大小
//                 {
                Array.Resize <Byte>(ref buffer, length + dataOffset);//buffer.Length + BUFFER_SIZE * 2);
//                 }
            }
            //将新读取的数据拷贝到缓冲区
            Array.Copy(tempbuffer, 0, buffer, dataOffset, length);
            //修改"数据实际长度"
            dataOffset += length;
            if (dataOffset > CP1616_NoAddr_PacketHead.HEAD_SIZE)//已经有头数据了
            {
                int PacketLen = BytesOP.MakeShort(buffer[3], buffer[4]);
                if (dataOffset < (CP1616_NoAddr_PacketHead.HEAD_SIZE + PacketLen + 2))
                {
                    return(null);
                }
            }
            //获得了完整的数据包
            CP1616_NoAddr_Packet packet = new CP1616_NoAddr_Packet();
            int index = 0;

            for (index = 0; index < dataOffset; index++)
            {
                if (packet.DataPacketed(buffer[index]))
                {
                    Array.Copy(buffer, CP1616_NoAddr_PacketHead.HEAD_SIZE + packet.Header.Len + 2, buffer, 0, dataOffset - (CP1616_NoAddr_PacketHead.HEAD_SIZE + packet.Header.Len + 2));
                    dataOffset -= (CP1616_NoAddr_PacketHead.HEAD_SIZE + packet.Header.Len + 2);//缓冲区实际数据长度减去一个完整封包长度
                    return(packet);
                }
            }
            dataOffset = 0;
            return(null);
//             TcpModbusPacket packet = GetPacket(buffer);
//             Array.Copy(buffer, CP1616_NoAddr_PacketHead.HEAD_SIZE + packet.Header.Len+2, buffer, 0, dataOffset - (CP1616_NoAddr_PacketHead.HEAD_SIZE + packet.Header.Len+2));
//             dataOffset -= (CP1616_NoAddr_PacketHead.HEAD_SIZE + packet.Header.Len+2);//缓冲区实际数据长度减去一个完整封包长度
//             return packet;
        }