예전에 제가 만든 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;
: }