UnitMain.cpp
上传用户:gyjjlc
上传日期:2013-03-29
资源大小:2124k
文件大小:5k
源码类别:

多显示器编程

开发平台:

C++ Builder

  1. //---------------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4. #include "UnitMain.h"
  5. //---------------------------------------------------------------------------
  6. #pragma package(smart_init)
  7. #pragma resource "*.dfm"
  8. #define COMM_CONFIG_FILE "SetComm.ini"
  9. TFormMain *FormMain;
  10. //---------------------------------------------------------------------------
  11. __fastcall TFormMain::TFormMain(TComponent* Owner)
  12.         : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. //主窗体表单创建事件响应函数
  17. void __fastcall TFormMain::FormCreate(TObject *Sender)
  18. {
  19.     AnsiString DevicePort;
  20.     int BaudRate,ByteSize,Parity,StopBits;
  21.     //从初始化文件SetComm.ini中获取串行通信设备的设置参数
  22.     DevicePort=GetCommParameter(&BaudRate,&ByteSize,&Parity,&StopBits);
  23.     //创建串行通信对象实例
  24.     COMX=new TCommunication();
  25.     //初始化串行口
  26.     COMX->ComInitialization(DevicePort,BaudRate,ByteSize,Parity,StopBits);
  27.     //输入串初始化
  28.     input="";
  29.     //创建并启动线程,内部实现以挂起方式启动
  30.     pThread=new ThreadComm(false);
  31. }
  32. //---------------------------------------------------------------------------
  33. //从指定初始化文件中获取串行通信设备的设置参数
  34. AnsiString __fastcall TFormMain::GetCommParameter(int * pBaudRate,
  35.      int * pByteSize, int * pParity, int * pStopBits)
  36. {
  37.     char A1[16];
  38.     //文件全路径名。GetCurrentDir取当前目录,C++ Builder系统函数
  39.     AnsiString FileName=GetCurrentDir()+"\"+COMM_CONFIG_FILE;
  40.     //API函数,读取初始化文件的"Communication"节点下"BaudRate"条目,到A1
  41.     GetPrivateProfileString("Communication","BaudRate","9600",A1,15, FileName.c_str());
  42.     //字符串到整形数的变换
  43.     *pBaudRate=AnsiString(A1).ToInt();
  44.     GetPrivateProfileString("Communication","ByteSize","8",A1,15, FileName.c_str());
  45.     *pByteSize=AnsiString(A1).ToInt();
  46.     GetPrivateProfileString("Communication","Parity","无校验",A1,15, FileName.c_str());
  47.     if(AnsiString(A1).AnsiCompare("无校验")==0)
  48.         *pParity=0;
  49.     else if(AnsiString(A1).AnsiCompare("奇校验")==0)
  50.         *pParity=1;
  51.     else
  52.         *pParity=2;
  53.     GetPrivateProfileString("Communication","StopBits","1",A1,15, FileName.c_str());
  54.     if(AnsiString(A1).AnsiCompare("1")==0)
  55.         *pStopBits=0;
  56.     else if(AnsiString(A1).AnsiCompare("1.5")==0)
  57.         *pStopBits=1;
  58.     else
  59.         *pStopBits=2;
  60.     GetPrivateProfileString("Communication","Device","COM1",A1,15, FileName.c_str());
  61.     return AnsiString(A1);
  62. }
  63. //---------------------------------------------------------------------------
  64. //向指定初始化文件中写入串行通信设备的设置参数
  65. void __fastcall TFormMain::SetCommParameter(char * DeviceP, char * BaudRateP, char * ByteSizeP, char * ParityP, char * StopBitsP)
  66. {
  67.     AnsiString FileName=GetCurrentDir()+COMM_CONFIG_FILE;
  68.     WritePrivateProfileString("Communication","Device",DeviceP, FileName.c_str());
  69.     WritePrivateProfileString("Communication","BaudRate",BaudRateP, FileName.c_str());
  70.     WritePrivateProfileString("Communication","ByteSize",ByteSizeP, FileName.c_str());
  71.     WritePrivateProfileString("Communication","Parity",ParityP, FileName.c_str());
  72.     WritePrivateProfileString("Communication","StopBits",StopBitsP, FileName.c_str());
  73. }
  74. //---------------------------------------------------------------------------
  75. //RichEdit1控件中键入一字符即按键按下事件响应函数,从串行口发送当前字符
  76. void __fastcall TFormMain::RichEdit1KeyPress(TObject *Sender, char &Key)
  77. {
  78.     //调用开始发送函数StartSend,将当前字符发送出去
  79.     StartSend(AnsiString(Key));
  80. }
  81. //---------------------------------------------------------------------------
  82. //打开文件对话框按钮单击事件响应函数
  83. void __fastcall TFormMain::Button1Click(TObject *Sender)
  84. {
  85.     //打开打开文件对话框,选择了有效文件的情况...
  86.     if(OpenDialog1->Execute())
  87.     {
  88.         RichEdit1->Lines->Clear();
  89.         //在Edit1中显示文件名
  90.         Edit1->Text=OpenDialog1->FileName;
  91.         //将选中文件打开在Memo1中
  92.         RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
  93.         //使“发送”按钮处于有效状态,允许拥护操作
  94.         Button2->Enabled=true;
  95.     }
  96. }
  97. //---------------------------------------------------------------------------
  98. //启动发送。文件发送情况,即数据块发送情况,以此方式发送首字符,后续字符
  99. //在发送缓冲区空/事件驱动下完成。对话发送方式,每按下一个字符就相当于发送
  100. //程度为1字节的数据块,认定这是一种机器等人的情况,不会出现发送覆盖的情况。
  101. void __fastcall TFormMain::StartSend(AnsiString strString)
  102. {
  103.     //装订线程中发送缓冲区参数
  104.     pThread->sendBuffer=strString;
  105.     pThread->sendCount=strString.Length();
  106.     //调用线程成员函数发送首字节字符
  107.     pThread->SendOneByte();
  108. }
  109. //---------------------------------------------------------------------------
  110. //“发送”按钮单击事件响应函数,启动数据块发送
  111. void __fastcall TFormMain::Button2Click(TObject *Sender)
  112. {
  113.     //自锁,发送期间,禁止被再次按下
  114.     Button2->Enabled=false;
  115.     //将此前打开在Memo1中的文件作为一个字串发送出去
  116.     StartSend(RichEdit1->Lines->Text);
  117. }
  118. //---------------------------------------------------------------------------
  119. //将实时接收到的字符显示出来,线程的同步函数间接调用该函数
  120. bool __fastcall TFormMain::CommInputProc(AnsiString Cmd)
  121. {
  122.     //实时接收到的字符串追加到成员变量中
  123.     input=input+Cmd;
  124.     //在Memo2中显示上述成员变量
  125.     RichEdit2->Lines->Text=input;
  126.     return true;
  127. }
  128. //---------------------------------------------------------------------------