yuanchaost

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

    #region 字段
        DispatcherTimer timer;

        DispatcherTimer fontTimer;

        public int Port = 0;
        TestItemsModel ConfigModel = new TestItemsModel();
        bool isFirstLoad = true;
        string IP = string.Empty;
        SocketServer server = null;
        // 每次发送的数据长度
        const int SEND_DATA_LEN_ONE_TIME = 256;
        //字库在SPI Flash中的起始地址
        const int FONT_BASE_ADDR_IN_SPI_FLASH = 0X00;
        //IC卡数据长度
        const int IC_CARD_DATA_LEN = 128;
        const int IC_CARD_DRIVER_NAME_LEN = 12;
        const int IC_CARD_GENDER_LEN = 2;
        const int IC_CARD_IDENTIYT_NUMBER_LEN = 18;
        const int IC_CARD_DRIVER_LICENSE_LEN = 18;
        const int IC_CARD_DRIVER_LICENSE_LMT_LEN = 3;
        const int IC_CARD_DRIVER_LEGAL_CODE_LEN = 18;
        const int IC_CARD_RESERVED_STD_EXTEND_LEN = 56;
        const int IC_CARD_CHECKSUM_LEN = 4;
        const int ACK_TIME_OUT = 10;
        const int ERASE_FLASH_ACK_TIME_OUT = 60;

   //IC卡数据的起始地址
        const int IC_CARD_DATA_BASE_ADDR = 0X00;
        const int COM_READ_BUF_LEN = 1024;

        const string CMD_READ_24C01_CARD = "#RIC";
        const string CMD_READ_SL4442_CARD = "#RSL";
        const string CMD_WRITE_24C01_CARD = "#WIC";
        const string CMD_WRITE_SL4442_CARD = "#WSL";
        const string CMD_CLEAR_SPI_FLASH = "#CLR";
        const string CMD_WRITE_SPI_FLASH = "#WW";

        bool bIsFontLibInSending = false; //正在发送字库
        bool bIsComPortOpen = false;//串口是否打开
        bool bIsFlashErase = false; //正在擦除
        bool bIsReadingICCard = false;
        bool bIsWritingICCard = false;
        private static FileStream fs = null;
        private static long index = 0;
        private static long blockCount;
        private static DateTime dt;
        int cardType = 0; //diskType" : int   , 0--4  分别SD1 -- 镜像盘
        int rsType = 0;  //RS485 0  MCU 串口 1
        DateTime startTime;
        DateTime endTime;

