Click to See Complete Forum and Search --> : MAXSTREAM modems.


drewdaman
April 4th, 2005, 12:40 PM
Hello,

I am using two maxsteram XTEND modems. i am havnig some trouble doing some simple io. :(

i send a string "hello" from one to the other in a loop. if i send less than 500 tiems, it works. .. if i try to send more than 500 times, the receving end receives the string as "eohell".. i am using non overlapped io.

sending code (this works since we have "while (i<500)".. if i make it "while (true)" or "while (i<600)" it does not work)... i have marked the line with "//HERE********************" in the code.. i have also marked two other lines that i would like you to see (with waitcommevent).


int main(){

HANDLE hport;
hport=CreateFile("COM1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,0,NULL);
DWORD dwCommEvent;
if (hport==INVALID_HANDLE_VALUE ){
std::cout<<"PORT UNAVAILABLE"<<std::endl;
}
else{
initcomport(hport);
SetCommMask(hport, EV_CTS|EV_DSR);
char * test="hello";
int i=0;
while (i<500){ //HERE********************
DWORD dwNumBytesWritten;
int bsent=0;
while (bsent<buffersize){
WriteFile(hport, test+bsent, buffersize-bsent, &dwNumBytesWritten, NULL);
bsent=bsent+dwNumBytesWritten;
}

//WaitCommEvent(hport, &dwCommEvent, NULL);//HERE********************

//dwCommEvent=0;//HERE********************
i++;
}
}//close else

return 0;
}


receiving code:

int main(){

HANDLE hport;
hport=CreateFile("COM1:", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,0,NULL);
if (hport==INVALID_HANDLE_VALUE ){
std::cout<<"PORT UNAVAILABLE"<<std::endl;
}

initcomport(hport); //function to initialize the port.



char * test=new char [buffersize+1];

while (true){
DWORD dwBytesTransferred=0;
int brec=0;
while (brec<buffersize){
ReadFile(hport,test+brec,buffersize-brec,&dwBytesTransferred,NULL);
brec=brec+dwBytesTransferred;
//test[dwBytesTransferred]='\0';
}
test[buffersize]='\0';
std::cout<<"Buffer is: "<<test<<std::endl;

}




return 0;
}


It seems that i have some sort of synchrozniation issue here... i tried using "WaitCommEvent()".. but it waits indefinitely when i use that... (this can be seen in the commented liens in the sending part)..

what i'm really looking for is to know when the modem is ready to send out more data and when the modem is ready to receive. i hope someone can h elp me out!

thanks a lot!
drew.

mxjschwartz
April 26th, 2005, 03:36 PM
Drew,

There are a couple of things that I would look at doing. In your code you have:

while (i<500){ //HERE********************
DWORD dwNumBytesWritten;
int bsent=0;
while (bsent<buffersize){
WriteFile(hport, test+bsent, buffersize-bsent, &dwNumBytesWritten, NULL);
bsent=bsent+dwNumBytesWritten;
}

For starters I would take the declarations outside the while loop. If you take them outside the while loop then they will only get declared once instead of 500-600 times. You can initialize them to zero in the while loop if you would like, but I would try taking the declarations out just to make sure it isn't somehow screwing something up.

Also, the way you open the comport Writefile should wait untill all the data is transmitted. There is a timeout which can be setup which will cause the function to return. I wonder if the time out is set too short. If the timeout is sufficent then all you would normally need to do to write to the port is as follows:

WriteFile(hport, test, buffersize, &dwNumBytesWritten, NULL);

The radio will observe flow control so to know when the radio is ready for data all you have to do is monitor the CTS line. If the radio has CTS asserted then you should be able to send it data.

I hope this helps.