📄 main.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>
#pragma hdrstop
#include <algorithm>
using std::min;
using std::max;
#include "Main.h"
#include "Option.h"
#include "About.h"
#include "PCOMM.h"
#pragma link "PCOMM.lib"
#define FP_X 250
#define FP_Y 383
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm_Main *Form_Main;
using namespace Gdiplus;
//---------------------------------------------------------------------------
__fastcall TForm_Main::TForm_Main(TComponent* Owner)
: TForm(Owner)
{
DoubleBuffered = true; // 双缓冲
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); // 初始化GDI+
//旋转初始化
R=sqrt(FP_X*FP_X+FP_Y*FP_Y);
PI=4*atan(1);
Beta=atan((float)FP_X/FP_Y);
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::FormClose(TObject *Sender,
TCloseAction &Action)
{
GdiplusShutdown(gdiplusToken); // 闭关GDI+
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::FormCreate(TObject *Sender)
{
StartTimes=false;
Menu_ChartClick(Sender);
//快速按钮初始化
TB_Start->ImageIndex=0;
TB_Pause->ImageIndex=3;
Menu_Pause->Enabled=false;
TB_Option->ImageIndex=4;
TB_Exit->ImageIndex=5;
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Menu_ChartClick(TObject *Sender)
{
//初始化窗体
Width=720;
Height=450;
Color=clMenuBar;
//Chart
Chart->Top=40;
Chart->Left=5;
Chart->Width=700;
Chart->Height=300;
GroupBox_Empty->Top=345;
GroupBox_Empty->Left=24;
GroupBox_Empty->Width=360;
GroupBox_Empty->Height=40;
GroupBox_Rint->Top=345;
GroupBox_Rint->Left=395;
GroupBox_Rint->Width=170;
GroupBox_Rint->Height=40;
GroupBox_Contrl->Top=345;
GroupBox_Contrl->Left=576;
GroupBox_Contrl->Width=120;
GroupBox_Contrl->Height=40;
//显示的控件 Chart,GroupBox_Empty,GroupBox_Contrl
Menu_Chart->Checked=true;
Chart->Visible=true;
GroupBox_Empty->Visible=true;
GroupBox_Contrl->Visible=true;
//隐藏的空间 Paint_Back,Paint_Finger,Paint_Frontal
Paint_Back->Visible=false;
Paint_Finger->Visible=false;
Paint_Frontal->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Menu_GalvaClick(TObject *Sender)
{
//初始化窗体及控件
Height=600;
Width=510;
Paint_Back->Top=40;
Paint_Back->Left=0;
Paint_Back->Width=500;
Paint_Back->Height=500;
Paint_Finger->Top=40;
Paint_Finger->Left=0;
Paint_Finger->Width=500;
Paint_Finger->Height=500;
Paint_Frontal->Top=40;
Paint_Frontal->Left=0;
Paint_Frontal->Width=500;
Paint_Frontal->Height=500;
GroupBox_Rint->Top=35;
GroupBox_Rint->Left=331;
//要显示的控件 Paint_Back,Paint_Finger,Paint_Frontal
Menu_Galva->Checked=true;
Paint_Back->Visible=true;
Paint_Finger->Visible=true;
Paint_Frontal->Visible=true;
//隐藏的控件 Chart,GroupBox_Empty,GroupBox_Rint,GroupBox_Contrl
Chart->Visible=false;
GroupBox_Empty->Visible=false;
GroupBox_Contrl->Visible=false;
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::ChartDblClick(TObject *Sender)
{
Menu_GalvaClick(Sender);//双击即转换为模拟检流计画面
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Paint_FrontalDblClick(TObject *Sender)
{
Menu_ChartClick(Sender); //双击即转换为图表显示画面
}
//---------------------------------------------------------------------------
void __stdcall Cntirq(int port)
{
int Len =sio_read( port, Form_Main->Rchar, 50);
for(int i=0;i<Len;i++)
{
if (Form_Main->Rchar[i]=='-') //判断受到数据的符号
{Form_Main->minus=true; i++;}
if (Form_Main->Rchar[i]=='\r')//是否发送完毕?
{
if(Form_Main->minus==true)
{ //负数的调整
Form_Main->Rint=-Form_Main->Rint;
Form_Main->minus=false;
}
//当前值显示栏
Form_Main->Label1->Caption=(float)Form_Main->Rint/10;
//图表显示,60个换一页
Form_Main->Series1->AddY((float)Form_Main->Rint/10);
Form_Main->t++;
if(Form_Main->t>61+Form_Main->TLength)
Form_Main->TLength+=60;
Form_Main->Chart->BottomAxis->SetMinMax(Form_Main->TLength,60+Form_Main->TLength);
Form_Main->TChange=Form_Main->TLength;
//模拟检流计的显示
if(Form_Main->Rint>300)
Form_Main->Theta=38; //如果输入过大,检流计指针过偏显示
else if(Form_Main->Rint<-300)
Form_Main->Theta=-38;
else Form_Main->Theta=(float)Form_Main->Rint*36/300;
{ //调整角度
Form_Main->Rsin=Form_Main->R*sin(Form_Main->Beta-Form_Main->Theta*Form_Main->PI/180);
Form_Main->Rcos=Form_Main->R*cos(Form_Main->Beta-Form_Main->Theta*Form_Main->PI/180);
Form_Main->T_height=(float)FP_X-(float)Form_Main->Rsin;
Form_Main->T_width=(float)FP_Y-(float)Form_Main->Rcos;
Form_Main->Paint_Finger->Repaint(); //刷新数据
}
Form_Main->Rint=0;
i+=2; //校准到下个数因为430发送的结束符为'\r''\n'两字符
}
else
Form_Main->Rint=Form_Main->Rint*10+Form_Main->Rchar[i]-48; //计算接收数据
}
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Menu_StartClick(TObject *Sender)
{
if(StartTimes==false)
{
Menu_OptionClick(Sender);
}
else
{
Form_Option->Port=Form_Option->Combo_Port->ItemIndex+1; //调整COM口
PortReturn=sio_open(Form_Option->Port); //开串口
//错误提示或执行
if (PortReturn==SIO_OPENFAIL)
{
Application->MessageBoxA
("没有此端口或另一个程序正在使用选定的端口。","提示",MB_OK);
}
else if (PortReturn==SIO_OK)
{
Application->MessageBoxA("连接成功!","提示",MB_OK);
sio_ioctl(Form_Option->Port,Form_Option->Baud,Form_Option->Mode);
sio_flush(Form_Option->Port,0);
Menu_Start->Enabled=false;
TB_Start->ImageIndex=1;
Menu_Pause->Enabled=true;
TB_Pause->ImageIndex=2;
TB_Pause->Enabled=true;
}
else if (PortReturn==SIO_BADPORT)
{
Application->MessageBox
("端口不可用。请核实后再连接。","提示",MB_OK);
}
else
{
Application->MessageBoxA("发生错误。请检查。","提示",MB_OK);
}
void __stdcall ( *p)(int);
p = Cntirq ;
sio_cnt_irq( Form_Option->Port, *p, 1 );
//读串口,要先建立Cntirq函数。
}
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Menu_OptionClick(TObject *Sender)
{
if(StartTimes==false) //如果第一次执行串口,则显示属性窗。
Application->CreateForm(__classid(TForm_Option), &Form_Option);
Form_Option->Show();
if(Form_Main->Menu_Start->Enabled==false)
{//如果已经开其串口,属性窗只显示数据而不能更改
Form_Option->Combo_Port->Enabled=false;
Form_Option->Combo_Baud->Enabled=false;
Form_Option->Combo_Data->Enabled=false;
Form_Option->Combo_Parity->Enabled=false;
Form_Option->Combo_Stop->Enabled=false;
}
else
{
Form_Option->Combo_Port->Enabled=true;
Form_Option->Combo_Baud->Enabled=true;
Form_Option->Combo_Data->Enabled=true;
Form_Option->Combo_Parity->Enabled=true;
Form_Option->Combo_Stop->Enabled=true;
}
Enabled=false; //开启属性窗时主窗体不可用
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Menu_PauseClick(TObject *Sender)
{
sio_close(Form_Option->Port); //关闭串口
Menu_Start->Enabled=true; //开始命令转为可以状态
TB_Start->ImageIndex=0; //开始快速键显示颜色
Menu_Pause->Enabled=false; //暂停键不可用
TB_Pause->ImageIndex=3; //暂停快速键显示灰色
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Menu_ExitClick(TObject *Sender)
{
if(Menu_Start->Enabled==false)
sio_close(Form_Option->Port);
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Quick_LLClick(TObject *Sender)
{
//"<<"键,向前翻页
if(TChange>60)
TChange-=60;
else
TChange=0;
Chart->BottomAxis->SetMinMax(TChange,60+TChange);
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Quick_RRClick(TObject *Sender)
{
//">>"键,向后翻页
if(TChange>=TLength-60)
TChange=TLength;
else
TChange+=60;
Chart->BottomAxis->SetMinMax(TChange,60+TChange);
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Quick_LClick(TObject *Sender)
{
//"<"键,向前走一个
if(TChange>0)
TChange-=1;
else
TChange=0;
Chart->BottomAxis->SetMinMax(TChange,60+TChange);
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Quick_RClick(TObject *Sender)
{
//">" 向后走一个
if(TChange>=TLength)
TChange=TLength;
else
TChange+=1;
Chart->BottomAxis->SetMinMax(TChange,60+TChange);
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Paint_BackPaint(TObject *Sender)
{
//刷新BACK画面
Gdiplus::Graphics BGImage(Paint_Back->Canvas->Handle);
Gdiplus::Image imageBG(L"Image\\Back.png");
BGImage.DrawImage(&imageBG, 0, 0, imageBG.GetWidth(), imageBG.GetHeight());
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Paint_FingerPaint(TObject *Sender)
{
//刷新Finger画面
Gdiplus::Graphics FGImage(Paint_Finger->Canvas->Handle);
Gdiplus::Image imageFG(L"Image\\Finger.png");
FGImage.TranslateTransform(T_height,T_width);//移动坐标原点
FGImage.RotateTransform((float)Theta);//旋转坐标系
FGImage.DrawImage(&imageFG, 0, 0,imageFG.GetWidth(), imageFG.GetHeight());
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Paint_FrontalPaint(TObject *Sender)
{
//刷新Frontal画面
Gdiplus::Graphics FTImage(Paint_Frontal->Canvas->Handle);
Gdiplus::Bitmap imageFT(L"Image\\Frontal.png");
FTImage.DrawImage(&imageFT, 0, 0, imageFT.GetWidth(), imageFT.GetHeight());
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Menu_AboutClick(TObject *Sender)
{
Application->CreateForm(__classid(TForm_About), &Form_About);
Form_About->Show();
}
//---------------------------------------------------------------------------
void __fastcall TForm_Main::Menu_HelpMClick(TObject *Sender)
{
ShellExecute(this->Handle,"open","Image\\模拟检流计.pdf","","",SW_SHOW );
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -