Re: 關於C++的串流處理

看板Programming作者 (新竹苦命研究生)時間18年前 (2006/08/10 21:49), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串5/5 (看更多)
※ 引述《kuoelec.bbs@bbs.ccns.ncku.edu.tw (自己的選擇)》之銘言: : 我想用C++寫一個程式接收由RS232讀入的資料 : 但小弟我不知如何下手 : 我找過google但資料少得可憐 : 請問各位大大有沒有推薦的書或網站?? : 一開始接觸這方面要從哪裡下手呢?? : 謝謝 orz 我用BCB寫的 //=============== 開啟一個 com port ========================== void __fastcall TForm1::openClick(TObject *Sender) { char* comno; DCB dcb; String temp; temp="Com"+IntToStr(rdCOM->ItemIndex+1); comno=temp.c_str(); hComm=CreateFile(comno,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0); if(hComm==INVALID_HANDLE_VALUE) { MessageBox(0,"Comm error","通訊錯誤",MB_OK); return; } Shape2->Brush->Color= clGreen; GetCommState(hComm,&dcb); dcb.BaudRate=CBR_38400; dcb.ByteSize=8; dcb.Parity=NOPARITY; dcb.StopBits=ONESTOPBIT; if(!SetCommState(hComm,&dcb)) { MessageBox(0,"Comm error","通訊錯誤",MB_OK); CloseHandle(hComm); return; } if(!SetupComm(hComm,2000,2000)) { MessageBox(0,"Comm error","通訊錯誤",MB_OK); CloseHandle(hComm); return; } EscapeCommFunction(hComm,CLRDTR); } //----------------------------------------------------------- //======================== 接收外部資料 副程式 DCB dcb; int temp1; String temp; char inbuff[19200]; DWORD nBytestRead,dwEvent,dwError; COMSTAT cs; ClearCommError(hComm,&dwError,&cs); GetCommState(hComm,&dcb); if(cs.cbInQue>sizeof(inbuff)) { PurgeComm(hComm,PURGE_RXCLEAR); return; } ReadFile(hComm,inbuff,cs.cbInQue,&nBytestRead,NULL); inbuff[cs.cbInQue]='\0'; } // ======================== 傳送資料副程式 void __fastcall TForm1::Timer2Timer(TObject *Sender) { String temp; int in; unsigned long lrc,BS; if(hComm==0) return; temp= Msend->Text; // 我讀取我面板上的使用者輸入字串 BS=temp.Length(); char *senddata; /*for(int i=0;i<BS;i++) senddata[i]=temp[i]; */ senddata=temp.c_str(); cbiq->Text=(BS); WriteFile(hComm,senddata,BS,&lrc,NULL); //delete[] senddata; } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.114.28.84
文章代碼(AID): #14spbCd7 (Programming)
文章代碼(AID): #14spbCd7 (Programming)