Code:
void serialportsetup(){
hCom = CreateFile( "COM1",
GENERIC_READ | GENERIC_WRITE,
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
if (hCom == INVALID_HANDLE_VALUE){
MessageBox(hEdit, "The COM port cannot be openned !", "Error!", MB_OK | MB_ICONEXCLAMATION);
}
bSuccess = GetCommState(hCom, &dcb);
if (!bSuccess) {
MessageBox(hEdit, "Cannot read the COM port state !", "Error!", MB_OK | MB_ICONEXCLAMATION);
}
dcb.BaudRate = 57600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fRtsControl=RTS_CONTROL_DISABLE;
dcb.fOutX=FALSE;
bSuccess = SetCommState(hCom, &dcb);
if (!bSuccess){
MessageBox(hEdit, "The COM port cannot be configured !", "Error!", MB_OK | MB_ICONEXCLAMATION);
}
bSuccess = SetCommMask(hCom, EV_CTS );
if (!bSuccess){
MessageBox(hEdit, "The CTS pin state cannot be changed !", "Error!", MB_OK | MB_ICONEXCLAMATION);
}
o.hEvent = CreateEvent( NULL,TRUE,FALSE,NULL);
assert(o.hEvent);
}
Send(UINT command1, UINT sendlength){
char command=(char)command1;
if(black_bkgd) command=command | 0x20;
ResetEvent(o.hEvent);
dcb.fRtsControl=RTS_CONTROL_ENABLE;
SetCommState(hCom, &dcb);
MessageBox(hEdit, "Send request !", "Warning !", MB_OK | MB_ICONEXCLAMATION);
if (WaitCommEvent(hCom, &dwEvtMask, &o)) ;
{
if (dwEvtMask & EV_CTS){
MessageBox(hEdit, "CTS received !", "Warning !", MB_OK | MB_ICONEXCLAMATION);
dcb.fRtsControl=RTS_CONTROL_DISABLE;
SetCommState(hCom, &dcb);
bSuccess=WriteFile(hCom,&command,1,NULL,&b);
if (!bSuccess) {
MessageBox(hEdit, "Cannot write data to the COM port!", "Error!", MB_OK | MB_ICONEXCLAMATION); wrong check...not important,anyway
}
if(mode!=5) WriteFile(hCom,sendbuffer,sendlength,NULL,&b);
else{
i=0;
Sleep(500);
do{
for(j=0;j<16;j++,i++ ){
WriteFile(hCom,&sendbuffer[i],1,NULL,&b);
}
Sleep(40);
}while(i<4096);
}
Sleep(100);
MessageBox(hEdit, "Message sent !", "Warning !", MB_OK | MB_ICONEXCLAMATION);
}
}
}
If I run this code for the first time as Send command within my app window it works. But it doesn't work correctly if I run it from the second time. As described in the post above after putting the RTS to the ACTIVE state(0 logic ,positive voltage),