Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,402 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,108 people online right now. Registration is fast and FREE... Join Now!




HANDLE and DWORD in Serial programming

 
Reply to this topicStart new topic

HANDLE and DWORD in Serial programming

totally_lost
18 Sep, 2006 - 11:43 PM
Post #1

New D.I.C Head
*

Joined: 13 Sep, 2006
Posts: 9



Thanked: 1 times
My Contributions
Hi all,

I am using C++ (using the Dev C++ compiler) for serial port programming. Altough i m well versed in normal C/C++ programming,serial port communication is new to me. Thus before I start on my project what I usually do is source for some code on the net and get an idea on how to do it. I ve been trying to compile the below code but its always giving compilation errors like

"HANDLE does not name a type"
"BOOL does not name a type"
"DCB does not name a type"
"DWORD does not name a type"
"COMMTIMEOUTS does not name a type"

How should I get rid of these errors???

Is there an additional library that I should add??? My Dev C++ compiler has only the basic libraries that comes with the free online version.(Hehe... those features of Dev C++ were more than enough for me for 2/3 years)

I would greatly appreciate any help.

Heres the code

Serial.h
CODE

#define FC_DTRDSR       0x01
#define FC_RTSCTS       0x02
#define FC_XONXOFF      0x04

// ascii definitions

#define ASCII_BEL       0x07
#define ASCII_BS        0x08
#define ASCII_LF        0x0A
#define ASCII_CR        0x0D
#define ASCII_XON       0x11
#define ASCII_XOFF      0x13


HANDLE SerialInit(char*, int);

char SerialGetc(HANDLE*);

void SerialPutc(HANDLE*, char);

char* SerialGets(HANDLE*);

void SerialPuts(HANDLE*, char*);

void sleep(int);

--------------------------------------------------------------------------

Serial.cpp
CODE

#include <stdio.h>
#include <time.h>

//#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers

#include <afxwin.h>    // serial.cpp : Defines the entry point for the console application.

//#include "stdafx.h"
#include <string.h>

#include "serial.h"

// Flow control flags

#define FC_DTRDSR       0x01
#define FC_RTSCTS       0x02
#define FC_XONXOFF      0x04

// ascii definitions

#define ASCII_BEL       0x07
#define ASCII_BS        0x08
#define ASCII_LF        0x0A
#define ASCII_CR        0x0D
#define ASCII_XON       0x11
#define ASCII_XOFF      0x13

    // variables used with the com port
    BOOL            bPortReady;
    DCB                dcb;
    COMMTIMEOUTS    CommTimeouts;
    BOOL            bWriteRC;
    BOOL            bReadRC;
    DWORD            iBytesWritten;
    DWORD            iBytesRead;

HANDLE SerialInit(char *ComPortName, int BaudRate)
{
    HANDLE hCom;
    
    hCom = CreateFile(ComPortName,
        GENERIC_READ | GENERIC_WRITE,
        0, // exclusive access
        NULL, // no security
        OPEN_EXISTING,
        0, // no overlapped I/O
        NULL); // null template

    bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes


    bPortReady = GetCommState(hCom, &dcb);
    dcb.BaudRate = BaudRate;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
//    dcb.Parity = EVENPARITY;
    dcb.StopBits = ONESTOPBIT;
    dcb.fAbortOnError = TRUE;

    // set XON/XOFF
    dcb.fOutX = FALSE;                    // XON/XOFF off for transmit
    dcb.fInX    = FALSE;                    // XON/XOFF off for receive
    // set RTSCTS
    dcb.fOutxCtsFlow = TRUE;                    // turn on CTS flow control
    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;    //
    // set DSRDTR
    dcb.fOutxDsrFlow = FALSE;                    // turn on DSR flow control
    dcb.fDtrControl = DTR_CONTROL_ENABLE;    //
//    dcb.fDtrControl = DTR_CONTROL_DISABLE;    //
//    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;    //

    bPortReady = SetCommState(hCom, &dcb);

    // Communication timeouts are optional

    bPortReady = GetCommTimeouts (hCom, &CommTimeouts);

    CommTimeouts.ReadIntervalTimeout = 5000;
    CommTimeouts.ReadTotalTimeoutConstant = 5000;
    CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
    CommTimeouts.WriteTotalTimeoutConstant = 5000;
    CommTimeouts.WriteTotalTimeoutMultiplier = 1000;

    bPortReady = SetCommTimeouts (hCom, &CommTimeouts);

    return hCom;
}

char SerialGetc(HANDLE *hCom)
{
    char rxchar;
    BOOL    bReadRC;
    static    DWORD    iBytesRead;

    bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL);

    return rxchar;
}

