|
첨부한 그림파일을 보시면...
메모1, 메모2, 버튼2개 써서...
메모1은 시리얼 포트로 들어오는 데이터를 무조건 표시하게 하고(9600bps)..
메모2는 내가 입력하고 싶은 한글데이터를 시리얼 포트로 출력하고자합니다.(Send버튼 눌렀을때)
cport콤포넌트를 이용하면 쉽게 할수 있다고 하던데..
저는 지금 cport콤포넌트를 이요하지 않고 해보려구 햇는데 무지 힙드네여..
빌더 전문가님 들의 조언부탁드립니다...
어떻게 하면 가능할지....
며칠동안 무지 무지 고민 했는데 해결이 안되네여...지발 부탁..~~
소스를 올려주시면 더 좋구요..(무리한 부탁인가??)
부탁드립니다..에구...
//--------------------------------------------------------------------------------------------------------
cport를 이용한 방법즘 갈켜 주세여..
아래쪽에 있는건 책에 있는거 참고해서 해 볼라 했는데 실패 했음당.ㅋ..ㅠ.ㅠ
//--------------------------------------------------------------------------------------------------------
제가 빌더 4.0책 보구 어리버리하게 해 봤는데..^^;;
물론 동작안 안되더라구여..맞는지 틀린지도 모르겠구..
컴파일 할때 에러는 없이 컴파일은 되구...
글구 참고고 메모2의 경우는 한글이 한자씩 밖에 안써져서..난감..
어떻게 고쳐야 할지....
위그 그림에 맞게 할려면 어딜 고쳐야 할까여..send버튼 누르면 출력되게 하고 싶은데..
아래 프로그램은 send버튼 클릭시...어떻게 프로그램을 해야 할지도 몰르겠어여..이룬.
아래 프로그램은 그렇지 않은듯...프로님 들의 도움 부탁...^^;;
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
#define BUFSIZ 1024
int ilnput;
char atdtbuff[50];
HANDLE hComm, hThread;
BOOL Reading;
COMMTIMEOUTS to;
DWORD id;
COMMPROP cp;
COMMCONFIG cc;
void CommError(DWORD dwError);
DWORD ReadThread ();
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
HANDLE hComm;
char CommName[4];
strcpy(CommName, "Com1");
CommName[4] = '\0';
if((hComm = CreateFile (CommName, GENERIC_READlGENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE)
{
ShowMessage(" Error opening COM port");
return;
}
cc.dcb.BaudRate = CBR_9600;
CommConfigDialog(CommName, Handle, &cc);
cc.dcb.fOutxCtsFlow = true;
cc.dcb.fRtsControl = true;
Reading = TRUE;
if ((hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ReadThread,
NULL, 0, &id))==INVALID_HANDLE_VALUE)
{
ShowMessage("Error READ thread");
CloseHandle (hComm);
return;
}
}
int __fastcall TForm1::NetWriteString(char *pszString, int cbString)
{
int i;
LONG lrc;
for (i = 0; i < cbString; i++){
char Ch = pszString[i];
if(!WriteFile(hComm, (LPBYTE)&Ch, 1, (LPDWORD)&lrc, NULL)){
CommError(GetLastError ());
}
}
return cbString;
}
void CommError (DWORD dwError)
{
LONG lrc;
COMSTAT cs;
ClearCommError(hComm, (LPDWORD)&lrc, &cs);
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_RXCLEARlPURGE_TXCLEAR);
return 0L;
}
void __fastcall TForm1::ProcessBytes(char *inbuff, int nBytesRead)
{
int i;
char Ch;
for (i=0; i<nBytesRead; i++){
Ch = inbuff[i];
switch (Ch){
case 0x1b:
break;
default:
if(Memo1->Lines->Count > 4)Memo1->Lines->Delete(0);
Memo1->Text = Memo1->Text + Ch;
Memo1->SelStart = strlen(Memo1->Text.c_str());
RadioButton2->Checked = true;
break;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Memo2KeyPress(TObject *Sender, char &Key)
{
static char Buff[3];
if((unsigned char)Key >= 0x80){
ilnput = !ilnput;
if (!ilnput){
Buff[1] = Key;
Buff[2] = '\0';
NetWriteString(Buff, 2);
}else
Buff[0] = Key;
}else{
ilnput = 0;
Buff[0] = Key; Buff[1] = '\0';
NetWriteString(Buff, 1);
}
Key = 0;
return;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ClearClick(TObject *Sender)
{
RadioButton3->Checked = true;
Memo1-> Clear();
Memo2-> Clear();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SENDClick(TObject *Sender)
{
RadioButton1->Checked = true;
}
//---------------------------------------------------------------------------
|