請問 如何 讀取 comport 呢? |
缺席
|
elva349
一般會員 發表:15 回覆:21 積分:17 註冊:2007-04-17 發送簡訊給我 |
目前 我已經能開啟comport 把我 Edit1->Text 輸入的資料傳出去
我目前的程式碼是這樣 [code cpp] HANDLE h_RS232; h_RS232=CreateFile(ComboBox1->Text.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); void __fastcall TForm1::BitBtn3Click(TObject *Sender) { if ((h_RS232)&&(Edit1->Text!="")) { DCB dcb; ZeroMemory(&dcb,sizeof(dcb)); dcb.DCBlength=sizeof(dcb); dcb.BaudRate=CBR_9600; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=ONESTOPBIT; if (SetCommState(h_RS232,&dcb)) { DWORD ByteCount; WriteFile(h_RS232, Edit1->Text.c_str(), strlen(Edit1->Text.c_str()), &ByteCount, NULL ); } Edit1->Text=""; } [/code] 目前資料送出都正常, 但是我現在不知道該如何讀取資料? 好疑惑... |
MVP2000
一般會員 發表:13 回覆:16 積分:5 註冊:2007-04-03 發送簡訊給我 |
BOOL WINAPI ReadFile(
__in HANDLE hFile, __out LPVOID lpBuffer, __in DWORD nNumberOfBytesToRead, __out_opt LPDWORD lpNumberOfBytesRead, __inout_opt LPOVERLAPPED lpOverlapped ); BOOL WINAPI WriteFile( __in HANDLE hFile, __in LPCVOID lpBuffer, __in DWORD nNumberOfBytesToWrite, __out_opt LPDWORD lpNumberOfBytesWritten, __inout_opt LPOVERLAPPED lpOverlapped ); http://msdn2.microsoft.com/en-us/library/aa365467.aspx #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />//--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TComboBox *ComboBox1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); HANDLE h_RS232; ///放到著裡來 }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif
------
May I ask you something ?
編輯記錄
MVP2000 重新編輯於 2007-12-17 18:17:38, 註解 無‧
|
elva349
一般會員 發表:15 回覆:21 積分:17 註冊:2007-04-17 發送簡訊給我 |
ReadFile 這個function 我已經查過了,現在是不知道 該何時readfile
總不能把程式一直停在ReadFile 吧? ===================引 用 MVP2000 文 章=================== BOOL WINAPI ReadFile( __in HANDLE hFile, __out LPVOID lpBuffer, __in DWORD nNumberOfBytesToRead, __out_opt LPDWORD lpNumberOfBytesRead, __inout_opt LPOVERLAPPED lpOverlapped ); BOOL WINAPI WriteFile( __in HANDLE hFile, __in LPCVOID lpBuffer, __in DWORD nNumberOfBytesToWrite, __out_opt LPDWORD lpNumberOfBytesWritten, __inout_opt LPOVERLAPPED lpOverlapped ); http://msdn2.microsoft.com/en-us/library/aa365467.aspx #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include #include #include #include <forms.hpp></forms.hpp> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TButton *Button1; TComboBox *ComboBox1; void __fastcall Button1Click(TObject *Sender); private: // User declarations public: // User declarations __fastcall TForm1(TComponent* Owner); HANDLE h_RS232; ///放到著裡來 }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif |
MVP2000
一般會員 發表:13 回覆:16 積分:5 註冊:2007-04-03 發送簡訊給我 |
------
May I ask you something ? |
elva349
一般會員 發表:15 回覆:21 積分:17 註冊:2007-04-17 發送簡訊給我 |
|
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
|
elva349
一般會員 發表:15 回覆:21 積分:17 註冊:2007-04-17 發送簡訊給我 |
弄了一兩天 終於弄出來了~~把CODE 貼出來給大家看看~~
小弟還是新手~~想知道這樣是否有什麼地方沒有考慮到 .... 這是 H 檔部份 [code cpp] //--------------------------------------------------------------------------- #ifndef Unit1H #define Unit1H //--------------------------------------------------------------------------- #include #include #include #include <Forms.hpp><br />#include <Buttons.hpp><br />//--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TComboBox *ComboBox1; TEdit *Edit1; TBitBtn *BitBtn1; TBitBtn *BitBtn2; TBitBtn *BitBtn3; void __fastcall BitBtn1Click(TObject *Sender); void __fastcall BitBtn2Click(TObject *Sender); private: // User declarations public: // User declarations HANDLE h_RS232; DWORD threadID; HANDLE hTread ; bool thread; __fastcall TForm1(TComponent* Owner); }; void Recv (void *lpParam); //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif [/code] 這是主程式 [code cpp] //--------------------------------------------------------------------------- #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn1Click(TObject *Sender) { //開啟comport~ h_RS232=0; h_RS232=CreateFile(ComboBox1->Text.c_str(), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, //設定成 OVERLAPPED NULL); if (h_RS232) { ////設定 buffer 大小///////// SetupComm( h_RS232, // handle of communications device 1024, // size of input buffer 1024 // size of output buffer ); //////////////////////////// ////設定time out////////////// COMMTIMEOUTS pCOMMTIMEOUTS; GetCommTimeouts( h_RS232,&pCOMMTIMEOUTS); //抓取預設值 pCOMMTIMEOUTS.ReadIntervalTimeout=200; //GetOverlappedResult 沒抓到要求數目資料的等待時間 pCOMMTIMEOUTS.ReadTotalTimeoutMultiplier=500; pCOMMTIMEOUTS.ReadTotalTimeoutConstant = 5000; //GetOverlappedResult 抓不到資料會等待的時間 pCOMMTIMEOUTS.WriteTotalTimeoutConstant =5000; pCOMMTIMEOUTS.WriteTotalTimeoutMultiplier =500; SetCommTimeouts( h_RS232, &pCOMMTIMEOUTS); //把值設定進去 ////////////////////////////// ////清空一些com 的東西////// PurgeComm(h_RS232,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ; //////////////////////////// /////設定 連線的狀態///////// DCB dcb; GetCommState(h_RS232,&dcb); //抓取預設值 dcb.DCBlength=sizeof(dcb); dcb.BaudRate=CBR_9600; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=ONESTOPBIT; if (SetCommState(h_RS232,&dcb)) //把值設定進去 {thread=1; //打開接收的 thread 旗標~ hTread= CreateThread( 0 , 0 ,(LPTHREAD_START_ROUTINE)Recv ,(void *)this,0,&threadID );//打開thread ShowMessage("Connect Success!"); BitBtn1->Enabled=false; BitBtn2->Enabled=true; BitBtn3->Enabled=true; } } //開啟thread else { ShowMessage("Connect Fail"); BitBtn1->Enabled=true; BitBtn2->Enabled=false; BitBtn3->Enabled=false; } } //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn2Click(TObject *Sender) { if (h_RS232) { thread=0; //把接收旗標設為0 讓thread 結束 Sleep(200); CloseHandle(h_RS232); // 關掉COMPORT HANDLE CloseHandle(hTread); // 關掉 Thread HANDLE ShowMessage("Disconnect!"); BitBtn1->Enabled=true; BitBtn2->Enabled=false; BitBtn3->Enabled=false; } } //--------------------------------------------------------------------------- void Recv (void *lpParam) { TForm1 *pObject = (TForm1*) lpParam; DWORD Mask; Mask =0; const int buf=10; //buf 不能小於3 等於1的話 buf-1 會等於0 就收不到資料 等於2的話GetOverlappedResult 會因為收不到資料等待很久 char temp[buf]; //用個temp 來儲存 ZeroMemory(temp,buf); //清空temp DWORD ByteCount = 0; OVERLAPPED ov ; memset( &ov ,0, sizeof(ov)) ; ResetEvent(ov.hEvent); SetCommMask(pObject->h_RS232,EV_RXCHAR); //設定 事件觸發 在有資料送進來時 WaitCommEvent(pObject->h_RS232,&Mask,&ov) ; //好像是開啟另外一個thread 去等待有無"設定"事件觸發 如有觸發 會回傳到 我的Mask 裡面 AnsiString Test=""; while(pObject->thread) { ZeroMemory(temp,buf); //清空 temp while (Mask & EV_RXCHAR) //檢查有無資料送進來~ { bool a; while ((a = ReadFile(pObject->h_RS232,temp,buf-1,&ByteCount,&ov))) { GetOverlappedResult(pObject->h_RS232,&ov,&ByteCount,true); Test = Test temp; ZeroMemory(temp,buf); } if (GetLastError()==ERROR_IO_PENDING) { GetOverlappedResult(pObject->h_RS232,&ov,&ByteCount,true); Test = Test temp; ZeroMemory(temp,buf); } Mask=0; WaitCommEvent(pObject->h_RS232,&Mask,&ov) ; PurgeComm(pObject->h_RS232,PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR ) ; ResetEvent(ov.hEvent); Mask=0; WaitCommEvent(pObject->h_RS232,&Mask,&ov) ; pObject->Edit1->Text=Test; } } } [/code]
編輯記錄
elva349 重新編輯於 2007-12-21 14:00:07, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |