ctrl + F6 切换源码和对应的 .H 文件。
BCB编译后的程序,在没装过BCB的机器上老提示缺少VCL60.BPL
打开菜单条上的“project | options... ",
选择packages页 ,去掉build with runtime packages 的勾
选择linker页,去掉use dynamic rtl前的勾
选择compiler页,按下release ,确定。
重新编译一下,生成可独立执行的文件
扫描本机串口的代码
- void __fastcall TFMain::FormCreate(TObject *Sender)
- {
- TRegistry *reg = new TRegistry(KEY_READ);
- TStringList *strlist = new TStringList;
- reg->RootKey = HKEY_LOCAL_MACHINE;
- reg->OpenKeyReadOnly("\\HARDWARE\\DEVICEMAP\\SERIALCOMM\\");
- reg->GetValueNames(strlist);
- cbPorts->Clear();
- for( int i=0; i<strlist->Count;i++ ) {
- strlist->Strings[i] = reg->ReadString(strlist->Strings[i]);
- cbPorts->Items->Add(strlist->Strings[i]);
- cbPorts->ItemIndex = 0;
- }
- delete reg,strlist;
- }
线程及其交互操作。
- //---------------------------------------------------------------------------
- /*常用串行通讯API函数及其作用
- 函数名 作用
- CreateFile 打开串口
- GetCommState 检测串口设置
- SetCommState 设置串口
- BuilderCommDCB 用字符串中的值来填充设备控制块
- GetCommTimeouts 检测通信超时设置
- SetCommTimeouts 设置通信超时参数
- SetCommMask 设定被监控事件
- WaitCommEvent 等待被监控事件发生
- WaitForMultipleObjects 等待多个被监测对象的结果
- WriteFile 发送数据
- ReadFile 接收数据
- GetOverlappedResult 返回最后重叠(异步)操作结果
- PurgeComm 清空串口缓冲区,退出所有相关操作
- ClearCommError 更新串口状态结构体,并清除所有串口硬件错误
- CloseHandle 关闭串行口
- */
- #pragma hdrstop
- #include "Uuartcomm.h"
- #define max(a, b) (((a) > (b)) ? (a) : (b))
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- __fastcall TUartCommThread::TUartCommThread(bool CreateSuspended, String uartName, HANDLE formHandle):TThread(CreateSuspended)
- {
- FreeOnTerminate = true;
- FCommPort = uartName;
- FfrmHandle = formHandle;
- }
- void TUartCommThread::SyncDisp(const char *message)
- {
- String *str = new String(message);
- SendMessage(FfrmHandle, WM_MSGCOMM, 0, (long)str);
- delete str;
- }
- void __fastcall TUartCommThread::Execute(void)
- {
- if (Terminated) return;
-
- if (OpenComm()) { //open uart OK
- SyncDisp("Open Uart OK.");
- }
- else { //open uart failed
- SyncDisp("Open Uart Failed.");
- return ;
- }
- try {
- doComm();
- }
- catch(Exception &E) {
- SyncDisp(E.Message.c_str());
- }
- CloseComm();
- }
- bool TUartCommThread::OpenComm(void)
- {
- FCommHandle = CreateFile( FCommPort.c_str(), /* //./com10 */
- GENERIC_READ | GENERIC_WRITE,
- 0, /*not shared */
- NULL, /*no security */
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- 0 /*template*/ );
- if (FCommHandle == INVALID_HANDLE_VALUE) {
- return false;
- }
- SetupComm(FCommHandle, 8*1024, 8*1024);
- //Set TimeOut Info
- COMMTIMEOUTS CommTimeOut;
- GetCommTimeouts( FCommHandle, &CommTimeOut);
- DWORD iWaitTime = 50;
- CommTimeOut.ReadIntervalTimeout = iWaitTime;
- CommTimeOut.ReadTotalTimeoutMultiplier = iWaitTime;
- CommTimeOut.ReadTotalTimeoutConstant = iWaitTime;
- CommTimeOut.WriteTotalTimeoutMultiplier = iWaitTime;
- CommTimeOut.WriteTotalTimeoutConstant = iWaitTime;
- SetCommTimeouts( FCommHandle, &CommTimeOut);
- //Set DCB Info
- DCB dcb;
- GetCommState(FCommHandle, &dcb);
- dcb.DCBlength = sizeof(DCB);
- dcb.BaudRate = BAUD_115200;
- dcb.ByteSize = DATABITS_8 ;
- dcb.Parity = PARITY_NONE; // =0;
- dcb.StopBits = STOPBITS_10; //=0;
- SetCommState(FCommHandle, &dcb);
- //BuildCommDCB("Baud=9600 Parity=N Data=8 Stop=1", &dcb);
- //Get CommProp Info
- //COMMPROP vCommProp;
- //GetCommProperties(FCommHandle, &vCommProp);
- return true;
- }
- void TUartCommThread::CloseComm(void)
- {
- try {
- PurgeComm(FCommHandle, (PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR));
- CloseHandle(FCommHandle);
- }
- catch (...) {
- }
- FCommHandle = INVALID_HANDLE_VALUE;
- }
- unsigned long TUartCommThread::SendData(const byte *Buf, int iLen)
- {
- unsigned long iWrited = 0;
- WriteFile(FCommHandle, Buf, iLen, &iWrited, 0);
- return iWrited;
- }
- unsigned long TUartCommThread::ReadData(byte *Buf, int iLen, DWORD Timeout)
- {
- int dwLength = 0;
- DWORD dwErrorFlags = 0;
- COMSTAT ComStat;
- unsigned long iRcvNum = 0;
- DWORD iSpan, iStart = GetTickCount() ;
- while (dwLength < iLen) {
- ClearCommError(FCommHandle, &dwErrorFlags, &ComStat);
- if (dwErrorFlags > 0) {
- //abort send/read operation and discard all data
- PurgeComm(FCommHandle, (PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR));
- }
- dwLength = ComStat.cbInQue ;
- iSpan = (GetTickCount()- iStart);
- if (iSpan > Timeout) break ;
- if (dwLength < iLen) Sleep(0);
- }
- ReadFile(FCommHandle, Buf, max(iLen, dwLength), &iRcvNum, NULL);
- PurgeComm(FCommHandle, (PURGE_RXABORT | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_TXCLEAR));
- return iRcvNum;
- }
- char TUartCommThread::GetLrc(char *Buf, int iStart, int iLen)
- {
- char fcs = 0x00;
-
- for (int i = 0; i < iLen; i++) {
- fcs = fcs ^ Buf[i + iStart];
- }
- return fcs;
- }
- void TUartCommThread::doComm(void)
- {
-
- }
- //---------------------------------------------------------------------------
- #ifndef UuartcommH
- #define UuartcommH
- #include <vcl.h>
- #include <Classes.hpp>
- #include <Messages.hpp>
- #include <ExtCtrls.hpp>
- #include <Menus.hpp> //包含必要的头文件
- //---------------------------------------------------------------------------
- // Simple threads
- #define WM_MSGCOMM WM_USER + 2
- class TUartCommThread : public TThread
- {
- private:
- HANDLE FfrmHandle; //窗口句柄
- HANDLE FCommHandle; //串口句柄
- String FCommPort; //串口名称
-
- bool OpenComm(void);
- void CloseComm(void);
- unsigned long SendData(const byte *Buf, int iLen);
- unsigned long ReadData(byte *Buf, int iLen, DWORD Timeout);
- char GetLrc(char *Buf, int iStart, int iLen);
- void SyncDisp(const char *message);
-
- protected:
- void __fastcall Execute(void);
- virtual void doComm(void);
-
- public:
- __fastcall TUartCommThread(bool CreateSuspended, String uartName, HANDLE formHandle);
- };
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include "uMain.h"
- #include <Registry.hpp>
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- #pragma resource "*.dfm"
- TFMain *FMain;
- //---------------------------------------------------------------------------
- __fastcall TFMain::TFMain(TComponent* Owner)
- : TForm(Owner)
- {
- }
- //---------------------------------------------------------------------------
- void __fastcall TFMain::btnGetFileClick(TObject *Sender)
- {
- if (dlgSelFile->Execute()) {
- edFile->Text = dlgSelFile->FileName;
- }
- }
- //---------------------------------------------------------------------------
- void __fastcall TFMain::FormCreate(TObject *Sender)
- {
- TRegistry *reg = new TRegistry(KEY_READ);
- TStringList *strlist = new TStringList;
- reg->RootKey = HKEY_LOCAL_MACHINE;
- reg->OpenKeyReadOnly("\\HARDWARE\\DEVICEMAP\\SERIALCOMM\\");
- reg->GetValueNames(strlist);
- cbPorts->Clear();
- for( int i=0; i<strlist->Count;i++ ) {
- strlist->Strings[i] = reg->ReadString(strlist->Strings[i]);
- cbPorts->Items->Add(strlist->Strings[i]);
- cbPorts->ItemIndex = 0;
- }
- delete reg,strlist;
- mmLog->Clear();
- }
- //---------------------------------------------------------------------------
- void __fastcall TFMain::btnDLClick(TObject *Sender)
- {
- commThread = new TUartCommThread(false, cbPorts->Text, this->Handle);
- }
- //---------------------------------------------------------------------------
- void __fastcall TFMain::WndProc(Messages::TMessage &Message)
- {
- if(Message.Msg == WM_MSGCOMM)
- {
- String *str = (String *)(Message.LParam);
- mmLog->Lines->Add(str->c_str());
- }
- TForm::WndProc(Message);
- }
- void __fastcall TFMain::btnConfigClick(TObject *Sender)
- {
- COMMCONFIG vCommConf;
- CommConfigDialog(cbPorts->Text.c_str(), this->Handle, &vCommConf);
- }
- //---------------------------------------------------------------------------
剩下的就是文件读写一类的内容了
阅读(3397) | 评论(0) | 转发(0) |