Example #1
0
 private void SendDataToUart_button_Click(object sender, EventArgs e)
 {
     if (SendDataToUart_textBox.Text == string.Empty)
     {
         return;
     }
     try
     {
         byte[] pre_data = HM_Utilitys.HexStringToByteArray(SendDataToUart_textBox.Text);
         Log(LogMsgType.Normal, HM_Utilitys.ByteArrayToHexString(pre_data) + Environment.NewLine);
         byte   cmd   = pre_data[0];
         byte[] data  = new byte[pre_data[1]];
         int    index = 0;
         for (int i = 2; i < pre_data.Length; i++)
         {
             data[index++] = pre_data[i];
         }
         Send_LEV_CMD_Data(cmd, data);
         //Send_USB_TO_FATOOL_CMD_Data(cmd, data);
     }
     //catch (FormatException)
     catch (Exception ex)
     {
         // Inform the user if the hex string was not properly formatted
         //Log(LogMsgType.Error, "Not properly formatted hex string: " + sendData_textBox.Text + "\n");
         Log(LogMsgType.Error, "Error: " + ex.ToString() + Environment.NewLine);
     }
 }
Example #2
0
        private void Receiving_Data_UnPAcking_By_USBPacket(byte Usb_Cmd, byte[] Usb_Data)
        {
            DSG_Current_ADC       = 0;
            CHG_Current_ADC       = 0;
            VBAT_ADC              = 0;
            VBAT_mV_To_ADC_Factor = 0.0f;
            DSG_mA_To_ADC_Factor  = 0.0f;
            CHG_mA_To_ADC_Factor  = 0.0f;
            //byte Usb_Cmd = ReceivedClass.USB_Cmd;
            //byte[] Usb_Data = (byte[])ReceivedClass.USB_ReceivedData.Clone();
            SetText("Receiving_Data_ByEvent CMD : " + HM_Utilitys.ByteToHexString(Usb_Cmd) + Environment.NewLine);
            SetText("Receive_Data byte: " + HM_Utilitys.ByteArrayToHexString(Usb_Data) + Environment.NewLine);

            //LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group group = LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group.None;
            List <LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group> outDataGroupList;
            List <byte[]> outDataList;

            if (Usb_Cmd == (byte)USB_CDC_Cmd.Cmd_UART_RS485_Receive_Data)
            {
                g_ReceivedUartDataList.AddRange(Usb_Data);
                SetText("Cmd_UART_RS485_Receive_Data byte: " + HM_Utilitys.ByteArrayToHexString(Usb_Data) + Environment.NewLine);
                SetText("Cmd_UART_RS485_Receive_Data List : " + HM_Utilitys.ByteArrayToHexString(g_ReceivedUartDataList.ToArray <byte>()) + Environment.NewLine);
                LEV_One_Wire_Receiving_Packet_Decoding.Packet_Decoding_To_Group_And_Clear_List(ref g_ReceivedUartDataList, out outDataGroupList, out outDataList);

                if ((outDataGroupList.Count >= 1) && (outDataGroupList.Count == outDataList.Count))
                {
                    for (int i = 0; i < outDataGroupList.Count; i++)
                    {
                        if (outDataGroupList[i] == LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group.OneWire_SystemData_Group)
                        {
                            SetText("OneWire_SystemData_Group : " + Environment.NewLine);
                            SetText("Group data : " + HM_Utilitys.ByteArrayToHexString(outDataList[i]) + Environment.NewLine);
                            Set_OneWire_SystemData(outDataList[i]);
                        }
                        else if (outDataGroupList[i] == LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group.OneWire_EEPROM_Group)
                        {
                            SetText("OneWire_EEPROM_Group : " + Environment.NewLine);
                            SetText("Group data : " + HM_Utilitys.ByteArrayToHexString(outDataList[i]) + Environment.NewLine);
                            Set_OneWire_EEPROMData(outDataList[i]);

                            myUI_CalculationInformatio(VBAT_ADC, DSG_Current_ADC, CHG_Current_ADC, VBAT_mV_To_ADC_Factor, DSG_mA_To_ADC_Factor, CHG_mA_To_ADC_Factor);
                        }
                        else
                        {
                            SetText("[Fail] None OneWire_Data : " + outDataGroupList[i] + Environment.NewLine);
                            SetText("Group data : " + HM_Utilitys.ByteArrayToHexString(outDataList[i]) + Environment.NewLine);
                        }
                    }
                }    //if (outDataGroupList.Count >= 1)
                else
                {
                    SetText("[Fail] OneWire_Data List Count Fail.  " + Environment.NewLine);
                    LEV_One_Wire_Receiving_Packet_Decoding.Packet_Decoding_To_Group_And_Clear_List(ref g_ReceivedUartDataList, out outDataGroupList, out outDataList);
                }
            }    //if(Usb_Cmd == (byte)USB_CDC_Cmd.Cmd_UART_RS485_Receive_Data){
        }//private void Receiving_Data_UnPAcking_By_USBPacket(byte Usb_Cmd, byte[] Usb_Data)
