⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 SPI读写
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
#include "IniFiles.hpp"
#include <stdio.h>
#include <math.h>
#include "Unit1.h"

#include <IniFiles.hpp>
#include <sys\stat.h>
#include <sys\stat.h>
#define FRMSIZE 256
#define BUFSIZE 10*1024
#define PRECESS(position,tolsize)     ProgressBar1->Position=(10000/(tolsize/FRMSIZE))* position

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
exitflag=false;
macid=0;
GetYcount=0;
}
//---------------------------------------------------------------------------
int  TForm1::  getfilelen(char *filename)
{
    FILE *fp;
    struct stat statbuf;
    long flength;
    int  fsum,numbytes;
    DWORD dwBytesToSent;
    char buf[BUFSIZE];
    memset(buf,0x00,BUFSIZE);
    int crc32=0;
    if((fp=fopen(filename,"rb")) == NULL)
    {
      ShowMessage("open file error!");
      fclose(fp);
      return 0;
    }
    fseek(fp, SEEK_SET, 0);
    stat(filename,&statbuf);
    flength = statbuf.st_size;
    fclose(fp);

    return flength;
}
int  TForm1::sendonefile(char *filename)
{
    FILE *fp;
    struct stat statbuf;
    long flength;
    int  fsum,numbytes;
    DWORD dwBytesToSent;
    char buf[BUFSIZE];
    memset(buf,0x00,BUFSIZE);
    int crc32=0;
    if((fp=fopen(filename,"rb")) == NULL)
    {
      ShowMessage("open file error!");
      fclose(fp);
      return 0;
    }
    fseek(fp, SEEK_SET, 0);
    stat(filename,&statbuf);
    flength = statbuf.st_size;

    if(flength%FRMSIZE != 0)
      fsum = flength/FRMSIZE + 1;
    else
      fsum = flength/FRMSIZE;
    numbytes=0;
    if(flength%FRMSIZE == 0)
    for(int i=0;i<fsum;i++)
    {

        memset(buf,'\0',FRMSIZE);
        numbytes += fread(buf,FRMSIZE,1,fp);
        WriteFile(handCom,&buf,FRMSIZE,&dwBytesToSent,NULL); //send s
        PRECESS(i+1,flength);
        for(int j=0;j<FRMSIZE;j++)
          crc32+= (BYTE)buf[j];
    }
    else
    for(int i=0;i<fsum;i++)
    {
        if(i==(fsum-1))
        {
            memset(buf,'\0',FRMSIZE);
            numbytes = fread(buf,flength-(i*FRMSIZE),1,fp);
            WriteFile(handCom,&buf,flength-(i*FRMSIZE),&dwBytesToSent,NULL); //send s
            for(int j=0;j<flength-(i*FRMSIZE);j++)
               crc32+= (BYTE)buf[j];
            if(flength<FRMSIZE) flength =FRMSIZE;
             ProgressBar1->Position=(10000/(flength/FRMSIZE))* (i+1)  ;

        }
        else
        {
            memset(buf,'\0',FRMSIZE);
            numbytes = fread(buf,FRMSIZE,1,fp);
            WriteFile(handCom,&buf,FRMSIZE,&dwBytesToSent,NULL); //send s
            PRECESS(i+1,flength);
             for(int j=0;j<FRMSIZE;j++)
                crc32+= (BYTE)buf[j];
        }
    }
    fclose(fp);
    return crc32;
}//
AnsiString TForm1::Getsubstr(AnsiString str,int beginpos)
{
       AnsiString mstr;
       mstr= str.SubString(beginpos,strlen(str.c_str())-beginpos+1);
       return mstr;
}



bool TForm1::WriteBUF(BYTE buf[])
{
   DWORD dwBytesToSent,dwByesToWrite;
   BYTE bufcmd[16];
   memset(bufcmd,0,16);
   memcpy(bufcmd,buf,strlen(buf));
   WriteFile(handCom,&bufcmd,strlen(bufcmd),&dwBytesToSent,NULL); //send s
/* DWORD dwBytesToSent=1,dwByesToWrite=2;
   char  pReadBuffer[16];
   BYTE bufcmd[1];
   bufcmd[0]=buf;
   memset(pReadBuffer,0,16);
   if(!onlyread)
   {
        WriteFile(handCom,&bufcmd,1,&dwBytesToSent,NULL); //send s
        Sleep(10);
   }
   bool bReadStatus=false;
   while(!bReadStatus) bReadStatus=ReadFile(handCom,&pReadBuffer,2,&dwByesToWrite,NULL); //send screen id
   if(dwByesToWrite==1)
   {
       if(BYTE(pReadBuffer[0])==0xa8)
         return true;
       else  if(BYTE(pReadBuffer[0])==0xe7)
         return true;
   }
   else if(dwByesToWrite==2)
   {
            if((BYTE(pReadBuffer[0])==bufcmd[0])&&(BYTE(pReadBuffer[1])==0x43) )
                return true;
            else  if((BYTE(pReadBuffer[0])==bufcmd[0])&&(BYTE(pReadBuffer[1])==0xe6) )
                return true;
            else  if((BYTE(pReadBuffer[0])==bufcmd[0])&&(BYTE(pReadBuffer[1])==bufcmd[0]) )
                return true;
    }
    else return false;
    */
}

