• C

getting data from serial com port

FILE *PORT1;
DCB           dcb ;

dcb.BaudRate     = 9600;
dcb.ByteSize     = 8;
dcb.Parity       = 0;
dcb.StopBits       = 1;

PORT1 = fopen("COM4","r+");
GetCommState(PORT1, &dcb ) ;

if(PORT1)
    {
       while(ch = getc(PORT1))
               {
         if (ch == STX)
             //data from serial com port
               }
    }

Hello,
Some data starting with STX is coming through a serial com port and
I am trying to get it.
The Code is above.

It is very interesting that I got data when I trace this code (Running the code one by one)
Hovever if I run the .exe and it starts doing it in a loop,  I can't get any data.

Please help...

Thanks a lot.
e116470Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

PaulCaswellCommented:
You're probably not giving the system time to service the serial port. You need something in your while loop that gives the system idle time. What O/S and what compiler are you using?

Paul

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
billtouchCommented:
My first thought here would be to use a prewritten serial port tool like SimpleTerm (http://www.ptronix.com/). That will read your input and display it in char, hex, mixed, or bin. I do a lot of serial communications (I just got comm working to drive a Sony Camera Pan Tilt Head last night) and I find thiis tool to be invaluable. It saves the time in trying to do a custom routine.

After you see the data, then you can do the code. The thing to note is that for most serial communications, the read part is done asynchonously in its own thread. This allows you to process and not have to wait on a read.

Bill
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

furqanchandioCommented:
hi

try inserting a delay statement or getche() in


       while(ch = getc(PORT1))
               {
        if (ch == STX)
            //data from serial com port
              // add a delay here//

             }
e116470Author Commented:
I have inserted a delay. It works but I think that it is not a good solution.
The problem is; How much delay???
AnthonyFeeCommented:
How much delay can be very complicated. This involves calculations using the clock speed of your PC and also how often the serial receive flag is
checked.

Normally, as you probably already know, delays should be avoided as it is bad programming. I would advise you stick with it in this case.

The only other way that you could possibly make this code work without delays is setting up an interrupt as shown in the code on the link below.

http://www.picant.com/c2c/examples/rs232/SerialCom.c.html

If you find this code too complicated, I would advise sticking with the delay using trial and error to find an appropriate value.

Fee
jhshuklaCommented:
could the trouble be here?
dcb.StopBits      = 1;

try
dcb.StopBits      = ONESTOPBIT; // you will see '#define ONESTOPBIT 0' if you hover over it
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.