Solved

interface with serial ports

Posted on 2004-03-23
6
182 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now