Back Forum Reply New

Overlapped and Non-overlapped Serial Programming

Hello,

I'm trying to code a program that accepts data on a virtual com port and then forwards it through the physical com port to an external device. The external device then sends data back to the physical port which my program directs to the virtual port. The virtual port is quot;connectedquot; to a third party program.

Now, my question is: Is it necessary to use overlapped operation if at no point operations actually overlap? You see, at no stage does my program need to perform operations simultaneously. It simply is a quot;go-betweenquot; between two external parties. It receives and sends in one direction and then receives and sends back the other direction.

I have coded this program using non-overlapped operation and WaitCommEvent(). It hangs the second time it calls WaitCommEvent to receive data from the external device. I would assume that because we are dealing with two separate com ports then the WaitCommEvent calls would not interfere with each other. What am i not understanding?

Thanking you in advance.

budge.Code:

#include lt;windows.hgt;
#include lt;iostreamgt;
#include lt;stringgt;
using namespace std;
using std::string;HANDLE hSerial3 = CreateFile(quot;COM3quot;,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);

HANDLE hSerial1 = CreateFile(quot;COM1quot;,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);

void sendbackOK( ) {

char writeBuff[50 + 1] = {0};
DWORD dwBytesWrite = 0;

// Write response to outgoing buffer
sprintf(writeBuff,quot;OKquot;);

if(!WriteFile(hSerial3, writeBuff, 50, amp;dwBytesWrite, NULL)){
// error occured. Report to User.
cout lt;lt; quot;ERROR OCCURED IN WRITING TO SERIAL PORTquot; lt;lt; quot;\nquot;;
}
else
cout lt;lt; quot;SENT RESPONSE: quot; lt;lt; writeBuff lt;lt; quot;\nquot;;

}

void sendbackCONNECT( ) {

char writeBuff[50 + 1] = {0};
DWORD dwBytesWrite = 0;

// Write response to outgoing buffer
sprintf(writeBuff,quot;CONNECT 9600quot;);

if(!WriteFile(hSerial3, writeBuff, 50, amp;dwBytesWrite, NULL)){
// error occured. Report to User.
cout lt;lt; quot;ERROR OCCURED IN WRITING TO SERIAL PORTquot; lt;lt; quot;\nquot;;
}
else
cout lt;lt; quot;SENT RESPONSE: quot; lt;lt; writeBuff lt;lt; quot;\nquot;;

}void forwardtoCOM1(char data[]) {

char writeExtBuff[64+1] = {0};
DWORD dwBytesWrite = 0;

// Write response to outgoing buffer
sprintf(writeExtBuff,data);

if(!WriteFile(hSerial1, writeExtBuff, 64, amp;dwBytesWrite, NULL)){
// error occured. Report to User.
cout lt;lt; quot;ERROR OCCURED IN WRITING TO SERIAL PORTquot; lt;lt; quot;\nquot;;
}
else
cout lt;lt; quot;SENDING DATA: quot; lt;lt; writeExtBuff lt;lt; quot;\nquot;;
}

void forwardtoCOM2(char data[]) {

char writeExtBuff[64+1] = {0};
DWORD dwBytesWrite = 0;

// Write response to outgoing buffer
sprintf(writeExtBuff,data);

if(!WriteFile(hSerial3, writeExtBuff, 64, amp;dwBytesWrite, NULL)){
// error occured. Report to User.
cout lt;lt; quot;ERROR OCCURED IN WRITING TO SERIAL PORTquot; lt;lt; quot;\nquot;;
}
else
cout lt;lt; quot;SENDING DATA: quot; lt;lt; writeExtBuff lt;lt; quot;\nquot;;
}

void getResponse()  {

DWORD dwCommEvent;
DWORD dwBytesRead;
char readBuff;
char receivedstring[32];
string s;
int i = 0;

// Check that serial port can be opened:
if(hSerial1==INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){
// serial port does not exist. Inform User.
}
// some other error has occured. Inform User.
}

// Define Parameters for serial port:
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

if (!GetCommState(hSerial1, amp;dcbSerialParams)) {
// error getting state
}

dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;

if(!SetCommState(hSerial1, amp;dcbSerialParams)){
// error setting serial port state
}

// Set a timeout:
COMMTIMEOUTS timeouts;
timeouts.ReadIntervalTimeout=1;  // Specify time-out between charactor for receiving
timeouts.ReadTotalTimeoutConstant=1; // Specify value is added to the product of the ReadTotalTimeoutMultiplier member
timeouts.ReadTotalTimeoutMultiplier=1; // Specify value that is multiplied by the requested number of bytes to be read.
timeouts.WriteTotalTimeoutConstant=100;
timeouts.WriteTotalTimeoutMultiplier=10;

