By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

Serial Comm Problem

P: n/a
I wrote a program that communicate with SerialComm. In every 300
milliseconds, my program continuously send & receive data via the serial
port once the program starts.

My program is once in a while, the serial port seems corrupted because when
my data is sent, it doesn't go through the serial port, so as same to
receive process. In order to fix this, I have to close the port and reopen
the port again.

Why?!! Is there any way to fix this problem or I have to check for
corruption port, then I reopen the serial port again?

Thanks!

Michael
Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:uQ**************@TK2MSFTNGP14.phx.gbl...
I wrote a program that communicate with SerialComm. In every 300
milliseconds, my program continuously send & receive data via the serial
port once the program starts.
I am puzzled by the idea that your program can
receive data every 300 mS. To me, that would
be determined by the sender, at the other end of
the serial link. So I wonder what you really mean.
My program is once in a while, the serial port seems corrupted because when
my data is sent, it doesn't go through the serial port, so as same to
receive process. In order to fix this, I have to close the port and reopen
the port again.
I translate your report thusly: My code does not
act as I would like after a time, and since it acts as
I would like for a short time, I can get longer periods
of likable behavior by restarting the failing operation.
Rather than diagnose this as a problem with my code,
I would prefer to call it a corruption of something
outside of my code.
Why?!! Is there any way to fix this problem or I have to check for
corruption port, then I reopen the serial port again?
As someone who has used the Comm API in a few
different ways, some of them with high throughput
requirements, and seen millions of messages complete
without error going thru the serial port driven by such
code, I can assure you that there is probably a way
to make your code work and there is no such thing
as "corruption port" of the kind you suspect.

However, to have any chance of helping you find out
how to fix your code, more details on what you are
now doing would be necessary.
Thanks! You're welcome.
Michael

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Nov 17 '05 #2

P: n/a
I am not very good in C++ problem and this is my codes below. I do not know
whether I got handshaking involve in my code. Could you guys let me know?
HANDLE OpenComm(char *lpszPort, int nBaud, char *nParity, int nData, int
nStop)
{
HANDLE hCom;
LPDCB lpDcb;

char szCom[10];
memset(szCom, 0, sizeof(szCom));
strcpy(szCom, "\\.\\COM");
strcat(szCom, lpszPort);
strcat(szCom, ":");

lpDcb = new(DCB);

//create port handle
hCom =
CreateFile(szCom,GENERIC_READ|GENERIC_WRITE,0,NULL ,OPEN_EXISTING,FILE_FLAG_O
VERLAPPED,NULL);

//failed coz invalid handle provided
if (hCom == INVALID_HANDLE_VALUE)
{
hCom = NULL;
}

//fail to get port state
if (!GetCommState(hCom,lpDcb))
{
hCom = NULL;
}

//set setting [COM1: baud=9600 parity=N data=8 stop=1]
char strTemp[50];
memset(strTemp, 0, sizeof(strTemp));
sprintf(strTemp, "baud=%d parity=%s data=%d stop=%d", nBaud, nParity,
nData, nStop);

if (!BuildCommDCB(strTemp,lpDcb))
{
hCom = NULL;
}

if (!SetCommState(hCom,lpDcb))
{
hCom = NULL;
}

// set communication timeouts
// get default values
BOOL bPort;
COMMTIMEOUTS CommTimeouts;

bPort = GetCommTimeouts(hCom, &CommTimeouts);
// set new values
CommTimeouts.ReadIntervalTimeout = 15;
CommTimeouts.ReadTotalTimeoutConstant = 250;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 250;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
bPort = SetCommTimeouts(hCom, &CommTimeouts);

return hCom;
}
Michael.


"Larry Brasfield" <donotspam_> wrote in message
news:eY**************@TK2MSFTNGP15.phx.gbl...
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:uQ**************@TK2MSFTNGP14.phx.gbl...
I wrote a program that communicate with SerialComm. In every 300
milliseconds, my program continuously send & receive data via the serial
port once the program starts.


