📄 main.~cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <inifiles.hpp>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <vector>
#include <iostream>
#include <fstream>
#pragma hdrstop
#include "Com_enum.h"
#include "main.h"
#include "About.h"
#include "Meta_com.h"
#include "OK_Wnd.h"
//-------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "CGAUGES"
#pragma resource "*.dfm"
TMainForm *MainForm;
//---------------------------------------------------------------------------
__fastcall TMainForm::TMainForm(TComponent* Owner)
: TForm(Owner)
{
m_nComPort = 1;
m_nBaudRate = 921600;
m_nFilesCount = 0;
m_InLVUpdate = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::m_btnExitClick(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::m_btnAboutClick(TObject *Sender)
{
AboutBox->Show();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::m_btnDownloadClick(TObject *Sender)
{
if( m_nFilesCount > 0 )
{
m_tskMetaCom = new Meta_com(true);
m_tskMetaCom->FreeOnTerminate = true;
m_tskMetaCom->Resume();
m_btnDownload->Enabled = false;
m_btnAdd->Enabled = false;
m_btnRemove->Enabled = false;
ComboBox1->Enabled = false;
ListView1->Enabled = false;
m_nCostTimeSec = 0;
Timer1->Enabled = true;
}
else
{
ShowMessage( "File pool is empty!" );
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormCreate(TObject *Sender)
{
//
META_Init(NULL);
m_pUpdateListLock = new TCriticalSection();
TIniFile *ini;
ini = new TIniFile( ChangeFileExt( Application->ExeName, ".INI" ) );
m_nComPort = ini->ReadInteger( "FORM", "com", 1 );
m_nBaudRate = ini->ReadInteger( "RORM", "baudrate", 921600 );
int ret;
unsigned short com_port_list[255];
unsigned short com_port_count = sizeof(com_port_list)/sizeof(unsigned short);
if( COM_ENUM_OK != (ret=ComPortEnumerate(com_port_list, &com_port_count)) )
{
if( COM_ENUM_NO_COM_PORT_FOUND == ret ) {
ShowMessage(" No COM port detected! ");
}
else {
ShowMessage(" Enumerate COM port fail! ");
}
//return;
}
else
{
m_COMPortCount = com_port_count;
memcpy( m_COMPortList, com_port_list, m_COMPortCount*sizeof(unsigned short) );
ComboBox1->Clear();
ComboBox1->Items->Clear();
int chk_index = -1;
for(int i=0; i<m_COMPortCount; i++)
{
AnsiString s = "COM" + IntToStr(m_COMPortList[i]);
ComboBox1->AddItem( s, this );
if( m_nComPort == m_COMPortList[i] )
chk_index = i;
}
if( chk_index == -1 )
{
chk_index = 0;
m_nComPort = m_COMPortList[0];
}
ComboBox1->ItemIndex = chk_index;
StatusBar1->Panels->Items[1]->Text = "COM" + IntToStr(m_COMPortList[chk_index]);
}
StatusBar1->Panels->Items[2]->Text = IntToStr(m_nBaudRate) + " bps";
//
m_nFilesCount = ini->ReadInteger( "DOWNLOAD", "FilesCount", 0 );
for( unsigned int i=0; i<m_nFilesCount; i++ )
{
DownloadBinFile binfile;
binfile.m_Filepath = ini->ReadString( "DOWNLOAD"+IntToStr(i), "FilePath", "" );
binfile.m_AliasName = ini->ReadString( "DOWNLOAD"+IntToStr(i), "AliaName", "" );
binfile.m_Enable = ini->ReadBool( "DOWNLOAD"+IntToStr(i), "Enable", true );
std::ifstream in( binfile.m_Filepath.c_str(), (std::ios_base::in | std::ios_base::binary) );
in.seekg( 0, std::ios_base::end );
binfile.m_Size = in.tellg();
if( binfile.m_Size == 0 )
{
continue;
}
m_FilesList.push_back( binfile );
}
m_nFilesCount = m_FilesList.size();
delete ini;
RedrawDownloadList();
//
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
{
//
unsigned int i;
TIniFile *ini;
DownloadFileList::iterator iter;
try {
ini = new TIniFile(ChangeFileExt(Application->ExeName, ".INI" ));
}catch(...) {
goto form_close_end;
}
ini->WriteInteger( "FORM", "com", m_nComPort );
ini->WriteInteger( "FORM", "baudrate", m_nBaudRate );
ini->WriteInteger( "DOWNLOAD", "FilesCount", m_nFilesCount );
for( i=0, iter=m_FilesList.begin(); iter!=m_FilesList.end(); i++, iter++ )
{
ini->WriteString( "DOWNLOAD"+IntToStr(i), "FilePath", iter->m_Filepath );
ini->WriteString( "DOWNLOAD"+IntToStr(i), "AliaName", iter->m_AliasName );
ini->WriteBool( "DOWNLOAD"+IntToStr(i), "Enable", iter->m_Enable );
}
form_close_end:
META_Deinit();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::FormDestroy(TObject *Sender)
{
delete m_pUpdateListLock;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::RedrawDownloadList(void)
{
//TODO: Add your source code here
CS_Sentry cs(m_pUpdateListLock);
m_InLVUpdate = true;
TListItem *pListItem;
ListView1->Items->Clear();
DownloadFileList::iterator iter;
for( iter=m_FilesList.begin(); iter!=m_FilesList.end(); iter++ )
{
pListItem = ListView1->Items->Add();
pListItem->Checked = iter->m_Enable;
pListItem->Caption = iter->m_AliasName;
pListItem->SubItems->Add( "0x"+IntToHex( (int)iter->m_Size, 8) );
pListItem->SubItems->Add( iter->m_Filepath );
}
m_InLVUpdate = false;
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::m_btnRemoveClick(TObject *Sender)
{
DownloadFileList list;
DownloadBinFile binfile;
if( ListView1->ItemIndex < 0 )
{
ShowMessage(" No Selected file ");
return;
}
TListItem* pListItem = ListView1->Items->Item[ListView1->ItemIndex];
binfile.m_AliasName = pListItem->Caption;
DownloadFileList::iterator iter;
for( iter=m_FilesList.begin(); iter!=m_FilesList.end(); iter++ )
{
if( 0 != binfile.m_AliasName.AnsiCompareIC(iter->m_AliasName) )
{
list.push_back( *iter );
}
}
m_FilesList.erase( m_FilesList.begin(), m_FilesList.end() );
m_FilesList = list;
m_nFilesCount = m_FilesList.size();
RedrawDownloadList();
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::m_btnAddClick(TObject *Sender)
{
static AnsiString strInitFolder;
if( 0 < strInitFolder.Length() )
{
OpenDialog1->InitialDir = strInitFolder;
}
else
{
OpenDialog1->InitialDir = Application->GetNamePath();
}
if( OpenDialog1->Execute() )
{
std::ifstream in( OpenDialog1->FileName.c_str(), (std::ios_base::in | std::ios_base::binary) );
in.seekg( 0, std::ios_base::end );
unsigned int size = in.tellg();
if( size > 0 )
{
DownloadBinFile binfile;
binfile.m_Size = size;
binfile.m_Filepath = OpenDialog1->FileName;
binfile.m_Enable = true;
binfile.m_AliasName = ExtractFileName( OpenDialog1->FileName );
m_FilesList.push_back( binfile );
m_nFilesCount = m_FilesList.size();
RedrawDownloadList();
}
else
{
ShowMessage(" Selected an empty file ");
}
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::Timer1Timer(TObject *Sender)
{
m_nProgressTimeSec ++;
m_nCostTimeSec ++;
unsigned int sec, min, h, t;
sec = m_nCostTimeSec % 60;
t = m_nCostTimeSec / 60;
min = t % 60;
h = t / 60;
Edit1->Text.printf( "%2d : %2d : %2d", h, min, sec );
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ListView1Change(TObject *Sender,
TListItem *Item, TItemChange Change)
{
if( m_InLVUpdate == true )
return;
DownloadFileList::iterator iter = m_FilesList.begin();
int idx;
//TListView* listview = dynamic_cast<TListView*>(Sender);
/*
switch( Change )
{
case ctText:
break;
case ctImage:
break;
case ctState:
break;
}*/
for( idx=0; idx<m_FilesList.size(); idx++, iter++ )
{
TListItem* pListItem = ListView1->Items->Item[idx];
if( 0 == Item->Caption.AnsiCompareIC(pListItem->Caption))
break;
}
if( iter != m_FilesList.end() )
{
iter->m_Enable = Item->Checked;
iter->m_AliasName = Item->Caption;
}
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::EnableCtrls(void)
{
//TODO: Add your source code here
m_btnDownload->Enabled = true;
m_btnAdd->Enabled = true;
m_btnRemove->Enabled = true;
ComboBox1->Enabled = true;
ListView1->Enabled = true;
//m_nCostTimeSec = 0;
Timer1->Enabled = false;
//Edit1->Text.printf( "%2d : %2d : %2d", 0, 0, 0 );
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::ComboBox1Change(TObject *Sender)
{
int chk_index = ComboBox1->ItemIndex;
m_nComPort = m_COMPortList[chk_index];
StatusBar1->Panels->Items[1]->Text = "COM" + IntToStr(m_COMPortList[chk_index]);
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -