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

📄 unit1.cpp

📁 SPI读写
💻 CPP
📖 第 1 页 / 共 2 页
字号:
     memset(pReadBuffer,0,16);
     dwByesToWrite=bReadStatus=0;
     Sleep(5);
     int k=0;
     while(!dwByesToWrite)
     {
        bReadStatus=ReadFile(handCom,&pReadBuffer,1,&dwByesToWrite,NULL); //send screen id
        if((k++)==8) break;

     }
     if(pReadBuffer[0]==0xac) MSGLIST->Lines->Add("烧写成功!");
     else       MSGLIST->Lines->Add("烧写失败!");
     Close_RS232(handCom);
     handCom=NULL;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{

      TIniFile *ini;
      char buffer[32];
      ini = new TIniFile(ChangeFileExt( Application->ExeName, ".INI" ) );

      iComPort=ini->ReadInteger("LedCom","ComID",NULL);
      macid=ini->ReadInteger("LedCom","MACID",NULL);
      if(iComPort==0)
       RadioGroup1->ItemIndex=0;
      else
      RadioGroup1->ItemIndex=iComPort-1;
      unsigned short year,month,day,hour,min,sec,msec,week;

      Date().DecodeDate(&year,&month,&day);
      OldDay=day;

      Timer1->Enabled=false;

 
      
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
 
  iComPort=1;  
}
//---------------------------------------------------------------------------
void __fastcall TForm1::RadioGroup1Click(TObject *Sender)
{
      iComPort=RadioGroup1->ItemIndex+1;
      TIniFile *ini;
      ini = new TIniFile(ChangeFileExt( Application->ExeName, ".INI" ) );

      ini->WriteInteger("LedCom","ComID",iComPort );
      //ini->WriteInteger("LedCom","MACID",macid );
}
//---------------------------------------------------------------------------
void TForm1::Close_RS232(HANDLE hHandle)
{
        //TODO: Add your source code here
        CloseHandle(hHandle);
}

//------------------------------------------------------------------------------
HANDLE TForm1::OpenPort(int iPortNum)
{
        HANDLE hComPort;
        char pcPortStr[10];

        if(iPortNum>4||iPortNum<1)
        {
                MessageBox(Handle,"无效的端口号","Error",MB_OK|MB_ICONSTOP);
                return 0;
        }
        wsprintf(pcPortStr,"COM%d",iPortNum);
        hComPort = CreateFile(pcPortStr,			// communication port string (COMX)
                                GENERIC_READ | GENERIC_WRITE,	// read/write types
				0,				// the object cannot be shared
				NULL,				// the handle cannot be inherited or no security attributes
				OPEN_EXISTING,			// comm devices must use OPEN_EXISTING
				FILE_ATTRIBUTE_NORMAL /*| FILE_FLAG_OVERLAPPED*/ ,// Async I/O
				0);				// template must be 0 for comm devices

	if (hComPort == INVALID_HANDLE_VALUE)
	{
		// port not found
		MessageBox(Handle,"不能打开指定的端口","Error",MB_OK|MB_ICONSTOP);
                CloseHandle(hComPort);
                return NULL;
	}
        else
        return hComPort;
        //SetCommMask(g_hComPort,EV_RXFLAG|EV_CTS|EV_DSR);
        //SetupComm(g_hComPort,4096,4096);

}
//-----------------------------------------------------------------------------
BOOL TForm1::Change_RS232_State(HANDLE hHandle, DWORD dwBaudRate, BYTE byParity)
{
        //TODO: Add your source code here
    DCB dcb;
    GetCommState(hHandle, &dcb);
    dcb.BaudRate=dwBaudRate;
    dcb.ByteSize = 8;
    dcb.Parity = byParity; //MARKPARITY MARK - set the 9th is 1;
    dcb.StopBits = ONESTOPBIT; //1 bit
    //dcb.fRtsControl =SETRTS;
    if(SetCommState(hHandle, &dcb))//set successfully
	  {
        //Must be set
        Sleep(100);
        return true;
    }
  	else
	  {
		    MessageBox(Handle,"不能设置指定的端口号!","Error",MB_OK|MB_ICONSTOP);
        return false;
    }
}



void __fastcall TForm1::Button3Click(TObject *Sender)
{
 if(handCom==NULL)
 {
    handCom = OpenPort(iComPort);
    TimerRec->Enabled=TRUE;

    Button3->Font->Color=clRed;
    Button3->Caption="CloseCom";
    RadioGroup1->Enabled=false;
 }
 else
 {

    Close_RS232(handCom);
    handCom=NULL;
    TimerRec->Enabled=FALSE;
    Button3->Font->Color=clWindowText;
    Button3->Caption="OpenCom";
    RadioGroup1->Enabled=true;
 }
 Application->ProcessMessages();
}
//---------------------------------------------------------------------------
char * TForm1::RecevieData()
{
  char pReadBuffer[1024];
  DWORD dwBytesToSent;

 BOOL bReadStatus;
  DWORD dwErrorFlags;
/*   COMSTAT ComStat;
  OVERLAPPED m_OverlappedRead,m_OverlappedWrite;
  char *pt=pReadBuffer;
  if(handCom==NULL) return NULL;
  ClearCommError( handCom, &dwErrorFlags, &ComStat );
  if( !ComStat.cbInQue ) return NULL;

  memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
  memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

  COMMTIMEOUTS CommTimeOuts;
  CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
  CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
  CommTimeOuts.ReadTotalTimeoutConstant = 0;
  CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
  CommTimeOuts.WriteTotalTimeoutConstant =500;
  SetCommTimeouts( handCom, &CommTimeOuts );
  exitflag=false;
  m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
  m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
  if(Change_RS232_State(handCom,57600, NOPARITY)) */
  {
        //Sleep(100);
        memset(pReadBuffer,0,1024);
        bReadStatus = ReadFile(handCom,&pReadBuffer,1,&dwBytesToSent,NULL); //send screen id
        if( !bReadStatus ){
                /*if( GetLastError() == ERROR_IO_PENDING ){
                    WaitForSingleObject( m_OverlappedRead.hEvent,100 );
                }*/
        }
  }
  return pReadBuffer;
}
void __fastcall TForm1::TimerRecTimer(TObject *Sender)
{
  char pReadBuffer[1024];
  DWORD dwBytesToSent;
  memset(pReadBuffer,0,1024);
  BOOL bReadStatus;
  DWORD dwErrorFlags;
  COMSTAT ComStat;
  OVERLAPPED m_OverlappedRead;
  char *pt=pReadBuffer;
  if(handCom==NULL) return;
  ClearCommError( handCom, &dwErrorFlags, &ComStat );
  if( !ComStat.cbInQue ) return ;

        memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) );
//memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) );

  COMMTIMEOUTS CommTimeOuts;
  CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
  CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
  CommTimeOuts.ReadTotalTimeoutConstant = 0;
  CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
  CommTimeOuts.WriteTotalTimeoutConstant =500;
  SetCommTimeouts( handCom, &CommTimeOuts );
  exitflag=false;
  m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
  //m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL );
  if(Change_RS232_State(handCom,19200, NOPARITY))
  {
    while(!exitflag)
    {
        bReadStatus = ReadFile(handCom,pt,1,&dwBytesToSent,NULL); //send screen id
        if( !bReadStatus ){
                if( GetLastError() == ERROR_IO_PENDING ){
                    WaitForSingleObject( m_OverlappedRead.hEvent,100 );
                }
        }
        if(*pt=='\0')
        {
          exitflag=true;
        }
        else pt++;
    }
    if(*pt=='\0')
        *(pt)=0x0d;
  }
  String str=pReadBuffer;
  MSGLIST->Lines->Add(str);
  if(strcmp(str.c_str(),"That's right? press y or Y .")==0)
  {
      //reInformation = str;
      GetYcount++;
  }

}
//---------------------------------------------------------------------------




void __fastcall TForm1::Button1Click(TObject *Sender)
{
   MSGLIST->Clear();
}
//---------------------------------------------------------------------------







void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
{
   Edit_path->Text=  OpenDialog1->FileName;

}
}
//---------------------------------------------------------------------------





⌨️ 快捷键说明

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