📄 unit1.~cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include <stdio.h>
#include <math.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "MSCommLib_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//设置串口信息
void Set_Uart(void)
{
String Uartsettings;
if(Form1->MSComm1->PortOpen)Form1->MSComm1->PortOpen=False;//关闭串口
Uartsettings=Uart_Bond+","; //得到波特率
Uartsettings+=Uart_Chek.SubString(1,1)+",";//得到N/O/E
Uartsettings+=Uart_Data+",";//得到数据位
Uartsettings+=Uart_Stop; //得到停止位
try
{
Form1->MSComm1->Settings=Uartsettings;//设置串口信息
}
catch(Exception&e)//错误处理
{
Application->MessageBox("设置不正确!请检查...","系统错误",MB_OK);
}
Form1->MSComm1->CommPort=Uart_Port; //设置到新的串口编号
try
{
Form1->MSComm1->PortOpen=True;//打开串口
}
catch(Exception&exception)//错误处理
{
Uart_Open=false;
Application->MessageBox("该串口不存在或串口被占用!请检查...","系统错误",MB_OK);
Form1->U_Status->Brush->Color=clBlack;
Form1->U_OPEN->Caption="打开串口";
return;
}
Form1->MSComm1->PortOpen=False;//关闭串口
Sleep(10);
Form1->MSComm1->PortOpen = True;//打开串口
if(!Uart_Open)Form1->MSComm1->PortOpen=False;//关闭串口
}
void FileSend_Juder(void)
{
if(Form1->Send_Files->Enabled)
{
SendFileEn=false;//关闭文件发送功能
Form1->Send_Button->Enabled=true;//启动发送按钮
Form1->Auto_Send->Enabled=true;//自动发送启动
Form1->Timer2->Interval=1000;//1000ms发送一个数据
Form1->Timer2->Enabled=false;//发送中断关闭
Form1->Send_Files->Caption="发送文件";
Form1->Send_Files->Enabled=false;//关闭发送按钮
Form1->Send_Box->Clear();//清除发送窗口的文字
}
}
//初始化时设置
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
MSComm1->InputLen = 1;//设置或返回一次从接收缓冲区中读取字节数,0表示一次读取所有数据
MSComm1->InBufferSize=8192; //设置接收缓冲区512Byte
MSComm1->InBufferCount=0;
MSComm1->OutBufferSize =8192; //设置发送缓冲区512Byte
MSComm1->OutBufferCount = 0;
MSComm1->CommPort = 1; //设置通讯串口
//On Error GoTo comer
MSComm1->InputMode = comInputModeBinary;
MSComm1->RThreshold = 1;//每个字符到接收缓冲区都触发接收事件
MSComm1->SThreshold = 0;//发送不产生中断
MSComm1->InBufferCount = 0;
MSComm1->OutBufferCount = 0;
Set_Uart();//设置串口信息
//以上为串口的初始化
T_Type_Hex->Checked=true;//默认选择Hex显示
T_AutoClear->Checked=true;//默认自动清空
T_Type_Hex->Enabled=false;//屏蔽格式选择
T_Type_Dec->Enabled=false;
Send_Files->Enabled=false;//关闭文件发送按钮
Label10->Caption="正点原子制作V1.5 ";//版本显示
Form1->Caption=Label10->Caption;//改变显示标题
Send_Box->Clear();//发送区清空
DISPLAY->Clear(); //接收区清空
AutoSend_Time->Text=1000;//默认设置
Display_Type=0;//字符显示方式
}
//---------------------------------------------------------------------------
void __fastcall TForm1::S_ClearClick(TObject *Sender)
{
FileSend_Juder();//发送文件退出处理
Send_Box->Clear();//清空发送区
}
//---------------------------------------------------------------------------
//串口打开与关闭控制
void __fastcall TForm1::U_OPENClick(TObject *Sender)
{
if(!Uart_Open)//如果没有打开,则打开串口
{
Uart_Open=true;
U_Status->Brush->Color=clRed;
U_OPEN->Caption="关闭串口";
if(!MSComm1->PortOpen)Set_Uart();//打开串口
}else//串口已经打开,则关闭串口
{
Uart_Open=false;
U_Status->Brush->Color=clBlack;
U_OPEN->Caption="打开串口";
if(MSComm1->PortOpen)
MSComm1->PortOpen=False;//关闭串口
}
}
//---------------------------------------------------------------------------
//改变串口编号
void __fastcall TForm1::U_PortChange(TObject *Sender)
{
Uart_Port=U_Port->ItemIndex+1; //得到串口编号
Set_Uart();//设定串口信息
}
//---------------------------------------------------------------------------
//改变波特率
void __fastcall TForm1::U_BondChange(TObject *Sender)
{
Uart_Bond=U_Bond->Items->Strings[U_Bond->ItemIndex];//得到设定值波特率
Set_Uart();//设定串口信息
}
//---------------------------------------------------------------------------
//改变奇偶校验
void __fastcall TForm1::U_CheckChange(TObject *Sender)
{
Uart_Chek=U_Check->Items->Strings[U_Check->ItemIndex];
Set_Uart();//设定串口信息
}
//---------------------------------------------------------------------------
//改变数据位
void __fastcall TForm1::U_DataChange(TObject *Sender)
{
Uart_Data=U_Data->Items->Strings[U_Data->ItemIndex];
Set_Uart();//设定串口信息
}
//---------------------------------------------------------------------------
//改变停止位
void __fastcall TForm1::U_StopChange(TObject *Sender)
{
Uart_Stop=U_Stop->Items->Strings[U_Stop->ItemIndex];
Set_Uart();//设定串口信息
}
//---------------------------------------------------------------------------
void __fastcall TForm1::T_TypeSetClick(TObject *Sender)
{
if(T_TypeSet->Checked)//选中
{
T_Type_Hex->Enabled=true;
T_Type_Dec->Enabled=true;
if(T_Type_Hex->Checked)Display_Type=1;//十六进制显示
else Display_Type=2;//十进制显示
}else //没有选中
{
T_Type_Hex->Enabled=false;
T_Type_Dec->Enabled=false;
Display_Type=0;//字符显示
}
}
//---------------------------------------------------------------------------
//清空接收区数据
void __fastcall TForm1::T_ClearClick(TObject *Sender)
{
DISPLAY->Clear();//清空显示
}
//---------------------------------------------------------------------------
bool PathOk=false;//路径是否存在
//文件保存
void __fastcall TForm1::T_SaveClick(TObject *Sender)
{
if(PathOk==false&&Form1->SaveDialog1->Execute())//如果执行有效则继续执行
{
PathOk=true;//标记路径
Form1->DISPLAY->Lines->SaveToFile(Form1->SaveDialog1->FileName);
//首先取得文件名称和路径,然后保存文件
}
if(PathOk)Form1->DISPLAY->Lines->SaveToFile(Form1->SaveDialog1->FileName);//直接保存
}
//---------------------------------------------------------------------------
//另存为
void __fastcall TForm1::T_SaveAsClick(TObject *Sender)
{
if(Form1->SaveDialog1->Execute())
{
PathOk=true;//标记
Form1->DISPLAY->Lines->SaveToFile(Form1->SaveDialog1->FileName);
//首先取得文件名称和路径,然后保存文件
}
}
//---------------------------------------------------------------------------
//每一毫秒输出一个字符
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
AnsiString tStr;//暂存一行的信息
char c[4];
uchar Rx_Buffer; //接收缓冲的字节
OleVariant temp; //声明变体变量
if(!Form1->MSComm1->InBufferCount)return;//接收缓冲无数据了,则不在执行下面步骤
if(Display_Enable&&Uart_Open)//使能显示(显示使能&串口打开)
{
temp=Form1->MSComm1->Input;
VarArrayRedim((Variant&)temp,1);
Rx_Buffer=(unsigned char)temp.GetElement(0);
switch(Display_Type)
{
case 0://字符显示
{
if(Rx_Buffer==10){Form1->DISPLAY->Lines->Add("");}//换行
else if(Rx_Buffer>0x80)//是汉字
{
c[0]=Rx_Buffer;
Sleep(2);//确保汉字的第二个部分字节收到
temp=Form1-> MSComm1->Input;//读取第二个数
VarArrayRedim((Variant&)temp,1);
Rx_Buffer=(unsigned char)temp.GetElement(0);
c[1]=Rx_Buffer;
c[2]='\0';
tStr=Form1->DISPLAY->Lines->Strings[Form1->DISPLAY->Lines->Count-1]+c;
}else tStr=Form1->DISPLAY->Lines->Strings[Form1->DISPLAY->Lines->Count-1]+(char)Rx_Buffer;
break;
}
case 1://十六进制显示
{
ltoa((long)Rx_Buffer,c,16);//转换16进制
if(c[0]>=97&&c[0]<=122)c[0]-=32;//把小写变为大写
if(c[1]>=97&&c[1]<=122)c[1]-=32;//把小写变为大写
if(c[1]=='\0')//接收单个HEX转换为标准格式 如B-->0B
{
c[1]=c[0];
c[0]='0';
}
c[2]='\0';
tStr=Form1->DISPLAY->Lines->Strings[Form1->DISPLAY->Lines->Count-1]+c+" ";
break;
}
case 2://十进制显示
{
ltoa((long)Rx_Buffer,c,10);//转换10进制
if(c[1]=='\0')//接收个位数据
{
c[2]=c[0];
c[1]=c[0]='0';
}else if(c[2]=='\0')//接收双位数据
{
c[2]=c[1];
c[1]=c[0];
c[0]='0';
}
c[3]='\0';//给c添上结束符
tStr=Form1->DISPLAY->Lines->Strings[Form1->DISPLAY->Lines->Count-1]+c+" ";
break;
}
}
Form1-> DISPLAY->Lines->Strings[Form1-> DISPLAY->Lines->Count-1]=tStr;//显示汉字或字符
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -