go_away

Author Topic: How to do Serial port communication using Dev Cpp IDE 4.9.9.2?  (Read 6359 times)

0 Members and 1 Guest are viewing this topic.

Offline bootstrapTopic starter

  • Full Member
  • ***
  • Posts: 61
  • Helpful? 0
How to do Serial port communication using Dev Cpp IDE 4.9.9.2?
« on: January 03, 2008, 12:00:23 AM »
Hello,I have started programming in DevCpp IDE.I want to do serial communication using DevCPP.Can any one suggest how to?I have some code which runs in TC but not in DevCPP..Please tell me how to do serial comm in DevCpp ?Please give me a sample code on which I can build upon..

Offline Fredrik Andersson

  • Robot Overlord
  • ****
  • Posts: 216
  • Helpful? 0
Re: How to do Serial port communication using Dev Cpp IDE 4.9.9.2?
« Reply #1 on: January 03, 2008, 04:02:06 PM »
Oooh, DevC++! That takes me back to when i used windows. I liked that programming environment really much.

Maybe you'll find something here: http://www.programmersheaven.com/zone3/cat411/
Current project: Pirrh - Portable Intelligent Round Rolling Hexapod

Offline bootstrapTopic starter

  • Full Member
  • ***
  • Posts: 61
  • Helpful? 0
Re: How to do Serial port communication using Dev Cpp IDE 4.9.9.2?
« Reply #2 on: January 14, 2008, 02:52:59 AM »
The following code is for sending characters out from the serial port (com1 ,9600,no parity ,one stop bit).
But I am not getting any output from the serial port.As of now I am sending the char 'a' out but I am not observing it.Can anyone help me rectify the 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 hComm;
   
    hComm = CreateFile(ComPortName,
        GENERIC_READ | GENERIC_WRITE,
        0, // exclusive access
        NULL, // no security
        OPEN_EXISTING,
        0, // no overlapped I/O
        NULL); // null template

    bPortReady = SetupComm(hComm, 1, 128); // set buffer sizes


    bPortReady = GetCommState(hComm, &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(hComm, &dcb);

    // Communication timeouts are optional

    bPortReady = GetCommTimeouts (hComm, &CommTimeouts);

    CommTimeouts.ReadIntervalTimeout = 5;
    CommTimeouts.ReadTotalTimeoutConstant = 5;
    CommTimeouts.ReadTotalTimeoutMultiplier = 1;
    CommTimeouts.WriteTotalTimeoutConstant = 5;
    CommTimeouts.WriteTotalTimeoutMultiplier = 1;

    bPortReady = SetCommTimeouts (hComm, &CommTimeouts);

    return hComm;
}

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

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

    return rxchar;
}

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

int main()
{
    HANDLE my=SerialInit("com1",9600);
    char letter;
    printf("ranjan\n");
       
    HANDLE *ptr;
    *ptr=my;
    for(;;)
    {
           
    SerialPutc(ptr,'a');     
    //letter=SerialGetc(ptr);
    cout<<letter<<endl;
    }
    getch();   
   
    return 0;
   
}

 


Get Your Ad Here

data_list