byte[] m_pBuffer = null;            /* 字库缓存地址程序中动态分配  */
        uint m_CurSndPos = 0;  /* 当前写的字库地址    */
        uint m_CurSPI_Flash_Addr = 0; /* 当前写SPI_Flash的位置地址  */
        uint m_LastSndLen = 0; /* 上一次发送的数据长度   */
        uint m_FontLibFileLen = 0; /* 字库缓存的长度    */
        #endregion
        public TestViewModel()
        {
            TestItemData = new ObservableCollection<TestItemDataModel>();
            CurrGPSData = new GPSDataModel();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(0.5);
            timer.Tick += Timer_Tick; ;
            fontTimer = new DispatcherTimer();
            fontTimer.Interval = TimeSpan.FromSeconds(0.5);
            fontTimer.Tick += FontTimer_Tick; ;
            LoadedCommand = new RelayCommand(Load);
            FontPathCommand = new RelayCommand(FontPath);
            CleaningMemoryCommand = new RelayCommand(CleaningMemory);
      

  UpgradeFontCommand = new RelayCommand(UpgradeFont);
            OpenPortCommand = new RelayCommand(OpenPort);
            HandleCommand = new RelayCommand(HandleTest);
            PassCommand = new RelayCommand(OnPass);
            NotPassCommand = new RelayCommand(OnNotPass);
            ExportTestCommand = new RelayCommand(ExportTest);
            StopTestCommand = new RelayCommand(StopTest);
            StartTestCommand = new RelayCommand(StartTest);
            BtnDes = "打开";
            Init();

        }


        #region Command
        public ICommand LoadedCommand { get; private set; }
        public ICommand FontPathCommand { get; private set; }
        public ICommand CleaningMemoryCommand { get; private set; }
        public ICommand UpgradeFontCommand { get; private set; }
        public ICommand WriteCardCommand { get; private set; }
        public ICommand CardReadingCommand { get; private set; }
        public ICommand OpenPortCommand { get; private set; }
        public ICommand HandleCommand { get; private set; }
        public ICommand PassCommand { get; private set; }
        public ICommand NotPassCommand { get; private set; }
        public ICommand StopTestCommand { get; private set; }
        public ICommand StartTestCommand { get; private set; }
        public ICommand ExportTestCommand { get; private set; }

        #endregion


        private string _TxtMsg;
        public string TxtMsg
        {
            get { return _TxtMsg; }
            set { Set(() => TxtMsg, ref _TxtMsg, value); }
        }

        private string _BtnDes;
        public string BtnDes
        {
            get { return _BtnDes; }
            set { Set(() => BtnDes, ref _BtnDes, value); }
        }

        private string _FilePath;
        public string FilePath
        {
            get { return _FilePath; }
            set { Set(() => FilePath, ref _FilePath, value); }
        }

        private string _LinkMsg;
        public string LinkMsg
        {
            get { return _LinkMsg; }
            set { Set(() => LinkMsg, ref _LinkMsg, value); }
        }

     

 private Visibility _StartTestVisual = Visibility.Collapsed;
        public Visibility StartTestVisual
        {
            get { return _StartTestVisual; }
            set { Set(() => StartTestVisual, ref _StartTestVisual, value); }
        }


        private Visibility _StopTestVisual = Visibility.Collapsed;
        public Visibility StopTestVisual
        {
            get { return _StopTestVisual; }
            set { Set(() => StopTestVisual, ref _StopTestVisual, value); }
        }


        private Visibility _ExportVisual = Visibility.Collapsed;
        public Visibility ExportVisual
        {
            get { return _ExportVisual; }
            set { Set(() => ExportVisual, ref _ExportVisual, value); }
        }

        private Visibility _ProVisual = Visibility.Collapsed;
        public Visibility ProVisual
        {
            get { return _ProVisual; }
            set { Set(() => ProVisual, ref _ProVisual, value); }
        }

      

  private int _ProMax ;
        public int ProMax
        {
            get { return _ProMax; }
            set { Set(() => ProMax, ref _ProMax, value); }
        }

        private int _ProVal = 100;
        public int ProVal
        {
            get { return _ProVal; }
            set { Set(() => ProVal, ref _ProVal, value); }
        }

        private GPSDataModel _CurrGPSData;
        public GPSDataModel CurrGPSData
        {
            get { return _CurrGPSData; }
            set { Set(() => CurrGPSData, ref _CurrGPSData, value); }
        }

        private bool _CamManual = false;

 public bool CamManual
        {
            get { return _CamManual; }
            set
            {
                Set(() => CamManual, ref _CamManual, value);
                foreach (var item in TestItemData)
                {
                    if (value)
                        item.ManualVisual = Visibility.Visible;
                    else
                        item.ManualVisual = Visibility.Collapsed;
                }
            }
        }

        private TestItemDataModel _CurrTestItemDataModel;
        public TestItemDataModel CurrTestItemDataModel
        {
            get { return _CurrTestItemDataModel; }
            set { Set(() => CurrTestItemDataModel, ref _CurrTestItemDataModel, value); }
        }

 private ObservableCollection<TestItemDataModel> _TestItemData = new ObservableCollection<TestItemDataModel>();
        public ObservableCollection<TestItemDataModel> TestItemData
        {
            get { return _TestItemData; }
            set { Set(() => TestItemData, ref _TestItemData, value); }
        }

        private ObservableCollection<TestItemDataModel> _ManualTestItemData = new ObservableCollection<TestItemDataModel>();
        public ObservableCollection<TestItemDataModel> ManualTestItemData
        {
            get { return _ManualTestItemData; }
            set { Set(() => ManualTestItemData, ref _ManualTestItemData, value); }
        }

 private ObservableCollection<KeyValueModel> _SerialPortName;
        public ObservableCollection<KeyValueModel> SerialPortName //串口名
        {
            get { return _SerialPortName; }
            set { Set(() => SerialPortName, ref _SerialPortName, value); }
        }


        private ObservableCollection<KeyValueModel> _BaudRate;
        public ObservableCollection<KeyValueModel> BaudRate  //波特率
        {
            get { return _BaudRate; }
            set { Set(() => BaudRate, ref _BaudRate, value); }
        }

        private ObservableCollection<KeyValueModel> _CheckBit;
        public ObservableCollection<KeyValueModel> CheckBit   //校验位
        {
            get { return _CheckBit; }
            set { Set(() => CheckBit, ref _CheckBit, value); }
        }


   

   private ObservableCollection<KeyValueModel> _DataBits;
        public ObservableCollection<KeyValueModel> DataBits    //数据位
        {
            get { return _DataBits; }
            set { Set(() => DataBits, ref _DataBits, value); }
        }

        private ObservableCollection<KeyValueModel> _StopBit;
        public ObservableCollection<KeyValueModel> StopBit     //停止位
        {
            get { return _StopBit; }
            set { Set(() => StopBit, ref _StopBit, value); }
        }

        private ObservableCollection<KeyValueModel> _Sex;
        public ObservableCollection<KeyValueModel> Sex   //性别
        {
            get { return _Sex; }
            set { Set(() => Sex, ref _Sex, value); }
        }

      

  private Brush _IndicatorLightColor = Brushes.Red;
        public Brush IndicatorLightColor
        {
            get { return _IndicatorLightColor; }
            set { Set(() => IndicatorLightColor, ref _IndicatorLightColor, value); }
        }

        private KeyValueModel _SelectSerialPort;
        public KeyValueModel SelectSerialPort
        {
            get { return _SelectSerialPort; }
            set { Set(() => SelectSerialPort, ref _SelectSerialPort, value); }
        }

        private KeyValueModel _SelectBaudRate;
        public KeyValueModel SelectBaudRate
        {
            get { return _SelectBaudRate; }
            set { Set(() => SelectBaudRate, ref _SelectBaudRate, value); }
        }

        private KeyValueModel _SelectCheckBit;
        public KeyValueModel SelectCheckBit
        {
            get { return _SelectCheckBit; }
            set { Set(() => SelectCheckBit, ref _SelectCheckBit, value); }
        }

 private KeyValueModel _SelectDataBits;
        public KeyValueModel SelectDataBits
        {
            get { return _SelectDataBits; }
            set { Set(() => SelectDataBits, ref _SelectDataBits, value); }
        }

        private KeyValueModel _SelectStopBit;
        public KeyValueModel SelectStopBit
        {
            get { return _SelectStopBit; }
            set { Set(() => SelectStopBit, ref _SelectStopBit, value); }
        }


        private KeyValueModel _SelectSex;
        public KeyValueModel SelectSex
        {
            get { return _SelectSex; }
            set { Set(() => SelectSex, ref _SelectSex, value); }
        }


       

 #region ByteArray - String ShowTip
        private byte[] GetByteArray(string name)
        {
            return System.Text.Encoding.Default.GetBytes(name);
        }
        private string GetStrByArray(byte[] array)
        {
            return System.Text.Encoding.Default.GetString(array);
        }
        private void ShowTip(string msg)
        {
            TipWindow win = new TipWindow();
            TipWindowViewModel model = new TipWindowViewModel()
            {
                Message = msg,
                ShowConfirm = false
            };
            win.Show(model);
        }

   private void UpgradeFont(object o)
        {
            if (CheckUpdate())
            {
                dt = DateTime.Now;
                fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
                long m_FontLibFileLen = fs.Length;
                if (m_FontLibFileLen == 0)
                {
                    ShowTip("字库文件大小为:0");
                    return;
                }
                byte[] byteArray = new byte[m_FontLibFileLen];
                fs.Read(byteArray, 0, (int)m_FontLibFileLen);
                int m_CurSndPos = 0;
                int m_CurSPI_Flash_Addr = 0;
                int m_LastSndLen = 0;
                if (m_FontLibFileLen < SEND_DATA_LEN_ONE_TIME)
                {
                    m_CurSndPos = 0; // 发送的位置在m_pBuffer中的偏移
                    m_LastSndLen = (int)m_FontLibFileLen;
                    SendFontDataToCom((uint)m_CurSPI_Flash_Addr, byteArray, (uint)m_LastSndLen);
                     bIsFontLibInSending = true;
                }
                else
                {
                    m_CurSndPos = 0;
                    m_LastSndLen = SEND_DATA_LEN_ONE_TIME;
                    SendFontDataToCom((uint)m_CurSPI_Flash_Addr, byteArray, (uint)m_LastSndLen);
                    bIsFontLibInSending = true;
                }
                int Range = (int)m_FontLibFileLen / (SEND_DATA_LEN_ONE_TIME); /* 文件长度 */
                if (Range == 0)    // 说明只需一步即可达到100%
                {
                    Range = 1;
                }
                else if (Range > 0 && (m_FontLibFileLen % (SEND_DATA_LEN_ONE_TIME)) != 0)
                {
                    Range += 1;
                }
                LinkMsg = "0%";
                startTime = DateTime.Now;
                endTime = DateTime.Now;
                int AckCmdLen = 1;
                byte[] AckCmdBuf = new byte[256];// [256] = { 0 };
                           while (bIsFontLibInSending)   /* 字库升级中 */
                {
                    AckCmdLen = GetAckCmdBuf(AckCmdBuf, 256);
                    if (AckCmdLen != 0)
                    {
                        ParaseFontUpdateAck(AckCmdBuf, AckCmdLen);
                        startTime = DateTime.Now;
                    }
                    else
                    {
                        endTime = DateTime.Now;
                        TimeSpan timeSpan = endTime - startTime;     // 两个CTime相减得到CTimeSpan
                        double nTSeconds = timeSpan.TotalSeconds;                // 得到总的秒数
                        if (nTSeconds > ACK_TIME_OUT)
                        {
                            fontTimer.Stop();
                            bIsFontLibInSending = false;
                            m_CurSndPos = 0;
                            m_LastSndLen = 0;
                            m_CurSPI_Flash_Addr = 0;
                            TxtMsg = "字库升级超时失败                     

  }
                    }
                }
            }
        }

  private void ParaseFontUpdateAck(byte[] PAckCmdBuf, int Len)
        {
            byte[] TmpStatues = new byte[128];
            string str = GetStrByArray(PAckCmdBuf);
            if (str.Contains(CMD_WRITE_SPI_FLASH))
            {
                uint AckComWriteSPI_Flash_Addr = 0;
                uint AckComWriteSPI_Flash_Len = 0;

 /* 写入SPI Flash的地址 */
                AckComWriteSPI_Flash_Addr |= (uint)(PAckCmdBuf[3] << 24);
                AckComWriteSPI_Flash_Addr |= (uint)(PAckCmdBuf[4] << 16);
                AckComWriteSPI_Flash_Addr |= (uint)(PAckCmdBuf[5] << 8);
                AckComWriteSPI_Flash_Addr |= (uint)(PAckCmdBuf[6] << 0);

                AckComWriteSPI_Flash_Len |= (uint)(PAckCmdBuf[7] << 24);
                AckComWriteSPI_Flash_Len |= (uint)(PAckCmdBuf[8] << 16);
                AckComWriteSPI_Flash_Len |= (uint)(PAckCmdBuf[9] << 8);
                AckComWriteSPI_Flash_Len |= (uint)(PAckCmdBuf[10] << 0);

                if ((AckComWriteSPI_Flash_Len == m_LastSndLen)
                    && (AckComWriteSPI_Flash_Addr == m_CurSPI_Flash_Addr))
                {
                    m_CurSndPos += m_LastSndLen;    /* 已经发送的长度累增 */
                    m_CurSPI_Flash_Addr += m_LastSndLen;    /* 当前写文件地址累增 */
                    if (m_CurSndPos < m_FontLibFileLen)
                                      {
                        /* 发送剩余的 */
                        if (m_FontLibFileLen - m_CurSndPos <= SEND_DATA_LEN_ONE_TIME)
                        {
                            m_LastSndLen = m_FontLibFileLen - m_CurSndPos;
                            SendFontDataToCom(m_CurSPI_Flash_Addr, PAckCmdBuf, m_LastSndLen);
                            bIsFontLibInSending = true;
                        }
                        else
                        {
                            m_LastSndLen = SEND_DATA_LEN_ONE_TIME;
                            SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer, m_LastSndLen);
                            bIsFontLibInSending = true;
                        }
                        TxtMsg = $"写入进度:{m_CurSndPos}/{m_FontLibFileLen}";
                        ProVisual = Visibility.Visible;
                        ProMax = (int)m_FontLibFileLen;
                        ProVal = (int)m_CurSndPos;
                    }

 else if (m_CurSndPos == m_FontLibFileLen)    /* 写完成 */
                    {
                        ProVal = (int)m_CurSndPos;
                        ProVisual = Visibility.Collapsed;
                        fontTimer.Stop();
                        bIsFontLibInSending = false;
                        m_CurSndPos = 0;
                        m_LastSndLen = 0;
                        m_CurSPI_Flash_Addr = 0;
                        endTime = System.DateTime.Now;
                        TimeSpan timeSpan = endTime - startTime;     // 两个CTime相减得到CTimeSpan
                        double nTSeconds = timeSpan.TotalSeconds;                // 得到总的秒数
                        TxtMsg = $"字库升级成功,共耗时:{nTSeconds}秒";
                        if (m_pBuffer != null)
                        {
                            m_pBuffer = null;
                        }
                    }
                    //CProgressCtrl *pProgressCtrl = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_UPDAT

 else    /* 写Flash 出错 !*/
                {
                    //sprintf(TmpStatues, "MCU写地址出错:AckAddr:%x, AckLen:%d, Addr:%x, Len:%d Pos:%d",\
                    //                AckComWriteSPI_Flash_Addr, AckComWriteSPI_Flash_Len, \
                    //                m_CurSPI_Flash_Addr, m_LastSndLen, m_CurSndPos);
                    //SetStauesText(TmpStatues);
                    //SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer + m_CurSndPos, m_LastSndLen);
                    //bIsFontLibInSending = true;
                }
            }
        }

 

  private byte[] GetByteArray(string name)
        {
            return System.Text.Encoding.Default.GetBytes(name);
        }
        private string GetStrByArray(byte[] array)
        {
            return System.Text.Encoding.Default.GetString(array);
        }

  private void ParaseFontUpdateAck(byte[] PAckCmdBuf, int Len)
        {
            byte[] TmpStatues = new byte[128];
            string str = GetStrByArray(PAckCmdBuf);
            if (str.Contains(CMD_WRITE_SPI_FLASH))
            {
                uint AckComWriteSPI_Flash_Addr = 0;
                uint AckComWriteSPI_Flash_Len = 0;

                /* 写入SPI Flash的地址 */
                AckComWriteSPI_Flash_Addr |= (uint)(PAckCmdBuf[3] << 24);
                AckComWriteSPI_Flash_Addr |= (uint)(PAckCmdBuf[4] << 16);
                AckComWriteSPI_Flash_Addr |= (uint)(PAckCmdBuf[5] << 8);
                AckComWriteSPI_Flash_Addr |= (uint)(PAckCmdBuf[6] << 0);

                AckComWriteSPI_Flash_Len |= (uint)(PAckCmdBuf[7] << 24);
                AckComWriteSPI_Flash_Len |= (uint)(PAckCmdBuf[8] << 16);
                AckComWriteSPI_Flash_Len |= (uint)(PAckCmdBuf[9] << 8);
                AckComWriteSPI_Flash_Len |= (uint)(PAckCmdBuf[10] << 0);


                if ((AckComWriteSPI_Flash_Len == m_LastSndLen)
                    && (AckComWriteSPI_Flash_Addr == m_CurSPI_Flash_Addr))
                {
                    m_CurSndPos += m_LastSndLen;    /* 已经发送的长度累增 */
                    m_CurSPI_Flash_Addr += m_LastSndLen;    /* 当前写文件地址累增 */
                    if (m_CurSndPos < m_FontLibFileLen)
                    {
                        /* 发送剩余的 */
                        if (m_FontLibFileLen - m_CurSndPos <= SEND_DATA_LEN_ONE_TIME)
                        {
                            m_LastSndLen = m_FontLibFileLen - m_CurSndPos;
                            SendFontDataToCom(m_CurSPI_Flash_Addr, PAckCmdBuf, m_LastSndLen);
                            bIsFontLibInSending = true;
                        }
                        else
                        {
                          m_LastSndLen = SEND_DATA_LEN_ONE_TIME;
                            SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer, m_LastSndLen);
                            bIsFontLibInSending = true;
                        }
                        TxtMsg = $"写入进度:{m_CurSndPos}/{m_FontLibFileLen}";
                        ProVisual = Visibility.Visible;
                        ProMax = (int)m_FontLibFileLen;
                        ProVal = (int)m_CurSndPos;
                    }

  else if (m_CurSndPos == m_FontLibFileLen)    /* 写完成 */
                    {
                        ProVal = (int)m_CurSndPos;
                        ProVisual = Visibility.Collapsed;
                        fontTimer.Stop();
                        bIsFontLibInSending = false;
                        m_CurSndPos = 0;
                        m_LastSndLen = 0;
                        m_CurSPI_Flash_Addr = 0;
                        endTime = System.DateTime.Now;
                        TimeSpan timeSpan = endTime - startTime;     // 两个CTime相减得到CTimeSpan
                        double nTSeconds = timeSpan.TotalSeconds;                // 得到总的秒数
                        TxtMsg = $"字库升级成功,共耗时:{nTSeconds}秒";
                        if (m_pBuffer != null)
                        {
                            m_pBuffer = null;
                        }
                    }

   //CProgressCtrl *pProgressCtrl = (CProgressCtrl *)GetDlgItem(IDC_PROGRESS_UPDATE);
                }
                else    /* 写Flash 出错 !*/
                {
                    //sprintf(TmpStatues, "MCU写地址出错:AckAddr:%x, AckLen:%d, Addr:%x, Len:%d Pos:%d",\
                    //                AckComWriteSPI_Flash_Addr, AckComWriteSPI_Flash_Len, \
                    //                m_CurSPI_Flash_Addr, m_LastSndLen, m_CurSndPos);
                    //SetStauesText(TmpStatues);
                    //SendFontDataToCom(m_CurSPI_Flash_Addr, m_pBuffer + m_CurSndPos, m_LastSndLen);
                    //bIsFontLibInSending = true;
                }
            }
        }

