Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9396368
  • 博文数量: 1747
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 20060
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1747)

文章存档

2024年(23)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

BCB

分类: C/C++

2011-10-09 10:28:36

ctrl + F6 切换源码和对应的 .H 文件。
 
BCB编译后的程序,在没装过BCB的机器上老提示缺少VCL60.BPL
 
打开菜单条上的“project | options... ",
选择packages页 ,去掉build with runtime packages 的勾
选择linker页,去掉use dynamic rtl前的勾
选择compiler页,按下release ,确定。
 
重新编译一下,生成可独立执行的文件
 
扫描本机串口的代码
  1. void __fastcall TFMain::FormCreate(TObject *Sender)
  2. {
  3.     TRegistry *reg = new TRegistry(KEY_READ);
  4.     TStringList *strlist = new TStringList;
  5.     reg->RootKey = HKEY_LOCAL_MACHINE;
  6.     reg->OpenKeyReadOnly("\\HARDWARE\\DEVICEMAP\\SERIALCOMM\\");
  7.     reg->GetValueNames(strlist);
  8.     cbPorts->Clear();
  9.     for( int i=0; i<strlist->Count;i++ ) {
  10.         strlist->Strings[i] = reg->ReadString(strlist->Strings[i]);
  11.         cbPorts->Items->Add(strlist->Strings[i]);
  12.         cbPorts->ItemIndex = 0;
  13.     }
  14.     delete reg,strlist;
  15. }