I am puzzled by the idea that your program can
receive data every 300 mS. To me, that would
be determined by the sender, at the other end of
the serial link. So I wonder what you really mean.
My program is once in a while, the serial port seems corrupted because when my data is sent, it doesn't go through the serial port, so as same to
receive process. In order to fix this, I have to close the port and reopen the port again.


I translate your report thusly: My code does not
act as I would like after a time, and since it acts as
I would like for a short time, I can get longer periods
of likable behavior by restarting the failing operation.
Rather than diagnose this as a problem with my code,
I would prefer to call it a corruption of something
outside of my code.
Why?!! Is there any way to fix this problem or I have to check for
corruption port, then I reopen the serial port again?


As someone who has used the Comm API in a few
different ways, some of them with high throughput
requirements, and seen millions of messages complete
without error going thru the serial port driven by such
code, I can assure you that there is probably a way
to make your code work and there is no such thing
as "corruption port" of the kind you suspect.

However, to have any chance of helping you find out
how to fix your code, more details on what you are
now doing would be necessary.
Thanks!

You're welcome.
Michael

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.

Nov 17 '05 #3

P: n/a
[Top-posting undone for clarity.]
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
"Larry Brasfield" <donotspam_> wrote in message
news:eY**************@TK2MSFTNGP15.phx.gbl...
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:uQ**************@TK2MSFTNGP14.phx.gbl...
>I wrote a program that communicate with SerialComm. In every 300
> milliseconds, my program continuously send & receive data via the serial
> port once the program starts.
I am puzzled by the idea that your program can
receive data every 300 mS. To me, that would
be determined by the sender, at the other end of
the serial link. So I wonder what you really mean.
It would still be useful to know what you are intending
to accomplish on the receive side, and what happens
every 300 mS.
> My program is once in a while, the serial port seems corrupted
> because when my data is sent, it doesn't go through the serial port,
> so as same to receive process. In order to fix this, I have to close
> the port and reopen the port again.

A more detailed set of observations would be useful.
What do you actually see? What did you expect?
However, to have any chance of helping you find out
how to fix your code, more details on what you are
now doing would be necessary.


That comment still applies. We see how you initialize
the comm port, but nothing reveals how you attempt
to send or receive data.
I am not very good in C++ problem and this is my codes below. I do not know
whether I got handshaking involve in my code. Could you guys let me know?
The BuildCommDCB() docs should tell you what the
default setting is. You've done nothing to override it.

A few minor code comments are inserted below.
HANDLE OpenComm(char *lpszPort, int nBaud, char *nParity, int nData, int
nStop)
{
HANDLE hCom;
LPDCB lpDcb;

char szCom[10];
memset(szCom, 0, sizeof(szCom));
strcpy(szCom, "\\.\\COM");
strcat(szCom, lpszPort);
strcat(szCom, ":");

lpDcb = new(DCB);
You may as well just define an auto DCB variable as
dynamically allocate such an object. I note that you
leak the one created above.
//create port handle
hCom =
CreateFile(szCom,GENERIC_READ|GENERIC_WRITE,0,NULL ,OPEN_EXISTING,FILE_FLAG_O
VERLAPPED,NULL);
Overlapped I/O is a great way to go when using the
comm API, but it is tricky to use. This is why it is
especially important to see what else you do with
the port, other than initialize it.
//failed coz invalid handle provided
if (hCom == INVALID_HANDLE_VALUE)
{
hCom = NULL;
}
If the above fails, the other comm API calls should
not be made.
//fail to get port state
if (!GetCommState(hCom,lpDcb))
{
hCom = NULL;
}
If the above fails, it would be a good idea to
close the HANDLE, not just set it to 0.
//set setting [COM1: baud=9600 parity=N data=8 stop=1]
char strTemp[50];
memset(strTemp, 0, sizeof(strTemp));
sprintf(strTemp, "baud=%d parity=%s data=%d stop=%d", nBaud, nParity, nData, nStop);

if (!BuildCommDCB(strTemp,lpDcb))
{
hCom = NULL;
Ditto.
}

if (!SetCommState(hCom,lpDcb))
{
hCom = NULL;

Ditto.
}

// set communication timeouts
// get default values
BOOL bPort;
COMMTIMEOUTS CommTimeouts;

bPort = GetCommTimeouts(hCom, &CommTimeouts);
I do not see why you want to get the timeouts when
you are setting them all anyway.
// set new values
CommTimeouts.ReadIntervalTimeout = 15;
CommTimeouts.ReadTotalTimeoutConstant = 250;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 250;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
bPort = SetCommTimeouts(hCom, &CommTimeouts);

return hCom;
}


So, what does your read code look like?

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Nov 17 '05 #4

P: n/a
Thanks Larry for helping, I do appreciated it. Below are my send and receive
comm port function. Basically this is a dll project that calls from vb.net.

int SendComm(HANDLE hCom, LPBYTE Buffer, unsigned int ByteCount)
{
DWORD dwWritten = 0;
OVERLAPPED OL={0};
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);

WriteFile(hCom, Buffer, ByteCount, &dwWritten, &OL);
CloseHandle(OL.hEvent);

PurgeComm(hCom, PURGE_TXABORT);

return GetLastError();
}
int ReceiveComm(HANDLE hCom, LPBYTE Buffer, unsigned int ByteCount)
{
DWORD dwWritten = 0;
OVERLAPPED OL={0};
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);

ReadFile(hCom, Buffer, ByteCount, &dwWritten, &OL);
CloseHandle(OL.hEvent);

PurgeComm(hCom, PURGE_RXABORT);

return GetLastError();
}
Below are the sending and receiving process that vb.net program will call to
this dll above. This is a communication between PC and a hardware that made
by my engineer.
PC Send: 0x01
PC Recv: 0x01
PC Send: {0xBA|0x00|BCC|0x00}
PC Recv: {0x00|0x03|0x34|LSB|HSB|BCC|0x00}

For example, I will keep on calling the above process every few seconds. But
after sometimes, I can't send command through the comm port (I don't know
why?). So I close the port and reopen it, it will return to normal again.
Why?!!

Thanks
Michael



"Larry Brasfield" <do***********************@hotmail.com> wrote in message
news:O2**************@TK2MSFTNGP09.phx.gbl...
[Top-posting undone for clarity.]
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
"Larry Brasfield" <donotspam_> wrote in message
news:eY**************@TK2MSFTNGP15.phx.gbl...
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:uQ**************@TK2MSFTNGP14.phx.gbl...
>I wrote a program that communicate with SerialComm. In every 300
> milliseconds, my program continuously send & receive data via the serial > port once the program starts.

I am puzzled by the idea that your program can
receive data every 300 mS. To me, that would
be determined by the sender, at the other end of
the serial link. So I wonder what you really mean.
It would still be useful to know what you are intending
to accomplish on the receive side, and what happens
every 300 mS.
My program is once in a while, the serial port seems corrupted
> because when my data is sent, it doesn't go through the serial port,
> so as same to receive process. In order to fix this, I have to close
> the port and reopen the port again.
A more detailed set of observations would be useful.
What do you actually see? What did you expect?
However, to have any chance of helping you find out
how to fix your code, more details on what you are
now doing would be necessary.
That comment still applies. We see how you initialize
the comm port, but nothing reveals how you attempt
to send or receive data.
I am not very good in C++ problem and this is my codes below. I do not
know whether I got handshaking involve in my code. Could you guys let me know?
The BuildCommDCB() docs should tell you what the
default setting is. You've done nothing to override it.

A few minor code comments are inserted below.
HANDLE OpenComm(char *lpszPort, int nBaud, char *nParity, int nData, int
nStop)
{
HANDLE hCom;
LPDCB lpDcb;

char szCom[10];
memset(szCom, 0, sizeof(szCom));
strcpy(szCom, "\\.\\COM");
strcat(szCom, lpszPort);
strcat(szCom, ":");

lpDcb = new(DCB);


You may as well just define an auto DCB variable as
dynamically allocate such an object. I note that you
leak the one created above.
//create port handle
hCom =

CreateFile(szCom,GENERIC_READ|GENERIC_WRITE,0,NULL ,OPEN_EXISTING,FILE_FLAG_O VERLAPPED,NULL);


Overlapped I/O is a great way to go when using the
comm API, but it is tricky to use. This is why it is
especially important to see what else you do with
the port, other than initialize it.
//failed coz invalid handle provided
if (hCom == INVALID_HANDLE_VALUE)
{
hCom = NULL;
}


If the above fails, the other comm API calls should
not be made.
//fail to get port state
if (!GetCommState(hCom,lpDcb))
{
hCom = NULL;
}


If the above fails, it would be a good idea to
close the HANDLE, not just set it to 0.
//set setting [COM1: baud=9600 parity=N data=8 stop=1]
char strTemp[50];
memset(strTemp, 0, sizeof(strTemp));
sprintf(strTemp, "baud=%d parity=%s data=%d stop=%d", nBaud, nParity, nData, nStop);
if (!BuildCommDCB(strTemp,lpDcb))
{
hCom = NULL;


Ditto.
}

if (!SetCommState(hCom,lpDcb))
{
hCom = NULL;

Ditto.
}

// set communication timeouts
// get default values
BOOL bPort;
COMMTIMEOUTS CommTimeouts;

bPort = GetCommTimeouts(hCom, &CommTimeouts);


I do not see why you want to get the timeouts when
you are setting them all anyway.
// set new values
CommTimeouts.ReadIntervalTimeout = 15;
CommTimeouts.ReadTotalTimeoutConstant = 250;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 250;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
bPort = SetCommTimeouts(hCom, &CommTimeouts);

return hCom;
}


So, what does your read code look like?

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.

Nov 17 '05 #5

P: n/a
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks Larry for helping, I do appreciated it. Below are my send and receive
comm port function. Basically this is a dll project that calls from vb.net.

int SendComm(HANDLE hCom, LPBYTE Buffer, unsigned int ByteCount)
{
DWORD dwWritten = 0;
OVERLAPPED OL={0};
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
What do you think this event is good for? I urge you
to look at the members of OVERLAPPED and the
docs describing overlapped I/O to see why an event
has a role in overlapped I/O.
WriteFile(hCom, Buffer, ByteCount, &dwWritten, &OL);
What do you suppose the API designers imagine the lifetime
of that OVERLAPPED object to be? Do you have a reason
to believe it will be as short as the lifetime of the one you
actually pass in here?
CloseHandle(OL.hEvent);

PurgeComm(hCom, PURGE_TXABORT);
Why is this call here?
return GetLastError();
}
int ReceiveComm(HANDLE hCom, LPBYTE Buffer, unsigned int ByteCount)
{
DWORD dwWritten = 0;
OVERLAPPED OL={0};
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);

ReadFile(hCom, Buffer, ByteCount, &dwWritten, &OL);
CloseHandle(OL.hEvent);

PurgeComm(hCom, PURGE_RXABORT);

return GetLastError();
}
All the same comments apply to the above routine.
Below are the sending and receiving process that vb.net program will call to
this dll above. This is a communication between PC and a hardware that made
by my engineer.
PC Send: 0x01
PC Recv: 0x01
PC Send: {0xBA|0x00|BCC|0x00}
PC Recv: {0x00|0x03|0x34|LSB|HSB|BCC|0x00}

For example, I will keep on calling the above process every few seconds. But
after sometimes, I can't send command through the comm port (I don't know
why?). So I close the port and reopen it, it will return to normal again.
Why?!!


