Hello,I am doing a project in which i need to send and receive data from a microcontroller.I was trying to do this in DEV CPP IDE 4.9.9.2 but couldn't succeed till now.The following is the code i tried compiling.

The code:

#include <bios.h>
#include <conio.h>

 #define COM1       0
 #define DATA_READY 0x100
 #define TRUE       1
 #define FALSE      0

 #define SETTINGS ( _COM_9600 | _COM_CHR8 | _COM_NOPARITY | _COM_STOP1)

 int main(void)
 {
    int in, out, status, DONE = FALSE;

    bioscom(0, SETTINGS, COM1);
    cprintf("... BIOSCOM [ESC] to exit ...\n");
    while (!DONE)
    {
       status = bioscom(3, 0, COM1);
       if (status & DATA_READY)
          if ((out = bioscom(2, 0, COM1) & 0x7F) != 0)
             putch(out);
          if (kbhit())
          {
             if ((in = getch()) == '\x1B')
                DONE = TRUE;
             bioscom(1, in, COM1);
          }
    }
    return 0;
 }

Previously i got an error saying that bios.h couldn't be found .Then I added bios.h from a TC's include directory.Now it got compiled but when I tried running it ,I got the following errors.

C:/Dev-Cpp/include/bios.h:21: error: variable or field `far' declared void

C:/Dev-Cpp/include/bios.h:21: warning: no semicolon at end of struct or union
C:/Dev-Cpp/include/bios.h:21: error: syntax error before '*' token

Can any one tell me how to solve this.Also can any one give me some sample code or resource to read on serial communication in C using DEV CPP IDE 4.9.9.2

How to send/receive on serial port is operating system dependent. The code you posted was written for MS-DOS and Dev-C++ only compiles for MS-Windows. You can not use any of the code you posted with that compiler. So you might as well just toss it away and use win32 api functions. Start by reading MSDN for CreateFile function to open the serial port, then scroll down the page until you see Communications Resources.

How to send/receive on serial port is operating system dependent. The code you posted was written for MS-DOS and Dev-C++ only compiles for MS-Windows. You can not use any of the code you posted with that compiler. So you might as well just toss it away and use win32 api functions. Start by reading MSDN for CreateFile function to open the serial port, then scroll down the page until you see Communications Resources.

I am doing it in windows Xp and I am unable to run that code.Plese tell me a simple program in DEV CPP Ide where I can send and recieve the data through the serial port.

XP comes with the program Hyperterminal which does this.

I am doing it in windows Xp and I am unable to run that code.

Yes, I told you so :)

Plese tell me a simple program in DEV CPP Ide where I can send and recieve the data through the serial port.

Simple? There is no such thing. Read through the information in the links I provided in my previous post. You will have to do quite a but of studying.

If you want to confine your program for DOS only (warning: highly unrecommended!) and shun learning (warning: again highly unrecommended!) what AD said, you may try to compile this code with some MS-DOs compiler.

I want to know why the above code i mentioned is getting compiled in TC but not DEV CPP IDE and how to make it run in DEV CPP IDE 4.9.9.2

AD has already answered this.

How to send/receive on serial port is operating system dependent. The code you posted was written for MS-DOS and Dev-C++ only compiles for MS-Windows. You can not use any of the code you posted with that compiler.

First of all, the mistake you have been doing is using Turbo C compiler and getting confused between code written for MS-DOS and Windows OS. STOP using your Turbo C compile and just focus on things which are more standard.

>I want to know why the above code i mentioned is getting compiled in TC but not DEV CPP IDE >and how to make it run in DEV CPP IDE 4.9.9.2
You can already see from the error message thrown by the compile on Dev-C++. You have been using some non standard library which was available under TC. But, Dev-C++ doest support those libraries what so ever.

And now explain us, why do you need this code. What is the main purpose of writing this code? What I understand from your post is that, you trying to use this program send and receive data between your microcontroller and your PC. If that was your main intention, there is already inbuilt terminal emulator "HyperTerminal" which does that for you. Where you can transfer your binary to your MController.

If that wasn't your intention, Can you explain you what are you trying to do?

And on the other hand, have a look at the links which I posted in my previous post, there is an article on how to program a serial port for Windows. Its a good Article which was taken from MSDN. If you follow that you will be able to achieve something on Dev-C++.

And start thinking STANDARDS.

ssharish

In case of VB, try searching for MSComm controls on google.

First of all, the mistake you have been doing is using Turbo C compiler and getting confused between code written for MS-DOS and Windows OS. STOP using your Turbo C compile and just focus on things which are more standard.

>I want to know why the above code i mentioned is getting compiled in TC but not DEV CPP IDE >and how to make it run in DEV CPP IDE 4.9.9.2
You can already see from the error message thrown by the compile on Dev-C++. You have been using some non standard library which was available under TC. But, Dev-C++ doest support those libraries what so ever.

And now explain us, why do you need this code. What is the main purpose of writing this code? What I understand from your post is that, you trying to use this program send and receive data between your microcontroller and your PC. If that was your main intention, there is already inbuilt terminal emulator "HyperTerminal" which does that for you. Where you can transfer your binary to your MController.

If that wasn't your intention, Can you explain you what are you trying to do?

And on the other hand, have a look at the links which I posted in my previous post, there is an article on how to program a serial port for Windows. Its a good Article which was taken from MSDN. If you follow that you will be able to achieve something on Dev-C++.

And start thinking STANDARDS.

ssharish

I dont want to use hypertermiaml..I want to have my own standard window where I can send and receive.I want to add more functionality to this and hyperterminal window will not serve that.I am using Dev C++ 4.9.9.2 .The above code was working only in Turbo C compiler.

I am using Dev C++ 4.9.9.2 .The above code was working only in Turbo C compiler.

It is obvious from the earlier posts. Try AD's suggestions..

The following code (.cpp for DevCpp, Windows) 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');     
    
    }
    getch();   
    
    return 0;
    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.