Solved

interface with serial ports

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

680 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