Exemplo n.º 1
0
        //=======Receiving Data Structure===============================
        //typedef struct{
        //    static char cStart = 0X3A;	//起始字元
        //    unsigned char 0X16;		    //Slave Address
        //    unsigned char Command;		//命令
        //    unsigned char LenExpected;	//數據長度
        //    unsigned char DataBuf[DATA_BUF_NUM];//數據內容
        //    unsigned char LRCDataLow;	    //checkSum with slave Low byte, included slave address, command, length and data.
        //    unsigned char LRCDataHigh;	//checkSum with slave High byte, included slave address, command, length and data.
        //    static char cEND1= 0X0D;	//結束字元 1
        //    static char cEND1= 0X0A;	//結束字元 2
        //} LEV Protocol Packet;
        public static bool CMD_Decoding_For_Receiving(byte[] receivingRawData, out byte ReceivedCommand, out byte[] decoding_Parameter)
        {
            bool IsFound_Packet_Form = false;

            ReceivedCommand    = 0;
            decoding_Parameter = new byte[0];
            List <byte> ReceivedDataList = new List <byte>();

            int startFormIndex;
            int dataLength, endCode1_idx, endCode2_idx;

            for (int i = 0; i < (receivingRawData.Length - 1); i++)
            {
                //finding leading codes
                if ((receivingRawData[i] == LeadingCode) && (receivingRawData[i + 1] == SlaveAddressCode))
                {
                    startFormIndex = i;
                    dataLength     = receivingRawData[startFormIndex + 3];    //offset 3 to get receiving data length
                    endCode1_idx   = startFormIndex + 3 + dataLength + 2 + 1; //add offset 2 is two of Cuecksum bytes
                    endCode2_idx   = startFormIndex + 3 + dataLength + 2 + 2; //add offset 2 is two of Cuecksum bytes
                    //finding Ending Codes
                    if ((receivingRawData[endCode1_idx] == EndCode1) && (receivingRawData[endCode2_idx] == EndCode2))
                    {
                        //calculate checkSum
                        byte[] CheckBuffer = new byte[dataLength + 3]; //included slave address, command, length and data.
                        Array.Copy(receivingRawData, startFormIndex + 2, CheckBuffer, 0, CheckBuffer.Length);
                        ushort checkSum = MassUtilityClass.ComputeCheckSum16(CheckBuffer);
                        int    checkSumLowByte_Index  = endCode1_idx - 2;
                        int    checkSumHighByte_Index = checkSumLowByte_Index + 1;
                        if (receivingRawData[checkSumLowByte_Index] == ((byte)(checkSum)) && receivingRawData[checkSumHighByte_Index] == ((byte)(checkSum >> 8)))
                        {
                            //Save data to structure
                            ReceivedCommand = receivingRawData[startFormIndex + 2];

                            for (byte j = 0; j < dataLength; j++)
                            {
                                ReceivedDataList.Add(receivingRawData[startFormIndex + 4 + j]);
                            }
                            IsFound_Packet_Form = true;
                            break;
                        }
                    } //if((ReceivingData[endCode1_idx] == EndingCode1) && (ReceivingData[endCode2_idx] == EndingCode2)){
                }     //if((ReceivingData[i] == LeadingCode) && (ReceivingData[i+1] == SlaveAddressCode)){
            }         //for(int i = 0; i < (ReceivingData.Length - 1); i++){
            if (IsFound_Packet_Form)
            {
                decoding_Parameter = ReceivedDataList.ToArray <byte>();
            }
            return(IsFound_Packet_Form);
        }
Exemplo n.º 2
0
        //========Transmiting Data Structure===========================
        //typedef struct{
        //    static char cStart = 0X3A;	//起始字元
        //    unsigned char 0X16;		//Slave Address
        //    unsigned char Command;		//應回應的命令
        //    unsigned char LenExpected;	//數據長度
        //    unsigned char DataBuf[DATA_BUF_NUM];//數據內容
        //    unsigned char LRCDataLow;	    //checkSum with slave Low byte, included slave address, command, length and data.
        //    unsigned char LRCDataHigh;	//checkSum with slave High byte, included slave address, command, length and data.
        //    static char cEND1= 0X0D;	//結束字元 1
        //    static char cEND1= 0X0A;	//結束字元 2
        //} LEV Protocol Packet;
        public static byte[] CMD_Forming_For_Transmitting(byte Cmd, byte[] Transmit_Parameter)
        {
            List <byte> TransmitData = new List <byte>();
            byte        LenExpected  = (byte)Transmit_Parameter.Length;

            TransmitData.Add(SlaveAddressCode);
            TransmitData.Add(Cmd);
            TransmitData.Add(LenExpected);
            TransmitData.AddRange(Transmit_Parameter);


            byte[] CheckBuffer = TransmitData.ToArray <Byte>();
            ushort checkSum    = MassUtilityClass.ComputeCheckSum16(CheckBuffer);

            TransmitData.Add((byte)(checkSum));        // low byte     //4 to last byte for packet
            TransmitData.Add((byte)(checkSum >> 8));   // high byte    //3 to last byte for packet
            TransmitData.Add(EndCode1);                //2 to last byte for packet
            TransmitData.Add(EndCode2);                //last byte for packet

            TransmitData.Insert(0, LeadingCode);       // add first Item

            return(TransmitData.ToArray <Byte>());
        }
Exemplo n.º 3
0
        private static bool data_CRC16_Check(byte[] data_with_crc16, out byte[] data_without_crc)
        {
            byte crc_hi, crc_lo;
            int  crc_hi_idx, crc_lo_idx;

            crc_hi_idx       = data_with_crc16.Length - 2;
            crc_lo_idx       = data_with_crc16.Length - 1;
            crc_hi           = data_with_crc16[crc_hi_idx];
            crc_lo           = data_with_crc16[crc_lo_idx];
            data_without_crc = new byte[0];
            ushort receiveCRC = (ushort)((crc_hi << 8) + crc_lo);

            byte[] checkData = new byte[data_with_crc16.Length - 2];
            Array.Copy(data_with_crc16, 0, checkData, 0, data_with_crc16.Length - 2);

            ushort crc16 = MassUtilityClass.ComputeModBusCrc16(checkData);

            if (receiveCRC == crc16)
            {
                data_without_crc = (byte[])checkData.Clone();
                return(true);
            }
            return(false);
        }