线程及其交互操作。

 

  1. //---------------------------------------------------------------------------
  2. /*常用串行通讯API函数及其作用
  3.     函数名 作用
  4.     CreateFile 打开串口
  5.     GetCommState 检测串口设置
  6.     SetCommState 设置串口
  7.     BuilderCommDCB 用字符串中的值来填充设备控制块
  8.     GetCommTimeouts 检测通信超时设置
  9.     SetCommTimeouts 设置通信超时参数
  10.     SetCommMask 设定被监控事件
  11.     WaitCommEvent 等待被监控事件发生
  12.     WaitForMultipleObjects 等待多个被监测对象的结果
  13.     WriteFile 发送数据
  14.     ReadFile 接收数据
  15.     GetOverlappedResult 返回最后重叠(异步)操作结果
  16.     PurgeComm 清空串口缓冲区,退出所有相关操作
  17.     ClearCommError 更新串口状态结构体,并清除所有串口硬件错误
  18.     CloseHandle 关闭串行口
  19. */

  20. #pragma hdrstop

  21. #include "Uuartcomm.h"
  22. #define max(a, b) (((a) > (b)) ? (a) : (b))




  23. //---------------------------------------------------------------------------

  24. #pragma package(smart_init)


  25. __fastcall TUartCommThread::TUartCommThread(bool CreateSuspended, String uartName, HANDLE formHandle):TThread(CreateSuspended)
  26. {
  27.     FreeOnTerminate = true;
  28.     FCommPort = uartName;
  29.     FfrmHandle = formHandle;
  30. }

  31. void TUartCommThread::SyncDisp(const char *message)
  32. {
  33.     String *str = new String(message);
  34.     SendMessage(FfrmHandle, WM_MSGCOMM, 0, (long)str);
  35.     delete str;
  36. }

  37. void __fastcall TUartCommThread::Execute(void)
  38. {
  39.     if (Terminated) return;
  40.     
  41.     if (OpenComm()) { //open uart OK
  42.        SyncDisp("Open Uart OK.");
  43.     }
  44.     else { //open uart failed
  45.       SyncDisp("Open Uart Failed.");
  46.       return ;
  47.     }

  48.     try {
  49.         doComm();
  50.     }
  51.     catch(Exception &E) {
  52.         SyncDisp(E.Message.c_str());
  53.     }

  54.     CloseComm();
  55. }

  56. bool TUartCommThread::OpenComm(void)
  57. {
  58.     FCommHandle = CreateFile( FCommPort.c_str(), /* //./com10 */
  59.                                  GENERIC_READ | GENERIC_WRITE,
  60.                                  0, /*not shared */
  61.                                  NULL, /*no security */
  62.                                  OPEN_EXISTING,
  63.                                  FILE_ATTRIBUTE_NORMAL,
  64.                                  0 /*template*/ );
  65.     if (FCommHandle == INVALID_HANDLE_VALUE) {
  66.         return false;
  67.     }

  68.     SetupComm(FCommHandle, 8*1024, 8*1024);

  69.     //Set TimeOut Info
  70.     COMMTIMEOUTS CommTimeOut;
  71.     GetCommTimeouts( FCommHandle, &CommTimeOut);
  72.     DWORD iWaitTime = 50;
  73.     CommTimeOut.ReadIntervalTimeout = iWaitTime;
  74.     CommTimeOut.ReadTotalTimeoutMultiplier = iWaitTime;
  75.     CommTimeOut.ReadTotalTimeoutConstant = iWaitTime;
  76.     CommTimeOut.WriteTotalTimeoutMultiplier = iWaitTime;
  77.     CommTimeOut.WriteTotalTimeoutConstant = iWaitTime;
  78.     SetCommTimeouts( FCommHandle, &CommTimeOut);

  79.     //Set DCB Info
  80.     DCB dcb;
  81.     GetCommState(FCommHandle, &dcb);
  82.     dcb.DCBlength = sizeof(DCB);
  83.     dcb.BaudRate = BAUD_115200;
  84.     dcb.ByteSize = DATABITS_8 ;
  85.     dcb.Parity = PARITY_NONE; // =0;
  86.     dcb.StopBits = STOPBITS_10; //=0;
  87.     SetCommState(FCommHandle, &dcb);

  88.     //BuildCommDCB("Baud=9600 Parity=N Data=8 Stop=1", &dcb);
  89.     //Get CommProp Info
  90.     //COMMPROP vCommProp;
  91.     //GetCommProperties(FCommHandle, &vCommProp);

  92.     return true;
  93. }

  94. void TUartCommThread::CloseComm(void)
  95. {
  96.     try {
  97.         PurgeComm(FCommHandle, (PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR));
  98.         CloseHandle(FCommHandle);
  99.     }
  100.     catch (...) {
  101.     }
  102.     FCommHandle = INVALID_HANDLE_VALUE;
  103. }

  104. unsigned long TUartCommThread::SendData(const byte *Buf, int iLen)
  105. {
  106.     unsigned long iWrited = 0;
  107.     WriteFile(FCommHandle, Buf, iLen, &iWrited, 0);
  108.     return iWrited;
  109. }

  110. unsigned long TUartCommThread::ReadData(byte *Buf, int iLen, DWORD Timeout)
  111. {
  112.     int dwLength = 0;
  113.     DWORD dwErrorFlags = 0;
  114.     COMSTAT ComStat;
  115.     unsigned long iRcvNum = 0;

  116.     DWORD iSpan, iStart = GetTickCount() ;
  117.     while (dwLength < iLen) {
  118.         ClearCommError(FCommHandle, &dwErrorFlags, &ComStat);
  119.         if (dwErrorFlags > 0) {
  120.             //abort send/read operation and discard all data
  121.             PurgeComm(FCommHandle, (PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR));
  122.         }
  123.         dwLength = ComStat.cbInQue ;

  124.         iSpan = (GetTickCount()- iStart);

  125.         if (iSpan > Timeout) break ;
  126.         if (dwLength < iLen) Sleep(0);
  127.     }

  128.     ReadFile(FCommHandle, Buf, max(iLen, dwLength), &iRcvNum, NULL);
  129.     PurgeComm(FCommHandle, (PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR));

  130.     return iRcvNum;
  131. }

  132. char TUartCommThread::GetLrc(char *Buf, int iStart, int iLen)
  133. {
  134.     char fcs = 0x00;
  135.     
  136.     for (int i = 0; i < iLen; i++) {
  137.         fcs = fcs ^ Buf[i + iStart];
  138.     }

  139.     return fcs;
  140. }

  141. void TUartCommThread::doComm(void)
  142. {
  143.         
  144. }

 

  1. //---------------------------------------------------------------------------

  2. #ifndef UuartcommH
  3. #define UuartcommH

  4. #include <vcl.h>
  5. #include <Classes.hpp>
  6. #include <Messages.hpp>
  7. #include <ExtCtrls.hpp>
  8. #include <Menus.hpp> //包含必要的头文件

  9. //---------------------------------------------------------------------------
  10. // Simple threads

  11. #define WM_MSGCOMM WM_USER + 2

  12. class TUartCommThread : public TThread
  13. {
  14.   private:
  15.     HANDLE FfrmHandle; //窗口句柄
  16.     HANDLE FCommHandle; //串口句柄
  17.     String FCommPort; //串口名称
  18.                 
  19.     bool OpenComm(void);
  20.     void CloseComm(void);

  21.     unsigned long SendData(const byte *Buf, int iLen);
  22.     unsigned long ReadData(byte *Buf, int iLen, DWORD Timeout);

  23.     char GetLrc(char *Buf, int iStart, int iLen);

  24.     void SyncDisp(const char *message);
  25.     
  26.   protected:
  27.     void __fastcall Execute(void);
  28.     virtual void doComm(void);
  29.       
  30.   public:
  31.     __fastcall TUartCommThread(bool CreateSuspended, String uartName, HANDLE formHandle);

  32. };

 

  1. //---------------------------------------------------------------------------

  2. #include <vcl.h>
  3. #pragma hdrstop

  4. #include "uMain.h"
  5. #include <Registry.hpp>
  6. //---------------------------------------------------------------------------
  7. #pragma package(smart_init)
  8. #pragma resource "*.dfm"
  9. TFMain *FMain;
  10. //---------------------------------------------------------------------------
  11. __fastcall TFMain::TFMain(TComponent* Owner)
  12.         : TForm(Owner)
  13. {
  14. }
  15. //---------------------------------------------------------------------------
  16. void __fastcall TFMain::btnGetFileClick(TObject *Sender)
  17. {
  18.     if (dlgSelFile->Execute()) {
  19.         edFile->Text = dlgSelFile->FileName;
  20.     }
  21. }
  22. //---------------------------------------------------------------------------
  23. void __fastcall TFMain::FormCreate(TObject *Sender)
  24. {
  25.     TRegistry *reg = new TRegistry(KEY_READ);
  26.     TStringList *strlist = new TStringList;
  27.     reg->RootKey = HKEY_LOCAL_MACHINE;
  28.     reg->OpenKeyReadOnly("\\HARDWARE\\DEVICEMAP\\SERIALCOMM\\");
  29.     reg->GetValueNames(strlist);
  30.     cbPorts->Clear();
  31.     for( int i=0; i<strlist->Count;i++ ) {
  32.         strlist->Strings[i] = reg->ReadString(strlist->Strings[i]);
  33.         cbPorts->Items->Add(strlist->Strings[i]);
  34.         cbPorts->ItemIndex = 0;
  35.     }
  36.     delete reg,strlist;

  37.     mmLog->Clear();
  38. }
  39. //---------------------------------------------------------------------------
  40. void __fastcall TFMain::btnDLClick(TObject *Sender)
  41. {
  42.     commThread = new TUartCommThread(false, cbPorts->Text, this->Handle);
  43. }
  44. //---------------------------------------------------------------------------

  45. void __fastcall TFMain::WndProc(Messages::TMessage &Message)
  46. {
  47.     if(Message.Msg == WM_MSGCOMM)
  48.     {
  49.         String *str = (String *)(Message.LParam);
  50.         mmLog->Lines->Add(str->c_str());
  51.     }

  52.     TForm::WndProc(Message);
  53. }
  54. void __fastcall TFMain::btnConfigClick(TObject *Sender)
  55. {
  56.     COMMCONFIG vCommConf;
  57.     CommConfigDialog(cbPorts->Text.c_str(), this->Handle, &vCommConf);
  58. }
  59. //---------------------------------------------------------------------------

剩下的就是文件读写一类的内容了

 

阅读(3397) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~