|
어딘가에서 COM 포트를 물고 안 놔주는 모양인데요.
어디서 그런지 먼저 찾으셔야 할 것 같네요.
그리고 COM 포트 제어는 간단하니까 CPort 쓰지 마시고 직접 열어서 사용하심이 어떨까 합니다...
제가 예전에 썼던 함수를 첨부합니다.
HANDLE ComPortInitialize(LPTSTR inPort) // inPort는 COM1 또는 COM2 식의 문자열
{
DCB dcb;
HANDLE hCom;
BOOL fSuccess;
hCom = CreateFile( inPort,
GENERIC_READ | GENERIC_WRITE,
0, // must be opened with exclusive-access
NULL, // no security attributes
OPEN_EXISTING,
0, // not overlapped I/O
NULL // hTemplate must be NULL for comm devices
);
if (hCom == INVALID_HANDLE_VALUE)
throw _T("CreateFile failed.");
fSuccess = GetCommState(hCom, &dcb);
if (!fSuccess)
throw _T("GetCommState failed.");
// Fill in DCB: 9,600 bps, 8 data bits, no parity, and 1 stop bit.
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = false;
dcb.fOutxCtsFlow = false;
dcb.fOutxDsrFlow = false;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fInX = false;
dcb.fOutX = false;
dcb.fNull = false;
fSuccess = SetCommState(hCom, &dcb);
SetupComm(hCom, RS232_BUFFER_SIZE, RS232_BUFFER_SIZE);
PurgeComm(hCom, PURGE_TXABORT | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_RXCLEAR);
COMMTIMEOUTS rs232timeout;
GetCommTimeouts(hCom,&rs232timeout);
rs232timeout.ReadIntervalTimeout = 2; // ReadFile에서 계속 멈춰있지 말고 2ms 마다 빠지도록 하기 위해
rs232timeout.ReadTotalTimeoutMultiplier = 1;
SetCommTimeouts(hCom,&rs232timeout);
if (!fSuccess)
throw _T("SetCommState failed.\n");
return hCom;
}
리턴값으로 핸들을 받아서
readable = ReadFile(hCom,readBuffer,1,&readBytes,NULL);
이런식으로 읽고,
WriteFile(hCom,writeBuffer,writeLength,&writeBytes,NULL);
이런식으로 쓰면 됩니다.
포트 사용이 끝나면 반드시
CloseHandle(hCom);
호출해줘서 포트를 닫아줘야지 다른 프로그램이 사용할 수 있답니다.
플머 님이 쓰신 글 :
: CPort 를 사용해 계측기와 통신중인데... 꼭 하루에 아침에 장비(컴퓨터와 계측기 전원)를 켤때마다...
:
: CPort->Open(), or CPort->Connected = true 동작중... 프로그램이 그대로 멈쳐있네요...
:
: ComPort 가 Open 이 안되서 그런것 같은데... 다시 프로그램 강제 종료하고 실행하면 (리부팅) 잘됩니다.
:
: 왜그런지...
|