When you open the port this way:
CreateFile(szCom,GENERIC_READ|GENERIC_WRITE,0,NULL ,
OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
then you are preparing to do your I/O using what is known as
"Overlapped I/O". This means that read and write operations do
not block. If they cannot complete immediately, the work is done
asynchronously relative to the thread that initiates the read or write,
and a notification of completion is signaled via the event in the that
OVERLAPPED object passed in upon read or write initiation.
This notification can happen awhile after the initiation call returns.

You need to rethink your decision to use overlapped I/O. Either
you do not need it, which is sort of suggested by how you have
coded your send and receive, or you need to use it the way it
was designed to be used. You may want to look at the MTTTY
sample to see some code that uses overlapped I/O.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Nov 17 '05 #6

P: n/a
Do you have any website or sample that can refer me to serial port examples
in vc++.net

Thanks
Michael


"Larry Brasfield" <do***********************@hotmail.com> wrote in message
news:uk**************@TK2MSFTNGP15.phx.gbl...
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Thanks Larry for helping, I do appreciated it. Below are my send and receive comm port function. Basically this is a dll project that calls from vb.net.
int SendComm(HANDLE hCom, LPBYTE Buffer, unsigned int ByteCount)
{
DWORD dwWritten = 0;
OVERLAPPED OL={0};
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);


What do you think this event is good for? I urge you
to look at the members of OVERLAPPED and the
docs describing overlapped I/O to see why an event
has a role in overlapped I/O.
WriteFile(hCom, Buffer, ByteCount, &dwWritten, &OL);


What do you suppose the API designers imagine the lifetime
of that OVERLAPPED object to be? Do you have a reason
to believe it will be as short as the lifetime of the one you
actually pass in here?
CloseHandle(OL.hEvent);

PurgeComm(hCom, PURGE_TXABORT);


Why is this call here?
return GetLastError();
}
int ReceiveComm(HANDLE hCom, LPBYTE Buffer, unsigned int ByteCount)
{
DWORD dwWritten = 0;
OVERLAPPED OL={0};
OL.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);

ReadFile(hCom, Buffer, ByteCount, &dwWritten, &OL);
CloseHandle(OL.hEvent);

PurgeComm(hCom, PURGE_RXABORT);

return GetLastError();
}


All the same comments apply to the above routine.
Below are the sending and receiving process that vb.net program will call to this dll above. This is a communication between PC and a hardware that made by my engineer.
PC Send: 0x01
PC Recv: 0x01
PC Send: {0xBA|0x00|BCC|0x00}
PC Recv: {0x00|0x03|0x34|LSB|HSB|BCC|0x00}

For example, I will keep on calling the above process every few seconds. But after sometimes, I can't send command through the comm port (I don't know why?). So I close the port and reopen it, it will return to normal again. Why?!!


When you open the port this way:
CreateFile(szCom,GENERIC_READ|GENERIC_WRITE,0,NULL ,
OPEN_EXISTING,FILE_FLAG_OVERLAPPED,NULL);
then you are preparing to do your I/O using what is known as
"Overlapped I/O". This means that read and write operations do
not block. If they cannot complete immediately, the work is done
asynchronously relative to the thread that initiates the read or write,
and a notification of completion is signaled via the event in the that
OVERLAPPED object passed in upon read or write initiation.
This notification can happen awhile after the initiation call returns.

You need to rethink your decision to use overlapped I/O. Either
you do not need it, which is sort of suggested by how you have
coded your send and receive, or you need to use it the way it
was designed to be used. You may want to look at the MTTTY
sample to see some code that uses overlapped I/O.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.

Nov 17 '05 #7

P: n/a
"Michael Chong" <mi*****@cyob.com.my> wrote in message
news:eq*************@tk2msftngp13.phx.gbl...
Do you have any website or sample that can refer me to serial
port examples in vc++.net


Start here:
http://www.microsoft.com/
Search for "MTTTY" using the site search utility.
Follow the link(s) in the result set. The sample
near the top of the article "Serial Communications
in Win32" especially deserves your attention.

[Brasfield previously wrote:]
You may want to look at the MTTTY sample to
see some code that uses overlapped I/O.

--
--Larry Brasfield
email: do***********************@hotmail.com
Above views may belong only to me.
Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.