Example #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 = HM_Utilitys.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);
        }
Example #2
0
        private const int Receiving_Max_Data_Length_UsbToPc    = 512;    //(0xff);   //not whole structure Length, only Data buffer Length

        /*  USB_CDC_Transmit_Packet
         * struct USB_CDC_Transmit_Packet
         * {
         *  private const byte LeadingCode = CDC_LeadingCode;   //起始字元
         *  private const byte SlAdd = CDC_SlaveAddressCode;    //Slave Address
         *  public byte command;    //命令
         *  private byte DataLenExpected_low;//數據長度,
         *  private byte DataLenExpected_high;//數據長度,
         *  [MarshalAs(UnmanagedType.ByValArray, SizeConst=CDC_Transmitting_Max_Data_Length)]
         *  private byte[] DataBuf;//數據內容
         *  private byte LRCDataLow;           //checkSum16 Low byte, included slave address, command, length and data.
         *  private byte LRCDataHigh;	      //checkSum16 High byte, included slave address, command, length and data.
         *  private const byte cEND1 = CDC_EndingCode1;//結束字元 1
         *  private const byte cEND2 = CDC_EndingCode2;//結束字元 2
         * }*/
        public static byte[] CMD_Forming_For_Transmitting(byte Cmd, byte[] Transmit_Parameter)
        {
            byte[]      TempData;
            List <byte> TransmitData = new List <byte>();
            UInt16      LenExpected;

            if (Cmd == 0x92)
            {
                LenExpected = 0;
            }

            if (Transmit_Parameter.Length > Transmitting_Max_Data_Length_PcToUSB)
            {
                LenExpected = Transmitting_Max_Data_Length_PcToUSB;
                TempData    = new byte[LenExpected];
                for (UInt16 i = 0; i < LenExpected; i++)
                {
                    TempData[i] = Transmit_Parameter[i];
                }
            }
            else
            {
                LenExpected = (UInt16)Transmit_Parameter.Length;
                TempData    = (byte[])Transmit_Parameter.Clone();
            }

            TransmitData.Add(SlaveAddressCode);
            TransmitData.Add(Cmd);
            TransmitData.Add((byte)LenExpected);
            TransmitData.Add((byte)(LenExpected >> 8));
            TransmitData.AddRange(TempData);


            byte[] CheckBuffer = TransmitData.ToArray <Byte>();
            ushort checkSum    = HM_Utilitys.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(EndingCode1);              //2 to last byte for packet
            TransmitData.Add(EndingCode2);              //last byte for packet

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

            return(TransmitData.ToArray <Byte>());
        }
Example #3
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    = HM_Utilitys.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>());
        }