Beispiel #1
0
 public void SerialPortClose()
 {
     if (gSerialComPort.IsOpen)
     {
         Application.DoEvents();
         gSerialComPort.DiscardInBuffer();
         gSerialComPort.DiscardOutBuffer();
         MassUtilityClass.PauseForMilliSeconds(1000);
         Application.DoEvents();
     }
     gSerialComPort.Close();
 }
Beispiel #2
0
 private bool sendLevCmd(byte cmd, byte[] values)
 {
     byte[] TransmitData = LEV_UART_Packet_Forming_and_Decoding.CMD_Forming_For_Transmitting(cmd, values);
     if (gSerialComPort.IsOpen)
     {
         Log(LogMsgType.Outgoing, "Transmitting: " + MassUtilityClass.ByteArrayToHexString(TransmitData, true) + Environment.NewLine);
         // Send the binary data out the port
         gSerialComPort.Write(TransmitData, 0, TransmitData.Length);
         return(true);
     }
     else
     {
         Log(LogMsgType.Error, "Could not Transmit data: " + MassUtilityClass.ByteArrayToHexString(TransmitData, true) + Environment.NewLine);
         return(false);
     }
 }
Beispiel #3
0
        public string getHexStructureLine()
        {
            List <byte> hexLine          = new List <byte>();
            string      hexStructureLine = ":";

            hexLine.Add((byte)HexData.Length);
            byte[] add = BitConverter.GetBytes(Address);
            hexLine.Add(add[1]);
            hexLine.Add(add[0]);
            hexLine.Add(CheckCode);
            hexLine.AddRange(HexData);
            byte[] computeChkSumData = hexLine.ToArray();
            byte   computeChdSum     = MassUtilityClass.CalculateHexChecksum8(computeChkSumData);

            hexLine.Add(computeChdSum);
            computeChkSumData = hexLine.ToArray();
            hexStructureLine += MassUtilityClass.ByteArrayToHexString(computeChkSumData, false);
            return(hexStructureLine);
        }
Beispiel #4
0
        public bool setHexStringLine(string HexLine)
        {
            string str = HexLine.Replace(':', ' ').Trim();

            byte[] hexByte = MassUtilityClass.HexStringToByteArray(str);
            byte   length  = hexByte[0];

            if ((length + 5) != hexByte.Length)
            {
                //fail
                length = 0;
                return(false);
            }
            //UInt16 address = BitConverter.ToUInt16(hexByte, 1);
            UInt16 address = hexByte[1];

            address = (UInt16)((address * 256) + hexByte[2]);
            //byte[] add = BitConverter.GetBytes(address);
            byte checkCode = hexByte[3];

            byte[] data = new byte[length];
            Array.Copy(hexByte, 4, data, 0, length);
            byte ChkSum8 = hexByte[hexByte.Length - 1];

            byte[] computeChkSumData = new byte[hexByte.Length - 1];
            Array.Copy(hexByte, 0, computeChkSumData, 0, computeChkSumData.Length);
            byte computeChdSum = MassUtilityClass.CalculateHexChecksum8(computeChkSumData);

            if (ChkSum8 != computeChdSum)
            {
                return(false);
            }
            Address   = address;
            CheckCode = checkCode;
            HexData   = data;
            return(true);
        }
Beispiel #5
0
        public bool Transforming()
        {
            bool readyForBurning = false;
            bool burning         = false;


            getModifyData();
            BarCodeID = MassUtilityClass.ByteArrayToString(BarCode1_Data) + MassUtilityClass.ByteArrayToString(BarCode2_Data);
            //BarCodeID = Regex.Replace(BarCodeID, @"[\W]+", "");  //移除所有特殊字元
            BarCodeID = Regex.Replace(BarCodeID, @"[^a-zA-Z_0-9]", "");  //移除所有特殊字元
            //TransforHexFile = "_" + BarCodeID.Replace('@', '_') + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".hex";
            TransforHexFile = "_" + BarCodeID + "_" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".hex";

            using (StreamWriter sw = new StreamWriter(@".\Log\Org_DataLog_" + DateTime.Now.ToString("yyyyMMdd_HHmmss_") + TransforHexFile + ".txt"))
            {
                sw.WriteLine("[OneWire_SystemData]");
                sw.WriteLine(MassUtilityClass.ByteArrayToHexString(OneWire_SystemData, true));
                sw.WriteLine("[OneWire_EEPROM]");
                sw.WriteLine(MassUtilityClass.ByteArrayToHexString(OneWire_EEPROM, true));
                sw.Close();
            }

            bool            flag1;
            HexFileDocument hexFD;

            try
            {
                hexFD = new HexFileDocument();
                //flag1 = hexFD.openHexFile(Path.Combine(HexTemplatePath, HexTemplateROMFile));
                flag1 = hexFD.openHexFile(Path.Combine(HexDirectoryName, HexTemplateROMFile));
            }
            catch (Exception ex)
            {
                MessageBox.Show("HexFileDocument: " + ex.ToString(), "HexFileDocument", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }

            if (flag1)
            {
                hexFD.setHexData((UInt16)(0x1000 + Cal_Offset), Cal_Data);
                hexFD.setHexData((UInt16)(0x1000 + MaSN_Offset), MaSN_Data);
                hexFD.setHexData((UInt16)(0x1000 + BarCode1_Offset), BarCode1_Data);
                hexFD.setHexData((UInt16)(0x1000 + BarCode2_Offset), BarCode2_Data);
                try
                {
                    hexFD.SaveHexFile(Path.Combine(HexTransformPath, TransforHexFile));
                    readyForBurning = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("SaveHexFile: " + ex.ToString(), "SaveHexFile", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    readyForBurning = false;
                }
                if (!readyForBurning)
                {
                    return(false);
                }

                burning = ProgramingHEX(BurningType.Unlock, Path.Combine(HexTemplatePath, HexTemplateUnlockFile));
                if (!burning)
                {
                    MessageBox.Show("Failure." + Environment.NewLine +
                                    "Programing Unlock HEX File :" + Path.Combine(HexTemplatePath, HexTemplateUnlockFile) + Environment.NewLine +
                                    "Please Check Connection and ReTry again.", "Programing Unlock HEX File", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    burning = ProgramingHEX(BurningType.Unlock, Path.Combine(HexTemplatePath, HexTemplateUnlockFile));
                    if (!burning)
                    {
                        MessageBox.Show("Failure.", "Programing Unlock HEX File", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false);
                    }
                }
                burning = ProgramingHEX(BurningType.ROM, Path.Combine(HexTransformPath, TransforHexFile));
                if (!burning)
                {
                    MessageBox.Show("Failure." + Environment.NewLine +
                                    "Programing ROM HEX File :" + Path.Combine(HexTransformPath, TransforHexFile) + Environment.NewLine +
                                    "Please Check Connection and ReTry again.", "Programing ROM HEX File", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    burning = ProgramingHEX(BurningType.ROM, Path.Combine(HexTransformPath, TransforHexFile));
                    if (!burning)
                    {
                        MessageBox.Show("Failure.", "Programing ROM HEX File", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false);
                    }
                }
                //burning = ProgramingHEX(BurningType.Prg, Path.Combine(HexTemplatePath, HexTemplatePRGFile));
                burning = ProgramingHEX(BurningType.Prg, Path.Combine(HexDirectoryName, HexTemplatePRGFile));
                if (!burning)
                {
                    MessageBox.Show("Failure." + Environment.NewLine +
                                    //"Programing PROGRAM HEX File :" + Path.Combine(HexTemplatePath, HexTemplatePRGFile) + Environment.NewLine +
                                    "Programing PROGRAM HEX File :" + Path.Combine(HexDirectoryName, HexTemplatePRGFile) + Environment.NewLine +
                                    "Please Check Connection and ReTry again.", "Programing PROGRAM HEX File", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    //burning = ProgramingHEX(BurningType.Prg, Path.Combine(HexTemplatePath, HexTemplatePRGFile));
                    burning = ProgramingHEX(BurningType.Prg, Path.Combine(HexDirectoryName, HexTemplatePRGFile));
                    if (!burning)
                    {
                        MessageBox.Show("Failure.", "Programing PROGRAM HEX File", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false);
                    }
                }
                //burning = ProgramingHEX(BurningType.Lock, Path.Combine(HexTemplatePath, HexTemplateLockFile));
                //if (!burning)
                //{
                //    MessageBox.Show("Failure." + Environment.NewLine +
                //        "Programing Lock HEX File :" + Path.Combine(HexTemplatePath, HexTemplateLockFile) + Environment.NewLine +
                //        "Please Check Connection and ReTry again.", "Programing Lock HEX File", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //    //burning = ProgramingHEX(BurningType.Lock, Path.Combine(HexTemplatePath, HexTemplateLockFile));
                //    if (!burning) { MessageBox.Show("Failure.", "Programing Lock HEX File", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; }
                //}

                return(true);
            }

            return(false);
        }
Beispiel #6
0
        private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            // If the com port has been closed, do nothing
            if (!gSerialComPort.IsOpen)
            {
                return;
            }

            // Obtain the number of bytes waiting in the port's buffer
            int readbytes = gSerialComPort.BytesToRead;

            if (readbytes > 0)
            {
                // Create a byte array buffer to hold the incoming data
                byte[] buffer = new byte[readbytes];
                // Read the data from the port and store it in our buffer
                gSerialComPort.Read(buffer, 0, readbytes);
                //Log(LogMsgType.Dummy, "RAW Data: " + MassUtilityClass.ByteArrayToHexString(buffer));
                //Log(LogMsgType.Dummy, "{receive bytes: " + buffer.Length + "}" + Environment.NewLine);

                ReceivedComPortBytesList.AddRange(buffer);


                List <LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group> outDataGroupList;
                List <byte[]> outDataList;
                LEV_One_Wire_Receiving_Packet_Decoding.Packet_Decoding_To_Group_And_Clear_List(
                    ref ReceivedComPortBytesList, 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)
                        {
                            Log(LogMsgType.Incoming, "Receiving OneWire_SystemData_Group: " +
                                MassUtilityClass.ByteArrayToHexString(outDataList[i], true) + Environment.NewLine);
                            if (gWaitReceiveData)
                            {
                                gGetStatusReceiveData = true;
                                RDTrans.setOneWire_SystemData(outDataList[i]);
                            }
                        }
                        else if (outDataGroupList[i] == LEV_One_Wire_Receiving_Packet_Decoding.Received_Data_Group.OneWire_EEPROM_Group)
                        {
                            Log(LogMsgType.Incoming, "Receiving OneWire_EEPROM_Group: " +
                                MassUtilityClass.ByteArrayToHexString(outDataList[i], true) + Environment.NewLine);
                            if (gWaitReceiveData)
                            {
                                gGetEEPROMReceiveData = true;
                                RDTrans.setOneWire_EEPROM(outDataList[i]);
                            }
                        }
                        else
                        {
                            //Log(LogMsgType.Error, "[Fail] None OneWire_Data : " + outDataGroupList[i] + Environment.NewLine);
                            //Log(LogMsgType.Error, "Group data : " + MassUtilityClass.ByteArrayToHexString(outDataList[i]) + Environment.NewLine);
                        }
                    }
                    if (gGetEEPROMReceiveData && gGetStatusReceiveData)
                    {
                        aTimer.Enabled        = false;
                        gWaitReceiveData      = false;
                        gGetEEPROMReceiveData = false;
                        gGetStatusReceiveData = false;
                        //RDTransForm.Show(this);
                        bool flag = RDTrans.Transforming();
                        if (flag)
                        {
                            UpdateLabelUI("Success", Color.LightGreen, Result_label);
                            MessageBox.Show("BarCodeID :" + Environment.NewLine +
                                            RDTrans.BarCodeID + Environment.NewLine +
                                            "Programming Success.");
                        }
                        else
                        {
                            UpdateLabelUI("Failure", Color.Red, Result_label);
                            MessageBox.Show("BarCodeID :" + Environment.NewLine +
                                            RDTrans.BarCodeID + Environment.NewLine +
                                            "Programming Fail.");
                        }
                    }
                }
            }
        }