Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

Serial Comm Problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Chong - 28 Feb 2005 16:11 GMT
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
Larry Brasfield - 28 Feb 2005 16:44 GMT
>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
Signature

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

Michael Chong - 01 Mar 2005 01:18 GMT
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.

> >I wrote a program that communicate with SerialComm. In every 300
> > milliseconds, my program continuously send & receive data via the serial
[quoted text clipped - 41 lines]
> email: donotspam_larry_brasfield@hotmail.com
> Above views may belong only to me.
Larry Brasfield - 01 Mar 2005 03:14 GMT
[Top-posting undone for clarity.]
>> >I wrote a program that communicate with SerialComm. In every 300
>> > milliseconds, my program continuously send & receive data via the serial
[quoted text clipped - 4 lines]
>> 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)
[quoted text clipped - 9 lines]
>
>     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];
[quoted text clipped - 4 lines]
>     {
>          hCom = NULL;

Ditto.

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

Ditto.

>     }
>
[quoted text clipped - 4 lines]
>
>     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;
[quoted text clipped - 6 lines]
>     return hCom;
> }

So, what does your read code look like?

Signature

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

Michael Chong - 01 Mar 2005 12:08 GMT
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

> [Top-posting undone for clarity.]
> >> >I wrote a program that communicate with SerialComm. In every 300
[quoted text clipped - 54 lines]
> >     //create port handle
> >     hCom =

CreateFile(szCom,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_O
> > VERLAPPED,NULL);
>
[quoted text clipped - 69 lines]
> email: donotspam_larry_brasfield@hotmail.com
> Above views may belong only to me.
Larry Brasfield - 01 Mar 2005 15:57 GMT
> 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.
[quoted text clipped - 4 lines]
>     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();
> }
[quoted text clipped - 12 lines]
>     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
[quoted text clipped - 8 lines]
> 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.

Signature

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

Michael Chong - 02 Mar 2005 01:01 GMT
Do you have any website or sample that can refer me to serial port examples
in vc++.net

Thanks
Michael

> > 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.
[quoted text clipped - 76 lines]
> email: donotspam_larry_brasfield@hotmail.com
> Above views may belong only to me.
Larry Brasfield - 02 Mar 2005 03:51 GMT
> 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.
Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.