コード例 #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);
        }
コード例 #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>());
        }