void SerialPutc(HANDLE *hCom, char txchar)
{
    BOOL    bWriteRC;
    static    DWORD    iBytesWritten;
    
    bWriteRC = WriteFile(*hCom, &txchar, 1, &iBytesWritten,NULL);
}

char* SerialGets(HANDLE *hCom)
{
    static char rxstring[256];
    char c;
    int pos = 0;

    while(pos <= 255) {
        c = SerialGetc(hCom);
        if(c == '\r') continue;        // discard carriage return
        rxstring[pos++] = c;
        if(c == '\n') break;
    }
    rxstring[pos] = 0;

    return    rxstring;
}

void SerialPuts(HANDLE *hCom, char *txstring)
{
    BOOL    bWriteRC;
    static    DWORD    iBytesWritten;

    bWriteRC = WriteFile(*hCom, txstring, strlen(txstring), &iBytesWritten,NULL);

}

void sleep( int _wait)
{
    clock_t goal;
    goal = clock() + _wait;
    while( goal > clock() );
}

User is offlineProfile CardPM
+Quote Post

totally_lost
RE: HANDLE And DWORD In Serial Programming
19 Sep, 2006 - 01:17 AM
Post #2

New D.I.C Head
*

Joined: 13 Sep, 2006
Posts: 9



Thanked: 1 times
My Contributions
hey...........

So I finally managed to compile the code in Dev C++ after doing some minor adjustments and adding windows.h.(phew.....)

The program is compiling. How do I check weather it is running properly. I m simply initializing the serial port and writing the letter 'a' to it.

Acctually ...can I use a digital oscilloscope and check the waveform pattern in it???????????

11 01000001 0 = stop bits / data bits / start bit

would I be able to see a waveform pattern???????

For example......I am using a Dell Laptop. I connect the serial port of the Dell laptop to a digital oscilloscope.Then observe the digital waveform pattern........Do you think it can be done. If so how long will the waveform pattern last.

I would greatly appreciate any help u guys can give....

the updated code is below (compiles without errors on Dev C++ ...thank god!!!!!!!!)

Serial.h
CODE

// Flow control flags

#define FC_DTRDSR       0x01
#define FC_RTSCTS       0x02
#define FC_XONXOFF      0x04

// ascii definitions
#include <stdio.h>
#include <time.h>

//#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers

//#include <afxwin.h>    // serial.cpp : Defines the entry point for the console application.

//#include "stdafx.h"
#include <string.h>

#define ASCII_BEL       0x07
#define ASCII_BS        0x08
#define ASCII_LF        0x0A
#define ASCII_CR        0x0D
#define ASCII_XON       0x11
#define ASCII_XOFF      0x13


HANDLE SerialInit(char*, int);

char SerialGetc(HANDLE*);

void SerialPutc(HANDLE*, char);

--------------------------------------------------------------------------


Serial.cpp
CODE

#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>

//#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers
//#include <bios.h>
//#include <afxwin.h>    // serial.cpp : Defines the entry point for the console application.

//#include "stdafx.h"
#include <string.h>
#include "serial.h"

// Flow control flags

#define FC_DTRDSR       0x01
#define FC_RTSCTS       0x02
#define FC_XONXOFF      0x04

// ascii definitions

#define ASCII_BEL       0x07
#define ASCII_BS        0x08
#define ASCII_LF        0x0A
#define ASCII_CR        0x0D
#define ASCII_XON       0x11
#define ASCII_XOFF      0x13
using namespace std;
    // variables used with the com port
    BOOL            bPortReady;
    DCB                dcb;
    COMMTIMEOUTS    CommTimeouts;
    BOOL            bWriteRC;
    BOOL            bReadRC;
    DWORD            iBytesWritten;
    DWORD            iBytesRead;

