Solved

interface with serial ports

Posted on 2004-03-23
6
188 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
[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
  • 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

Independent Software Vendors: 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

624 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