Solved

interface with serial ports

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

708 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