Example #3
0
 private void Send_USB_TO_FATOOL_CMD_Data(byte Cmd, byte[] data)
 {
     if (!IsConnectionToFaDevice)
     {
         Log(LogMsgType.Error, "[Error] Could not send while Fa-Tool Device have not found." + Environment.NewLine);
         return;
     }
     byte[] TransmitData = USB_CDC_Packet_Forming_and_Decoding.CMD_Forming_For_Transmitting(Cmd, data);
     Log(LogMsgType.Outgoing, "Transmitting: " + HM_Utilitys.ByteArrayToHexString(TransmitData) + Environment.NewLine);
     FaConnect.ComPortSendData(TransmitData);
 }
Example #4
0
        private void Send_LEV_CMD_Data(byte Cmd, byte[] data)
        {
            if (!IsConnectionToFaDevice)
            {
                return;
            }


            byte[] levPacket = LEV_UART_Packet_Forming_and_Decoding.CMD_Forming_For_Transmitting(Cmd, data);
            Log(LogMsgType.Outgoing, "TransmittinglevPacket: " + HM_Utilitys.ByteArrayToHexString(levPacket) + Environment.NewLine);
            byte[] TransmitViaUart_As_Usb_Parameter = (byte[])levPacket.Clone();
            Send_USB_TO_FATOOL_CMD_Data((byte)USB_CDC_Cmd.Cmd_UART_RS485_Transmit_Data, TransmitViaUart_As_Usb_Parameter);
        }
Example #5
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 #6
0
        //// The method that implements the delegated functionality
        //事件處理方法
        private void FaReceivedMessageChange(Object sender, EventArgs e)
        {
            // 判斷物件是否為 ComPortStatusUpdate_EventArgs 實體
            if (e is SerialPortMessage_EventArgs)
            {
                // 將物件由 EventArgs 轉型 ComPortStatusUpdate_EventArgs
                SerialPortMessage_EventArgs msg = e as SerialPortMessage_EventArgs;
                Log(LogMsgType.Incoming, "[FaReceivedMessage] Msg Type: " + msg.MsgType + ", ConectedStatus: " + msg.ConectedStatus + Environment.NewLine);
                Log(LogMsgType.Incoming, "Com port information : " + msg.ComPortInformation + Environment.NewLine);

                if (msg.MsgType == MessageTypes.ConnectingStatus)
                {
                    if (msg.ConectedStatus == ConnectingStatusTypes.Connected)
                    {
                        FA_Connection();
                    }
                    else
                    {
                        FA_DisConnection();
                    }
                }


                if (msg.MsgType == MessageTypes.ReceiveData)
                {
                    Log(LogMsgType.Incoming, "RX Packet: " + HM_Utilitys.ByteArrayToHexString(msg.ReceivedBuffer) + Environment.NewLine);
                    byte   ReceivedCommand;
                    byte[] decoding_Parameter;
                    bool   is_found = USB_CDC_Packet_Forming_and_Decoding.CMD_Decoding_For_Receiving(msg.ReceivedBuffer, out ReceivedCommand, out decoding_Parameter);
                    if (is_found)
                    {
                        Log(LogMsgType.Incoming, "rx cmd: " + HM_Utilitys.ByteToHexString(ReceivedCommand) + Environment.NewLine);
                        Log(LogMsgType.Incoming, "rx data: " + HM_Utilitys.ByteArrayToHexString(decoding_Parameter) + Environment.NewLine);
                    }
                    else
                    {
                        Log(LogMsgType.Error, "received packet error. " + Environment.NewLine);
                    }
                }
            }
            else
            {
                Log(LogMsgType.Incoming, "[FaReceivedMessage] Unknow Message ....." + Environment.NewLine);
            }
        }
Example #7
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 #8
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>());
        }
Example #9
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 = HM_Utilitys.ComputeModBusCrc16(checkData);

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