unsigned int TForm1::SendOneBinFile()
{
    //FILE *fp;
    //struct stat statbuf;
    //long flength;
    //int  fsum,numbytes;
    //DWORD dwBytesToSent; 
    char buf[BUFSIZE];
    memset(buf,0x00,BUFSIZE);
    return 1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
  BYTE pReadBuffer[32];
  BYTE pWriteBuffer[32];
  DWORD dwBytesToSent,dwByesToWrite;
  DWORD dwEvent,dwError;
  BOOL bReadStatus;
  DWORD dwErrorFlags;
  COMSTAT ComStat;
  OVERLAPPED m_OverlappedRead,m_OverlappedWrite;
  AnsiString rebuff;
  Edit_SIZE->Text = getfilelen(Edit_path->Text.c_str()) ;
  ProgressBar1->Position=0;
  Application->ProcessMessages();
  int  m_strlen;

  
  memset(pReadBuffer,0,16);
  memset(pWriteBuffer,0,16);
  handCom = OpenPort(iComPort);

  COMMTIMEOUTS CommTimeOuts;
  CommTimeOuts.ReadIntervalTimeout = 100;
  CommTimeOuts.ReadTotalTimeoutMultiplier = 50;
  CommTimeOuts.ReadTotalTimeoutConstant = 1000;
  CommTimeOuts.WriteTotalTimeoutMultiplier = 50;
  CommTimeOuts.WriteTotalTimeoutConstant =500;
  SetCommTimeouts( handCom, &CommTimeOuts );
  
  if(handCom==NULL)
  {
        MSGLIST->Lines->Add("请打开有效串口,ERROR!");
        return;
  }
  if(Change_RS232_State(handCom,19200, NOPARITY))
  {
  //while(1)
  {
    memset(pWriteBuffer,0,32);
    char dd[16];
    bReadStatus=ReadFile(handCom,&pReadBuffer,22,&dwByesToWrite,NULL); //send screen id
    for(int i=0;i<22;i++)
    sprintf(dd+i,"%X    ",pReadBuffer[i]);
    MSGLIST->Lines->Add (dd);
    Sleep(1000);
  }
  return;

    ACKBEGIN:
        MSGLIST->Lines->Add("开始握手!");
        pWriteBuffer[0]=0xa9  ;
        WriteBUF(pWriteBuffer);
        Sleep(10);
        memset(pWriteBuffer,0xff,16);
        memset(pReadBuffer,0,16);
     int bReadStatus=0;
     while(!bReadStatus)

       bReadStatus=ReadFile(handCom,&pReadBuffer,1,&dwByesToWrite,NULL); //send screen id
     if(pReadBuffer[0]!=0xa8) goto ACKBEGIN;
     MSGLIST->Lines->Add("开始烧写!");
     if(rg_op_type->ItemIndex==0)
        pWriteBuffer[0]=0xA7;
     else pWriteBuffer[0]=0xA6;

     if(RG_filetype->ItemIndex==0)
        pWriteBuffer[1]=0xA5;
     else pWriteBuffer[1]=0xA4;

     Edit_SIZE->Text = getfilelen(Edit_path->Text.c_str()) ;
     pWriteBuffer[2]=((Edit_SIZE->Text.ToInt())&0xffff0000)>>24;
     pWriteBuffer[3]=(((Edit_SIZE->Text.ToInt())&0xffff0000)>>16)&0xff;
     pWriteBuffer[4]=((Edit_SIZE->Text.ToInt())&0x0000ffff)>>8;
     pWriteBuffer[5]=(((Edit_SIZE->Text.ToInt())&0x0000ffff))&0xff;

     pWriteBuffer[6]=0x02;
     pWriteBuffer[7]=0xd8;
     pWriteBuffer[8]=0x52;
     WriteFile(handCom,&pWriteBuffer,16,&dwBytesToSent,NULL); //send s
     memset(pReadBuffer,0,16);
     Sleep(5);
     bReadStatus=0;
     while(!bReadStatus)
     bReadStatus=ReadFile(handCom,&pReadBuffer,1,&dwByesToWrite,NULL); //send screen id    0xaa
     if(pReadBuffer[0]!=0xaa) goto ACKBEGIN;

     int crc32 = sendonefile(Edit_path->Text.c_str());
     Application->ProcessMessages();
     memset(pWriteBuffer,0,16);
     pWriteBuffer[0]=(crc32&0x0000ffff)>>8  ;
     pWriteBuffer[1]=(crc32&0x0000ffff)&0xff  ;
     Sleep(50);
     WriteFile(handCom,&pWriteBuffer,2,&dwBytesToSent,NULL); //send crc check

     memset(pWriteBuffer,0,16);
     pWriteBuffer[0]=0xab;
     WriteBUF(pWriteBuffer) ; //send end cmd word

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -