[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Serial Communications

Posted on 1997-09-07
2
Medium Priority
?
681 Views
Last Modified: 2006-11-17
I am having a bit of trouble using serial communications with C.  I am using a Dell XM590 90Mhz with NT4 server and VC++ compiler.  Although I am using the C++ compiler,  I am writing in generic C and not using any MFC (that I'm aware of).  I am trying to communicate with an RS232 compliant device (not a modem) on COM2.  The puropse of this device is to simply sit and wait until it gets a radio signal from another source, then pass information to the computer based on that radio signal.  I have verified that the device is working properly by hooking it to another computer and a generic testing program specifically made for this device.

I started out by opening the device with the CreateFile function.  I then checked the status of the device by sending a command by ReadFile and WriteFile.  So far, everything is working fine.  The problem occurs while trying to wait for the radio signal.  I set the comm mask to EV_RXCHAR, then use the WaitCommEvent function to wait for a character to arrive indicating that the radio signal has activated the device.  This is failing miserably.  Even though the comm mask is set, the WaitCommFunction fires on every comm event, seemingly even nonexistent ones.  The code for this function looks something like this:

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char ReadFileData;
    BOOL quit = FALSE;
    OVERLAPPED ov;
    HANDLE h = CreateFile(paramaters);  /* opens the comm port.
    WriteFileEx(paramaters); /*sets the device in command mode.
    ReadFileEx(paramaters); /*reads the response verifying command mode
    WriteFileEx(paramaters);  /*set the device to data mode
    ReadFileEx(paramaters); /*get the data mode response and clear the input buffer.
    SetCommMask(h, EV_RXCHAR); /*set the comm mask to wait for an incoming character.
    while(!quit)
    {
        WaitCommEvent(parameters);
        ReadFileEx(parameters);  
        If(ReadFileBuffer == 0x0A) /*The code for incoming communication
        {
            ReadFileEx(parameters); /*Read the rest of the buffer and parse the data
        }
    }
    return 0;
}


I know this is rather vauge as source code goes, but I don't have the original code with me, and hopefully this gievs an idea of what I am trying to accomplish.
0
Comment
Question by:dirtdart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
bertil earned 200 total points
ID: 1254238
Hi there.
I have used the serial comm stuff in NT4 and they run nice
on W95 too,The functions you need is standard Win32 API
so don't worry about MFC.I don't use the SetCommMask to
generate Events I poll it instead,if you need the Event style
use the polling method and put it in a separate thread and
have it to send normal messages to the main thread.
I open the port with CreateFile like you do be aware that you
must use OPEN_EXISTING when openning Com ports.
Then you need to setup a DCB with the correct baudrate
DSR flow and other things, feed that DCB to SetCommState.
To transmit use WriteFile and optionally FlushFileBuffers to
make sure that all chars are sent before you continue in your
program.Reading is a bit more tricky, to see if there are anything
in the buffer crate a COMSTAT and feed that to ClearCommError
then check if cbInQue in COMSTAT is greather than 0, if so
do a ReadFile and use the cbInQue as param 3  the param
that holds the count of chars you want to read.Thats it.

--Bertil

0
 
LVL 5

Author Comment

by:dirtdart
ID: 1254239
Thanks for the info.  This should make things run much smoother.  However, One other minor issue comes to mind. Is there any good way to make a timer in C?  I wouldn't want to constantly be polling the comm port to the expense of everything else on the machine.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question