C++Builder Programming Forum
C++Builder  |  Delphi  |  FireMonkey  |  C/C++  |  Free Pascal  |  Firebird
볼랜드포럼 BorlandForum
 경고! 게시물 작성자의 사전 허락없는 메일주소 추출행위 절대 금지
C++빌더 포럼
Q & A
FAQ
팁&트릭
강좌/문서
자료실
컴포넌트/라이브러리
메신저 프로젝트
볼랜드포럼 홈
헤드라인 뉴스
IT 뉴스
공지사항
자유게시판
해피 브레이크
공동 프로젝트
구인/구직
회원 장터
건의사항
운영진 게시판
회원 메뉴
북마크
볼랜드포럼 광고 모집

C++빌더 Q&A
C++Builder Programming Q&A
[19610] Re:Re:Re:API 함수???? 232통신 푸러그램...에서.. WriteFile은???
조준회 [] 1309 읽음    2002-06-25 17:56

예전에 제가 만든 RS-232 클래스에 문제가 있을때 해결을 위해 참고했던
소스입니다.

먼저 이걸 분석해 보시는 것이 나을 듯 싶습니다.

http://www.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_res&no=142



슬픈 초보 님이 쓰신 글 :
: Tutorial에 있는 강의를 봤는데염~~
:
: 흑흑.. 언제 강의가 올라오나.. 목 빠지게 기둘리고 있답니다.
:
: 또.. 책에 있는데로..
: 컴포트 열려구.. CreateFile() 썼구여..
: 통신 Parameter같은거 Setting할려구... GetCommProperties() 등등.. 썻구여~
: 했는데.. 여기까지는 에러 없이(?) 잘 가는 거 같은데...
: 문자를 보내서.. Send하는 부분에서.. 먹통이 되버려여.. (WriteFile)...
:
: 기냥.. 쪼금.. 부끄럽지만... 소스 다~ 올릴께영~
:
: 참!! 글구.. 질문 하나더!
:
:    return 1L;   return 2L;  이러던데..
:
: 1L.. 2L이 뭔가여???
:
: //---------------------------------------------------------------------------
: __fastcall TForm1::TForm1(TComponent* Owner)
:         : TForm(Owner)
: {
:   Cbo_Comport->ItemIndex = 1;
: }
: //---------------------------------------------------------------------------
:
: void __fastcall TForm1::Btn_ConnectClick(TObject *Sender)
: {
:   char CommName[4];
:   strcpy(CommName, Cbo_Comport->Text.c_str());
:   CommName[4] = '\0';
:
:   cc.dwSize = sizeof(COMMCONFIG);
:   cc.dcb.BaudRate = CBR_9600;
:
:   CommConfigDialog(CommName, Handle, &cc);
:
:   if((hComm = CreateFile(CommName,GENERIC_READ|GENERIC_WRITE,
:     0,NULL, OPEN_EXISTING,0,NULL))==INVALID_HANDLE_VALUE)
:   {
:         ShowMessage("Error opening COM Port");
:         return;
:   }
:
:   cc.dcb.fOutxCtsFlow = true;
:   cc.dcb.fRtsControl = true;
:
:   if(!SetCommState(hComm, &cc.dcb)) {
:         CloseHandle(hComm);
:         ShowMessage("포트 설정 Error");
:         return;
:   }
:
:   cp.wPacketLength = sizeof(COMMPROP);
:   GetCommProperties(hComm, &cp);
:
:   Reading = TRUE;
:
:   if((hThread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ReadThread,
:          NULL, 0, &id)) == INVALID_HANDLE_VALUE)
:   {
:         ShowMessage(" Error Creating Read Thread");
:         CloseHandle(hComm);
:         return;
:   }
:   //Memo1->SetFocus();
:   Edit1->SetFocus();
: }
: //---------------------------------------------------------------------------
:
:
: void CommError(DWORD dwError)
: {
:   LONG lrc;
:   COMSTAT cs;
:
:   ClearCommError(hComm, (LPDWORD)&lrc, &cs);
:   CloseHandle(hComm);
: }
:
: void __fastcall TForm1::Btn_DisConnectClick(TObject *Sender)
: {
:    Reading = false;
:    ResumeThread(hThread);
:
:   
:    SetCommMask(hComm, 0L);
:    while(GetExitCodeThread(hThread, &id))
:    {
:      if(id == STILL_ACTIVE)
:        continue;
:      else
:        break;
:    }
:
:    CloseHandle(hThread);
:    CloseHandle(hComm);
: }
: //---------------------------------------------------------------------------
: DWORD ReadThread()
: {
:   char inbuff[BUFSIZ];
:   DWORD nBytesRead;
:
:   if(!(cp.dwProvCapabilities & PCF_INTTIMEOUTS))
:     return 1L;
:
:   memset(&to, 0, sizeof(to));
:   to.ReadIntervalTimeout = MAXDWORD;
:   SetCommTimeouts(hComm, &to);
:
:   while(Reading)
:   {
:     if(!ReadFile(hComm, inbuff, BUFSIZ, &nBytesRead, NULL))
:     {
:       CommError(GetLastError());
:     }
:     else
:     {
:       if(nBytesRead)
:         Form1->ProcessBytes(inbuff, nBytesRead);
:     }
:   }
:
:   PurgeComm(hComm, PURGE_RXCLEAR|PURGE_TXCLEAR);
:
:   return 0L;
: }
:
: void __fastcall TForm1::ProcessBytes(char *inbuff, int nBytesRead)
: {
:   int i;
:   char Ch, buff[100];
:
:   memset(buff, NULL, 100);
:   for(i=0; i<nBytesRead; i++)
:   {
:     buff[i]=inbuff[i];
:   }
:   Memo1->Lines->Add(buff);
: }
:
: //---------------------------------------------------------------------------
:
: //---------------------------------------------------------------------------
:
:
: //---------------------------------------------------------------------------
:
:
: void __fastcall TForm1::Msg_SendClick(TObject *Sender)
: {
:   LONG lrc;
:   int nBytes, i;
:   char Buff[100];
:
:   nBytes = Edit1->Text.Length();
:   strcpy(Buff, Edit1->Text.c_str());
:   Memo1->Lines->Add(Edit1->Text);
:   Edit1->SetFocus();
:
:   for(i = 0; i<nBytes; i++) {
:     char Ch = Buff[i];
:     if(!WriteFile(hComm, (LPBYTE)&Ch, 1, (LPDWORD)&lrc, NULL))
:     {
:        CommError(GetLastError());
:     }
:   }
:   return;
: }

+ -

관련 글 리스트
19604 API 함수???? 232통신 푸러그램...에서.. WriteFile은??? 슬픈초보 1120 2002/06/25
19605     Re:API 함수???? 232통신 푸러그램...에서.. WriteFile은??? 조준회 1338 2002/06/25
19609         Re:Re:API 함수???? 232통신 푸러그램...에서.. WriteFile은??? 슬픈 초보 1345 2002/06/25
19610             Re:Re:Re:API 함수???? 232통신 푸러그램...에서.. WriteFile은??? 조준회 1309 2002/06/25
Google
Copyright © 1999-2015, borlandforum.com. All right reserved.