private void CleaningMemory(object o)
        {
            if (CheckPortOpen())
            {
                if (true == bIsFlashErase)
                {
                    ShowTip("正在清除");
                    return;
                }
                SendEraseFlashToCom(FONT_BASE_ADDR_IN_SPI_FLASH, 0);
            }
        }

  void SendFontDataToCom(uint WriteAddr, byte[] pSndBuf, uint SndBufLen)
        {
            /* 发送:#WW + 地址(4字节) + 长度(4字节) ... 数据(长度不超过256) ... 校验和(#W,参与校验,共1字节) + OO# */
            byte[] pTmpBuf = null;
            uint CalcBufLen = 0;
            byte[] pCalcAddr = new byte[512];// [512] = { 0 };
            pTmpBuf = pCalcAddr;    /* 计算校验和的起始地址 */

            /* 命令头 */
            pTmpBuf[CalcBufLen++] = (byte)'#';
            pTmpBuf[CalcBufLen++] = (byte)'W';
            pTmpBuf[CalcBufLen++] = (byte)'W';

            /* 写入SPI Flash的地址 */
            pTmpBuf[CalcBufLen++] =(byte) ((WriteAddr >> 0) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((WriteAddr >> 8) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((WriteAddr >> 16) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((WriteAddr >> 24) & 0xff);

     /* 写入数据的长度 */
            pTmpBuf[CalcBufLen++] = (byte)((SndBufLen >> 0) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((SndBufLen >> 8) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((SndBufLen >> 16) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((SndBufLen >> 24) & 0xff);

            //真正发送的数据
            Array.Copy(pSndBuf, 0, pTmpBuf, CalcBufLen, SndBufLen);

            CalcBufLen += SndBufLen;
            byte XorSum = XorCheckSum(pCalcAddr, CalcBufLen);
            pTmpBuf[CalcBufLen++] = XorSum;
            pTmpBuf[CalcBufLen++] = (byte)'O';
            pTmpBuf[CalcBufLen++] = (byte)'O';
            pTmpBuf[CalcBufLen++] = (byte)'#';
            try
            {
                SerialHelper.serialP.Write(pTmpBuf, 0, (int)CalcBufLen);
            }
            catch (Exception ex)
            {
                LogService.Instance.Debug("SendFontDataToCom:" + ex.Message);
                TxtMsg = "写字库数据失败!";
            }
        }

 void FontPath(object o)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = ".*";
            ofd.Filter = "字库文件|*.DZK";
            if (ofd.ShowDialog() == true)
            {
                FilePath = ofd.FileName;
            }
        }

void SendEraseFlashToCom(UInt32 ClearAddr, UInt32 ClearLen)
        {
            string msg = "正在擦除Flash...";
            if (bIsFlashErase)
            {
                ShowTip(msg);
                return;
            }
            TxtMsg = msg;
        
            bIsFlashErase = true;
            /* 发送:#WW + 地址(4字节) + 长度(4字节) ... 数据(长度不超过256) ... 校验和(#W,参与校验,共1字节) + OO# */
            byte[] pTmpBuf = null;
            byte[] pCalcAddr = null;
            uint CalcBufLen = 0;
            pTmpBuf = new byte[512];
            pCalcAddr = pTmpBuf;    /* 计算校验和的起始地址 */
                                    /* 命令头 */
            pTmpBuf[CalcBufLen++] = (byte)'#';
            pTmpBuf[CalcBufLen++] = (byte)'C';
            pTmpBuf[CalcBufLen++] = (byte)'L';
            pTmpBuf[CalcBufLen++] = (byte)'R';

/* 写入SPI Flash的地址 */
            pTmpBuf[CalcBufLen++] = (byte)((ClearAddr >> 0) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((ClearAddr >> 8) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((ClearAddr >> 16) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((ClearAddr >> 24) & 0xff);
            /* 写入数据的长度 */
            pTmpBuf[CalcBufLen++] = (byte)((ClearLen >> 0) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((ClearLen >> 8) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((ClearLen >> 16) & 0xff);
            pTmpBuf[CalcBufLen++] = (byte)((ClearLen >> 24) & 0xff);
            byte XorSum = XorCheckSum(pCalcAddr, CalcBufLen);
            pTmpBuf[CalcBufLen++] = XorSum;
            pTmpBuf[CalcBufLen++] = (byte)'O';
            pTmpBuf[CalcBufLen++] = (byte)'O';
            pTmpBuf[CalcBufLen++] = (byte)'#';
            SerialHelper.serialP.Write(pTmpBuf, 0, (int)CalcBufLen);
        }

byte Hex2BCD(byte Val)
        {
            byte t_h = 0, t_l = 0;
            byte temp = 0;
            t_h = (byte)(Val / 10);
            t_l = (byte)(Val % 10);
            temp = (byte)((t_h << 4) + t_l);
            return temp;
        }

        byte BCD2Hex(byte Val)
        {
            return (byte)(((Val >> 4) & 0xf) * 10 + (Val & 0xf));
        }

  byte XorCheckSum(byte[] pBuff, uint Len)
        {
            byte CheckSum = 0;
            if (null == pBuff)
            {
                return CheckSum;
            }
            for (uint i = 0; i < Len; i++)
            {
                CheckSum ^= pBuff[i]; //*(pBuff + i);
            }
            return CheckSum;
        }

 

 byte AndCheckSum(byte[] pBuff, uint Len)
        {
            byte CheckSum = 0;
            if (null == pBuff)
            {
                return CheckSum;
            }
            for (uint i = 0; i < Len; i++)
            {
                CheckSum += pBuff[i]; //*(pBuff + i);
            }
            return CheckSum;
        }

 

 

 #region 清理存储器
     
        int GetAckCmdBuf(byte[] PAckCmdBuf, int Len)
        {
            int len, Idx;
            int AckCmdLen = 0;    // 单片机返回的应答个数
            byte[] RxDataBuf = new byte[COM_READ_BUF_LEN];// [COM_READ_BUF_LEN] = { 0 };    // 串口读取的缓存
            int TotalRcvLen = 0;        // 保存当前缓存的个数
            /* 以下你可以根据自己的通信协议加入处理代码 */
            len = SerialHelper.serialP.Read(PAckCmdBuf, 0, COM_READ_BUF_LEN - TotalRcvLen);  //pCtrlComm->Read(RxDataBuf + TotalRcvLen, COM_READ_BUF_LEN - TotalRcvLen);
            if (len > 0)
            {
                TotalRcvLen += len; // 长度累加
            }

 for (Idx = 0; Idx < TotalRcvLen; Idx++)
            {
                if (('O' == RxDataBuf[Idx])
                    && ('O' == RxDataBuf[Idx + 1])
                    && ('#' == RxDataBuf[Idx + 2]))
                {
                    AckCmdLen = (int)Idx + 3;
                    TotalRcvLen -= (int)AckCmdLen;    /* 数据向前移动 */
                    byte[] pTmpBuf = new byte[RxDataBuf.Length + AckCmdLen];
                    if (Len >= AckCmdLen)
                    {
                        Array.Copy(RxDataBuf, 0, pTmpBuf, 0, RxDataBuf.Length + AckCmdLen);
                    }
                    else
                    {
                        LinkMsg = "超出缓存限制!";
                        AckCmdLen = 0;
                    }
                    for (int TmpIdx = 0; TmpIdx < TotalRcvLen; TmpIdx++)
                    {
                        RxDataBuf[TmpIdx] = pTmpBuf[TmpIdx];
                    }
                    break;
                }
              }
            return AckCmdLen;
        }

 

 


           

 

posted on 2020-07-27 14:08  yuanchaost  阅读(161)  评论(0编辑  收藏  举报