HANDLE SerialInit(char *ComPortName, int BaudRate)
{
    HANDLE hCom;
    
    hCom = CreateFile(ComPortName,
        GENERIC_READ | GENERIC_WRITE,
        0, // exclusive access
        NULL, // no security
        OPEN_EXISTING,
        0, // no overlapped I/O
        NULL); // null template

    bPortReady = SetupComm(hCom, 2, 128); // set buffer sizes


    bPortReady = GetCommState(hCom, &dcb);
    dcb.BaudRate = BaudRate;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
//    dcb.Parity = EVENPARITY;
    dcb.StopBits = ONESTOPBIT;
    dcb.fAbortOnError = TRUE;

    // set XON/XOFF
    dcb.fOutX = FALSE;                    // XON/XOFF off for transmit
    dcb.fInX    = FALSE;                    // XON/XOFF off for receive
    // set RTSCTS
    dcb.fOutxCtsFlow = TRUE;                    // turn on CTS flow control
    dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;    //
    // set DSRDTR
    dcb.fOutxDsrFlow = FALSE;                    // turn on DSR flow control
    dcb.fDtrControl = DTR_CONTROL_ENABLE;    //
//    dcb.fDtrControl = DTR_CONTROL_DISABLE;    //
//    dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;    //

    bPortReady = SetCommState(hCom, &dcb);

    // Communication timeouts are optional

    bPortReady = GetCommTimeouts (hCom, &CommTimeouts);

    CommTimeouts.ReadIntervalTimeout = 5000;
    CommTimeouts.ReadTotalTimeoutConstant = 5000;
    CommTimeouts.ReadTotalTimeoutMultiplier = 1000;
    CommTimeouts.WriteTotalTimeoutConstant = 5000;
    CommTimeouts.WriteTotalTimeoutMultiplier = 1000;

    bPortReady = SetCommTimeouts (hCom, &CommTimeouts);

    return hCom;
}

char SerialGetc(HANDLE *hCom)
{
    char rxchar;
    BOOL    bReadRC;
    static    DWORD    iBytesRead;

    bReadRC = ReadFile(*hCom, &rxchar, 1, &iBytesRead, NULL);

    return rxchar;
}

void SerialPutc(HANDLE *hCom, char txchar)
{
    BOOL    bWriteRC;
    static    DWORD    iBytesWritten;
    
    bWriteRC = WriteFile(*hCom, &txchar, 1, &iBytesWritten,NULL);
    
    return;
}

int main()
{
    HANDLE my=SerialInit("com1",1200);
    char letter;
    
    HANDLE *ptr;
    *ptr=my;
    SerialPutc(ptr,'a');    
    //letter=SerialGetc(ptr);
    getch();  
    
    return 0;
    
}


This post has been edited by jayman9: 19 Sep, 2006 - 06:56 AM
User is offlineProfile CardPM
+Quote Post

bootstrap
RE: HANDLE And DWORD In Serial Programming
3 Jan, 2008 - 06:14 AM
Post #3

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 2

Dude great job that you got the code compiled in DevCpp.Even I went mad compiling codes which would work in normal c/c++ using TC but not in DevCpp.

Now coming to your solution.The best and the simple way to test whether your code is working or not is short the pins of the Rx and Tx of the serial port.You can do this as follows:

1.Buy a serial port female connecter (around Rs 15)

2.Look out for the pin diagram of the female pin serial port and short the Rx and Tx pins.

3.After shorting ,connect the femlae pin to the Male serial port and then switch on the laptop.

4.Write/Add code to send data via the serial (Tx pin) and then receive the data using the Rx pin. (I think this could be done by removing the comments(//) for the line
//letter=SerialGetc(ptr);
in the main block.

(Basically by shorting the Rx and Tx pin you are echoing back the letters/numbers etc.. that you have sent out to the serial port)

This post has been edited by infibit: 3 Jan, 2008 - 06:22 AM
User is offlineProfile CardPM
+Quote Post

bootstrap
RE: HANDLE And DWORD In Serial Programming
14 Jan, 2008 - 12:00 AM
Post #4

New D.I.C Head
*

Joined: 3 Jan, 2008
Posts: 2

Your code is compiling fine but the char 'a' is not being output at the serial port.I have the electronics to chek whether it is working or not by usinga microcontroller.(By using hyperterminal I am able to speak to the microcontroller)but your code seems to have a logical error becuase of which the charater is not being sent to the serial port.

Please tell me the right/working code

This post has been edited by infibit: 14 Jan, 2008 - 12:01 AM
User is offlineProfile CardPM
+Quote Post

born2c0de
RE: HANDLE And DWORD In Serial Programming
14 Jan, 2008 - 11:33 PM
Post #5

printf("I'm a %XR",195936478);
Group Icon

Joined: 26 Nov, 2004
Posts: 3,935



Thanked: 34 times
Dream Kudos: 2800
Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions
QUOTE
Is there an additional library that I should add??

Yes, you need windows.h
User is offlineProfile CardPM
+Quote Post

LogMeIn
RE: HANDLE And DWORD In Serial Programming
20 Jan, 2008 - 08:07 PM
Post #6

New D.I.C Head
*

Joined: 15 Jan, 2007
Posts: 3


My Contributions
Quick thought.... I am just starting to play with serial i/o. I connected the pins of a cable end to a board and put in LED's to visually see whats going on. Set the baud rate to say 10 or less and you can observe the 1's and 0's. Its still hard enough to see how many consecutive 1's or 0's goes by but its possible to confirm the byte you are hoping to see. Good luck.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 03:10AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month