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 7 2607
"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.
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.
[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.
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.
"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.
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.
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: ^CeFoS^ |
last post by:
Hello to everybody,
I've done an application that draws in a frame the trajectory of a
robot. The robot position is readed through the serial port, and
several commands are wrote through the...
|
by: Mike C. |
last post by:
I am trying to get Java v1.4.2 to use my PCI add-on serial port.
So far, everything I try gives me
javax.comm.NoSuchPortException
at...
|
by: bray_stenovations |
last post by:
Hello,
I am experiencing some strange problems while using a usb to serial
adapter vs. a real comm port. The problems started with getting a blue
screen at random times. Changing usb to serial...
|
by: Peter Krikelis |
last post by:
Hi All,
I am having a problem setting up input mode for serial
communications.
(Sorry about the long code post).
The following code is what I use to set up my comm port.
|
by: coolsilver.net |
last post by:
I am not sure if I am in the right group.
My problem is this:
My VB.NET application prints to the Epson printer using Epson OPOS
drivers. I have downloaded the Epson ADK for .NET from the...
|
by: ken |
last post by:
Hello everyone,
I'm new to visual VB and I am trying to setup communications to the
outside world. I found the example listed below in the help section of
Microsoft Visual Basic 2005 Express...
|
by: ... |
last post by:
Hi
I need to send a chr(255) to a serial port. When I send it, through
comm.write (chr(255)) it sends a chr(63) ... in Hex, I write chr(&FF) and it
actually sends chr(&3F) ... why does this...
|
by: alnsaisrinivas |
last post by:
Hi
I've a problem with I/O blocking on my serial port at this particular
scenario...
1.I've developed a driver to communicate with some device using Serial
Comm (overlapped I/O).
2.I'm able to...
|
by: glaskan |
last post by:
This code is meant to take an input from the serial port and then save the input from the serial port as the name and as the data to a text file but all i am getting is an empty text file or a text...
|
by: Kemmylinns12 |
last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: antdb |
last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine
In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Johno34 |
last post by:
I have this click event on my form. It speaks to a Datasheet Subform
Private Sub Command260_Click()
Dim r As DAO.Recordset
Set r = Form_frmABCD.Form.RecordsetClone
r.MoveFirst
Do
If...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: DizelArs |
last post by:
Hi all)
Faced with a problem, element.click() event doesn't work in Safari browser.
Tried various tricks like emulating touch event through a function:
let clickEvent = new Event('click', {...
| |