UnitMain.cpp
资源名称:第6章 多线程编程.rar [点击查看]
上传用户:gyjjlc
上传日期:2013-03-29
资源大小:2124k
文件大小:5k
源码类别:
多显示器编程
开发平台:
C++ Builder
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "UnitMain.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- #define COMM_CONFIG_FILE "SetComm.ini"
- TFormMain *FormMain;
- //---------------------------------------------------------------------------
- __fastcall TFormMain::TFormMain(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- //主窗体表单创建事件响应函数
- void __fastcall TFormMain::FormCreate(TObject *Sender)
- {
- AnsiString DevicePort;
- int BaudRate,ByteSize,Parity,StopBits;
- //从初始化文件SetComm.ini中获取串行通信设备的设置参数
- DevicePort=GetCommParameter(&BaudRate,&ByteSize,&Parity,&StopBits);
- //创建串行通信对象实例
- COMX=new TCommunication();
- //初始化串行口
- COMX->ComInitialization(DevicePort,BaudRate,ByteSize,Parity,StopBits);
- //输入串初始化
- input="";
- //创建并启动线程,内部实现以挂起方式启动
- pThread=new ThreadComm(false);
- }
- //---------------------------------------------------------------------------
- //从指定初始化文件中获取串行通信设备的设置参数
- AnsiString __fastcall TFormMain::GetCommParameter(int * pBaudRate,
- int * pByteSize, int * pParity, int * pStopBits)
- {
- char A1[16];
- //文件全路径名。GetCurrentDir取当前目录,C++ Builder系统函数
- AnsiString FileName=GetCurrentDir()+"\"+COMM_CONFIG_FILE;
- //API函数,读取初始化文件的"Communication"节点下"BaudRate"条目,到A1
- GetPrivateProfileString("Communication","BaudRate","9600",A1,15, FileName.c_str());
- //字符串到整形数的变换
- *pBaudRate=AnsiString(A1).ToInt();
- GetPrivateProfileString("Communication","ByteSize","8",A1,15, FileName.c_str());
- *pByteSize=AnsiString(A1).ToInt();
- GetPrivateProfileString("Communication","Parity","无校验",A1,15, FileName.c_str());
- if(AnsiString(A1).AnsiCompare("无校验")==0)
- *pParity=0;
- else if(AnsiString(A1).AnsiCompare("奇校验")==0)
- *pParity=1;
- else
- *pParity=2;
- GetPrivateProfileString("Communication","StopBits","1",A1,15, FileName.c_str());
- if(AnsiString(A1).AnsiCompare("1")==0)
- *pStopBits=0;
- else if(AnsiString(A1).AnsiCompare("1.5")==0)
- *pStopBits=1;
- else
- *pStopBits=2;
- GetPrivateProfileString("Communication","Device","COM1",A1,15, FileName.c_str());
- return AnsiString(A1);
- }
- //---------------------------------------------------------------------------
- //向指定初始化文件中写入串行通信设备的设置参数
- void __fastcall TFormMain::SetCommParameter(char * DeviceP, char * BaudRateP, char * ByteSizeP, char * ParityP, char * StopBitsP)
- {
- AnsiString FileName=GetCurrentDir()+COMM_CONFIG_FILE;
- WritePrivateProfileString("Communication","Device",DeviceP, FileName.c_str());
- WritePrivateProfileString("Communication","BaudRate",BaudRateP, FileName.c_str());
- WritePrivateProfileString("Communication","ByteSize",ByteSizeP, FileName.c_str());
- WritePrivateProfileString("Communication","Parity",ParityP, FileName.c_str());
- WritePrivateProfileString("Communication","StopBits",StopBitsP, FileName.c_str());
- }
- //---------------------------------------------------------------------------
- //RichEdit1控件中键入一字符即按键按下事件响应函数,从串行口发送当前字符
- void __fastcall TFormMain::RichEdit1KeyPress(TObject *Sender, char &Key)
- {
- //调用开始发送函数StartSend,将当前字符发送出去
- StartSend(AnsiString(Key));
- }
- //---------------------------------------------------------------------------
- //打开文件对话框按钮单击事件响应函数
- void __fastcall TFormMain::Button1Click(TObject *Sender)
- {
- //打开打开文件对话框,选择了有效文件的情况...
- if(OpenDialog1->Execute())
- {
- RichEdit1->Lines->Clear();
- //在Edit1中显示文件名
- Edit1->Text=OpenDialog1->FileName;
- //将选中文件打开在Memo1中
- RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
- //使“发送”按钮处于有效状态,允许拥护操作
- Button2->Enabled=true;
- }
- }
- //---------------------------------------------------------------------------
- //启动发送。文件发送情况,即数据块发送情况,以此方式发送首字符,后续字符
- //在发送缓冲区空/事件驱动下完成。对话发送方式,每按下一个字符就相当于发送
- //程度为1字节的数据块,认定这是一种机器等人的情况,不会出现发送覆盖的情况。
- void __fastcall TFormMain::StartSend(AnsiString strString)
- {
- //装订线程中发送缓冲区参数
- pThread->sendBuffer=strString;
- pThread->sendCount=strString.Length();
- //调用线程成员函数发送首字节字符
- pThread->SendOneByte();
- }
- //---------------------------------------------------------------------------
- //“发送”按钮单击事件响应函数,启动数据块发送
- void __fastcall TFormMain::Button2Click(TObject *Sender)
- {
- //自锁,发送期间,禁止被再次按下
- Button2->Enabled=false;
- //将此前打开在Memo1中的文件作为一个字串发送出去
- StartSend(RichEdit1->Lines->Text);
- }
- //---------------------------------------------------------------------------
- //将实时接收到的字符显示出来,线程的同步函数间接调用该函数
- bool __fastcall TFormMain::CommInputProc(AnsiString Cmd)
- {
- //实时接收到的字符串追加到成员变量中
- input=input+Cmd;
- //在Memo2中显示上述成员变量
- RichEdit2->Lines->Text=input;
- return true;
- }
- //---------------------------------------------------------------------------
English
