hi everyone.........
i need a c++ program that can help me in chatting through serial port from one pc to another........
only i want to sen and recieve charactors..........
thank you........

Dani AI

Generated

Short practical guidance for doing a simple character chat over a serial link from Visual C++ on Windows (clarifies gaps in the posted snippets and ties together points from , , , and ).

On Windows you should avoid DOS/BIOS serial calls and use the Win32 serial APIs: open the COM port like a file, configure the port parameters (DCB), set sensible timeouts (COMMTIMEOUTS), then perform reads/writes either with a dedicated reader thread or with overlapped (asynchronous) I/O so the UI doesn't block. A minimal working sequence is: open -> GetCommState/modify DCB -> SetCommState -> SetCommTimeouts -> ReadFile/WriteFile loop -> CloseHandle.

A compact example (VC6/Win32) showing the core sequence — open, set baud/parity/stop, simple write and read —:

/* minimal Win32 serial sequence (error checks omitted for brevity) */
HANDLE h = CreateFile("\\\\.\\COM1",
                      GENERIC_READ | GENERIC_WRITE,
                      0, NULL, OPEN_EXISTING, 0, NULL);

DCB dcb = {0}; dcb.DCBlength = sizeof(dcb);
GetCommState(h, &dcb);
dcb.BaudRate = CBR_9600; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT;
SetCommState(h, &dcb);

COMMTIMEOUTS tout = {0};
tout.ReadIntervalTimeout = 50; tout.ReadTotalTimeoutConstant = 100;
tout.ReadTotalTimeoutMultiplier = 10;
SetCommTimeouts(h, &tout);

DWORD written, read; char outch = 'A', inch;
WriteFile(h, &outch, 1, &written, NULL);
if (ReadFile(h, &inch, 1, &read, NULL) && read == 1) { /* process inch */ }

CloseHandle(h);

Quick troubleshooting checklist: confirm the port name and that no other program (HyperTerminal) has it open; verify identical baud/byte-size/parity/stop settings on both ends; check cable type (null-modem vs straight-through); use PurgeComm before tests; for COM numbers >=10 use the "\\.\COM10" naming; prefer a reader thread or overlapped reads to avoid blocking. The posted DOS/BIOS-based code and missing async helper functions explain why the snippets failed; providing a short, self-contained test (the open/config/read/write sequence above) plus the exact compiler/runtime error makes debugging much faster for responders.

Recommended Answers

All 14 Replies

This has been discussed many times before, even quite recently.

Plus you need to tell us which Operating system and Compiler you're using, since standard C++ knows nothing about serial ports.

i m using standard c++.........
i need a complete program on it..........
plz help me out........

This is a nice idea, but you should know that you're noy gonig to get any help just by asking, you need to show some effort. So how about you make a program up as best as you can and we can help. Also what COMPILER are you using and what OS are you using?

> i m using standard c++
Great, that really clears things up for us :icon_rolleyes:

> i need a complete program on it.
I'm sure you do.
And I'm also sure I could post one which works on my machine, but does nothing on yours.
Why, because you've not answered my first reply yet.

Oh, and read the intro threads on why we don't just give out complete answers just because you asked.

commented: Absolutely :) +2

i m using windows xp........
also standard c++ compiler..........
i mean visual c++........
i have some programes but are not working............
i also checked it with hyper terminal it works with it but i need this in c++........
help me out........
thanks

OK, so post one of your programs and lets see what we can make of it.

Oh, and drop all the ... at the end of each line, it's not adding anything.

ok sorry sir for this
i m posting my codes

no 1

#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include<time.h>
#include<dos.h>
 
 
#define COM1 0x03F8
#define COM2 0x02F8
#define DATA_READY 0X60B0
#define SETTINGS (_COM_9600|_COM_NOPARITY|_COM_STOP1|_COM_CHR8)
 
    int start_addr();
    int end_addr();
    void group_addr();
    void display_time();
    void transmit_frame(int*,int*);
    void recieve_frame(int*);
 
 
    void main(void)
 
    {
         char send;
         int abyte,in;
         int a,b,c,r,addr,status;
         unsigned int ret_value;
         int count, arr[6];
         int bit_num,s_addr, e_addr;
         int parity, baud,port,mode;
         int com_port_no,stop_bit;
         char abyte_stopbit, abyte_baud, abyte_parity,abyte_databit;
         char arr_baud[]= {0x00, 0x20, 0x40, 0x60,0x80,0xA0,0xC0,0xE0};;
 
         clrscr();
 
          //COM PORT SELECT SETTINGS//
         printf("\nSelect COM Port\nCOM 1\nCOM 2\n");
         scanf("%d",&com_port_no);
         switch(com_port_no)
            {
             case 1:port = 0;
                break;
 
             case 2:port =1;
                break;
            }
 
 
                        //TRANSMISSION MODE//
                  clrscr();
                 addr = start_addr();
                 printf("\nESC to exit\n");
                // for(;;)
 
                    {
                transmit_frame(&addr,&port);
                    delay(5);
                    printf("\n");
                    recieve_frame(&port);
                    }
                  printf("\n");
 
 
 
 
        getch();
 
        }
 
     void transmit_frame(int*addr,int*port)
        {
 
         unsigned int in;
         int trans,i;
         char arr[] = {0x01};
 
 
 
         if(kbhit())
            {
               if(getch()=='\x1B')
                { exit(0);
                }
            }
         else
            {
                trans = arr[0];
                printf("%x ",trans);
                _bios_serialcom(_COM_SEND,*port, trans);
                delay(100);
            }
 
        }
 
 
 
    void recieve_frame(int*port)
        {int v;
        int trans;
 
        unsigned int status,out;
     {
       status = _bios_serialcom_COM_STATUS,*port,SETTINGS);
       printf("%x",status);
           
              out=_bios_serialcom(_COM_RECEIVE,*port,0);
              trans = out;
              printf("\n%x",trans);
           
 
 
         }
 
         }
 
    start_addr()
        {int s_addr1;
        printf("\nStart Address:");
        scanf("%x",&s_addr1);
        return(s_addr1);
        }

===============================
===============================
===============================
===============================
===============================


no 2

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "async.h"

#define	COM_PORT	COM1
#define BAUD_RATE	38400
#define	COM_SETTINGS	(BITS_8 | STOP_1 | NO_PARITY)

int	main()
{
int	c;

printf( "Simple-Term.  Press F10 to Quit.\n\n");

if( AsyncInit( COM_PORT))
	{
	printf( "Error initalizing COM Port.\n");
	return(1);
	}

AsyncSet( BAUD_RATE, COM_SETTINGS);
AsyncHand( DTR | RTS);

for(;;)
	{
	/*	If a character was recieved, print it to the console */
	if( (c=AsyncIn())!=0)
		putch( c);

	/*	If a key has been pressed	*/
	if( kbhit() )
		{
		c=getch();
		if( c==0)	/* Check for 'extended characters */
			{
			c=getch();	/* Get the extended code */
			if( c==0x44)	/* Exit if it is a F10 */
				break;
			}
		else
			AsyncOut(c);
		}

	/*	If the buffer has a lot in it, drop RTS and empty the buffer */
	if( AsyncInStat()>4096)
		{
		AsyncHand( DTR);	/* Drop RTS */
		while( AsyncInStat()>0)
			putch( AsyncIn() );
		AsyncHand( DTR | RTS);
		}
	}

AsyncStop();
return(0);
}

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

CODE 1:

#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include<time.h>
#include<dos.h>

void main(void)

... and many others...

i m using standard c++.........

(!!!)
I bet you're kidding.

Try formatting your code properly. That would make ot more readable and comprehendable. And codes similiar to this one are written for DOS based C++ compilers. Even if there's no other error, this is not meant for Standard C++

CODE 2:

You've not given the function definitions in async.h file in the second code. Go ahead, keep the others wondering.

Since you're using VC6, it would be better to use a win32 API function to open the serial port, such as CreateFile

CreateFile Function

Creates or opens a file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, or named pipe. The function returns a handle that can be used to access the object.

seriosly i dont know too much of c++
i have downloaded these codes from net
help me if you can

seriosly i dont know too much of c++
i have downloaded these codes from net
help me if you can

Really? I would have never guessed...

If you want to learn C++, this is probably the worst possible way to do it. There are many invaluable books and resources that can help you get started.

Try reading C++ Primer Plus, 5th Ed. by Stephen Prata, or something similar, to get you started. Once you can write some code for yourself, I'm sure people will be happy to help you with it.

My stock answer for all things serial is for you to dig around this site.

dot.muffin is right, you should learn some more C++ before downloading random snippets of code which just mention "serial port", then complain that they don't work for you (and have no idea why).

you people jst cant make a chat program..............
i dont believe it...........

If you honestly think we can't help you because we aren't capable, you aren't too bright yourself.

Check the sticky thread 'Read this before posting'

In other words, we aren't going to help you with your homework, kid.

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.