if(!SetCommTimeouts(hSerial1, amp;timeouts)){
cout lt;lt; quot;ERROR SETTTING TIMEOUTSquot;  lt;lt; endl;
// error occures setting timeouts. Inform User.
}

if (!SetCommMask(hSerial1, EV_RXCHAR)) {  cout lt;lt; quot;ERROR: Error setting EV_RXCHAR mask!.quot; lt;lt; quot;\nquot;;  // Error setting communications event mask
}
  if (dwCommEvent.WaitCommEvent(hSerial1, amp;dwCommEvent, NULL)) {       do {       if (ReadFile(hSerial1, amp;readBuff, 1, amp;dwBytesRead, NULL)) {// A byte has been read; process it.
receivedstring[i] = readBuff;
i++;       }       else {          // An error occurred in the ReadFile call.          cout lt;lt; quot;ERROR IN READFILE CALLquot; lt;lt; endl;                 }       break;               } while (dwBytesRead);  // Convert array to String  forwardtoCOM2(receivedstring);  s = receivedstring;    // Print what was received to standard out  cout lt;lt; (quot;RECEIVED STRING AND SENT: quot;) lt;lt; s lt;lt; endl;
  for (int j = 0; jlt;32; j++) {
receivedstring[j] = NULL;
}
  i=0;

}

else {   // Error in WaitCommEvent

}}int main() {

cout lt;lt; quot;Starting Program...quot; lt;lt; endl;

// Check that serial port can be opened:
if(hSerial3==INVALID_HANDLE_VALUE){
if(GetLastError()==ERROR_FILE_NOT_FOUND){
// serial port does not exist. Inform User.
}
// some other error has occured. Inform User.
}

// Define Parameters for serial port:
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);

if (!GetCommState(hSerial3, amp;dcbSerialParams)) {
// error getting state
}

dcbSerialParams.BaudRate=CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;

if(!SetCommState(hSerial3, amp;dcbSerialParams)){
// error setting serial port state
}

// Set a timeout:
COMMTIMEOUTS timeouts={0};
timeouts.ReadIntervalTimeout=20;
timeouts.ReadTotalTimeoutConstant=100;
timeouts.ReadTotalTimeoutMultiplier=10;
timeouts.WriteTotalTimeoutConstant=50;
timeouts.WriteTotalTimeoutMultiplier=10;

if(!SetCommTimeouts(hSerial3, amp;timeouts)){
// error occures setting timeouts. Inform User.
}////////////////////////////////////////////////////////////////////
//READ RECEIVED COMMANDS FROM MV90 AND DEAL WITH THEM APPROPRIATELY
////////////////////////////////////////////////////////////////////

DWORD dwCommEvent;
DWORD dwBytesRead;
char readBuff;
char receivedstring[64];

string s;int i = 0;

if (!SetCommMask(hSerial3, EV_RXCHAR))  cout lt;lt; quot;ERROR: Error setting EV_RXCHAR mask.quot; lt;lt; quot;\nquot;;  // Error setting communications event mask

while (true) {  if (WaitCommEvent(hSerial3, amp;dwCommEvent, NULL)) {        do {       if (ReadFile(hSerial3, amp;readBuff, 1, amp;dwBytesRead, NULL)) {// A byte has been read; process it.
receivedstring[i] = readBuff;
i++;       }       else          // An error occurred in the ReadFile call.          break;               } while (dwBytesRead);  // Convert array to String  s = receivedstring;    // Print what was received to standard out  cout lt;lt; (quot;RECEIVED: quot;) lt;lt; s lt;lt; quot;\nquot;;;   

if (s.substr(0,4) == quot;ATE0quot;)  {
sendbackOK();

}

else if (s.substr(0,4) == quot;ATH0quot;)  {
sendbackOK();
}

else if (s.substr(0,5) == quot;ATamp;D2quot;)  {
sendbackOK();
}

else if (s.substr(0,20) == quot;ATS7=60DTZIGBEENODE1quot;) {
sendbackCONNECT();
}

else {
//SINCE CONNECTION MADE: FORWARD DATA TO COM1
forwardtoCOM1(receivedstring);
getResponse();}

for (int k = 0; klt;64; k++) {
receivedstring[k] = NULL;
}
  i=0;  
}

else   // Error in WaitCommEvent  break;  
}
}
¥
Back Forum Reply New