Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

interface with serial ports

Posted on 2004-03-23
6
Medium Priority
?
193 Views
Last Modified: 2010-04-01
I want to read data from my rs 232 port, and get information like
"Is there a device connected on the other end"
"is it receiving any data"
this is basically for a diagnosis- is there a broken cable, is there a disconnected cable, bad device

I suppose it has something to do with reading pins -like TD,RD, CD etc on the port,
but i am not sure about it.
Can someone give me a general idea in this area

Vikas
0
Comment
Question by:vikasgkutty
  • 2
  • 2
6 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10663207
This code may be helpful.
But You must be aware that it strongly depends on the device on the other end what You could test and how.

#include <windows.h>
#include <assert.h>

HANDLE hCom;
OVERLAPPED o;
BOOL fSuccess;
DWORD dwEvtMask;

hCom = CreateFile( "COM1",
    GENERIC_READ | GENERIC_WRITE,
    0,    // exclusive access
    NULL, // default security attributes
    OPEN_EXISTING,
    FILE_FLAG_OVERLAPPED,
    NULL
    );

if (hCom == INVALID_HANDLE_VALUE)
{
    // Handle the error.
    return;
}

// Set the event mask.

fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR | EV_BREAK);


if (!fSuccess)
{
    // Handle the error.
    return;
}

// Create an event object for use in WaitCommEvent.

o.hEvent = CreateEvent(
    NULL,   // default security attributes
    FALSE,  // auto reset event
    FALSE,  // not signaled
    NULL    // no name
    );

assert(o.hEvent);

if (WaitCommEvent(hCom, &dwEvtMask, &o))
{
    if (dwEvtMask & EV_DSR)
    {
         // To do.
    }

    if (dwEvtMask & EV_CTS)
    {
         // To do.
    }

    if (dwEvtMask & EV_BREAK)
    {
         // To do.
    }
}
0
 

Author Comment

by:vikasgkutty
ID: 10667624
Could you  briefly tell me what this code is doing
i dont get it

0
 
LVL 17

Expert Comment

by:mokule
ID: 10668572
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/communications_functions.asp


The CreateFile function creates or opens the following objects and returns a handle that can be used to access the object - COM1 port here

The SetCommMask function specifies a set of events to be monitored for a communications device. Here will be monitored CTS DSR lines and BREAK state

The WaitCommEvent function waits for an event to occur for a specified communications device

If u want transmit

if( GetCommState(hCom, &dcbPort) )
     {
               dcbPort.BaudRate = 9600;
               dcbPort.ByteSize = 8;
               dcbPort.Parity = NOPARITY;
               dcbPort.StopBits = ONESTOPBIT;
                    if(SetCommState(hCom, &dcbPort))
                    {
                                                                }
                 }

WriteFile(hCom, buf, count,&sended, NULL);
ReadFile(hCom, buf, count, &received, NULL);

GetCommTimeouts
SetCommTimeouts

0
 

Author Comment

by:vikasgkutty
ID: 10933815
Hi,...
i am very new to vc++
how should i go abt writing this in visual studio
could u tell me
file -> new win32 ?
    or where should i go...

i did this
file -> new workspace-> win32 application
         new file->c++ file-> pasted urs

Vikas
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 11135248
PAQed, with points refunded (100)

Computer101
E-E Admin
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

916 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