from the Automation List department...
Serial Port Communication in C or C++
Programming languages. topic
Posted by Rodrigo on 6 January, 2004 - 2:45 pm
Ok people, I am in sooooo much trouble!

I just got this job to do, and I have NO idea of how to do it. I need to make a .dll file so that you can open, close and view the status of the serial port. You should be able to control speed, timeout and stuff like that, can't remember.

Please help me out, I know like the basics of programming! PLEASE!

Posted by dodo1370 on 6 January, 2004 - 9:53 pm
You could build a ActiveX dll in VB using MSComm to control the serial port (it is very easy to work with this) and than you can use that as class for any other application.


Posted by Marcelo on 9 February, 2004 - 4:26 pm
Were can I find how to use the serial port (Mscomm) in a VB6 Activex Dll project? I have an exe program that control a DC motor interface and works very well. But now, I want to translate the same routines to a DLL Activex client project, but I get a Variable Object definition error when I use any MSCOmm1.xxxx method. Could you help me?


Posted by cihan uzunoglu on 12 February, 2004 - 12:59 am
can u help me for Mscomm? because I need to control the serial port pins through an interface. this will help me to control the joystick of an remote control car. I have no idea... can you show me a way?
thanks for considering.

Posted by tvmsojan on 28 March, 2004 - 2:23 pm
i want to know the details of serial port communication in c or tc reply in my mail

Posted by michael shiloh on 6 January, 2004 - 10:25 pm
My company is available on a consulting basis. Please contact me offlist if you are interested.

Michael Shiloh

Posted by Ashraf on 25 March, 2005 - 3:57 pm
I want to transfer simple data between two computers through serial port (via serial cable) communication using ANSI C. Can anybody help me plzzzzzzzzzz.. i will be very thankful.

Posted by Anonymous on 2 March, 2006 - 1:54 pm

this is the code for ur project in c. for further queries u can contact mr. prateek gupta from nitk.

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

#define COM1 0
#define DATA_READY 0x100
#define SETTINGS ( 0x80 | 0x02 | 0x00 | 0x00)

int main(void)

int in, out, status;
bioscom(0, SETTINGS, COM1); /*initialize the port*/
cprintf("Data sent to you: ");

while (1)

status = bioscom(3, 0, COM1); /*wait until get a data*/
if (status & DATA_READY)
if ((out = bioscom(2, 0, COM1) & 0x7F) != 0) /*input a data*/

if (kbhit())
if ((in = getch()) == 27) /* ASCII of Esc*/

bioscom(1, in, COM1); /*output a data*/


return 0;


Posted by Robert Scott on 6 January, 2004 - 10:28 pm
The "status" of the serial port, as you call it, is not under the control of anything other than the process that opened it. You cannot change the baud rate or anything else unless your process is the one actually using the serial port. But it sounds like you want to make some sort of a "monitor" process to oversee the usage of the serial port. That is just not how it is done. Setting those parameters is just part of using the serial port (under Windows, anyway). Take a look at the Win32 documentation on communication for details.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting

Posted by David Wooden on 6 January, 2004 - 10:39 pm
Hello Rodrigo:

Can you be a little more specific? What development tools do you have or can you use? Does it have to be a dll or can you use a COM or ActiveX control? (The MSComm control does everything you need, if you can use it.) For what operating system(s): Windows 95, 98, ME, NT, 2000, XP, CE? What will talk to this dll? What sort of interface do you need, or can you define it?

Posted by Punita on 7 January, 2004 - 12:09 am
We can help you make a DLL for the serial port communications and status monitoring. I saw a post here that says that the serial port is under the control of the process that opens the port - RIGHT - but if you use the DLL to open the port, or pass in a port HANDLE to the DLL functions, you can get the status and do whatever I/O you want.

Please also indicate how and where you would like to use the DLL. For more get in touch at

- Punita

Posted by Rainer Lehrig on 7 January, 2004 - 11:46 am
Have a look at

This is written in C++ and runs on
Linux/Unix, OpenVMS and Windows.

Posted by Philippe Boudot on 7 January, 2004 - 5:45 pm

To open and configure the COM port:


char str_com[10];
unsigned short no_com = 0;

sprintf( str_com, "\\\\.\\COM%d\0", no_com+1);
hCom = CreateFile(str_com,GENERIC_READ|GENERIC_WRITE,0,NULL,
GetCommState( hCom, &lpCC.dcb);

/* Initialisation des parametres par defaut */
lpCC.dcb.BaudRate = CBR_9600;
lpCC.dcb.ByteSize = 8;
lpCC.dcb.StopBits = ONESTOPBIT;
lpCC.dcb.Parity = NOPARITY;

lpCC.dcb.fDtrControl = DTR_CONTROL_DISABLE;
lpCC.dcb.fRtsControl = RTS_CONTROL_DISABLE;
SetCommState( hCom, &lpCC.dcb );

lpTo.ReadIntervalTimeout = 0;
lpTo.ReadTotalTimeoutMultiplier = 10;
lpTo.ReadTotalTimeoutConstant = 10;
lpTo.WriteTotalTimeoutMultiplier = 10;
lpTo.WriteTotalTimeoutConstant = 100;


To close the port :


To monitor communications events :

SetCommMask(hCom, dwEvtMask );

dwEvtMask is logical or with :

and used WaitCommEvent function

BOOL WaitCommEvent(
LPDWORD lpEvtMask,

hope that it will help you.

Philippe Boudot<br>
Electronic Research & Development<br>
CP Industrial Business Unit<br>

Posted by rituert on 21 January, 2004 - 1:04 pm
I'm too interested in serial port by language c, but i need to do a comunication by modbus protocol, how can i do it? Thanks.

Posted by kinki on 28 March, 2004 - 12:05 pm
Hi, can any1 teach me how to write a simple serial communication program in C programming with ANSI C standard? Better if can run in both UNIX and window... I AM REALLY STUCKED..

Posted by Suresh on 3 September, 2012 - 2:33 am
Can anyone help me to complete c++ dll function to read com port?


i tried to write function in c++ and need to compile it as dll file. i want to use this dll from my other application. the other application will push values to dll file as portnumber, baudrate, etc...
below is the code, kindly help to compile & to read from comport
#include "stdafx.h"
#include "rs232.h"

int GetWeight( short, wchar_t * Data[], wchar_t **Output, SIZE_T *OutputSize )
int result = 1;

if( Data[0] )
*OutputSize = 50;
*Output = (wchar_t *) CoTaskMemAlloc(*OutputSize);

if( *Output )
wchar_t *EndPtr;
// the below values are pushed from my other application
double v11 = wcstod(Data[0], &EndPtr); // Port Name
double v12 = wcstod(Data[1], &EndPtr); // Baud Rate
double v13 = wcstod(Data[2], &EndPtr); // Data Bits
double v14 = wcstod(Data[3], &EndPtr); // Stop Bits
double v15 = wcstod(Data[4], &EndPtr); // Parity

int portNum = int (v11);
int baudRate = int (v12);
int dataBit = int (v13);
int stopBit = int (v14);
int parity = int (v15);

unsigned char* v16;

v16 = readPort(portNum);

// need to get comport reading as v16

swprintf(*Output, L"%.10lf", v16);
result = 0;

return result;


void openPort(int port_num , int baudrate ,int databits, int parity, int stopbit) {

if(OpenComport(port_num, baudrate,databits,parity,stopbit)) {
printf("Can not open comport");
return 1;
return 0;

unsigned char* readPort(int cport_nr)

unsigned char buf[4096];
unsigned char buf2[4096];

int n,i;
n = PollComport(cport_nr, buf, 4095);

if(n > 0) {
buf[n] = 0; //always put a "null" at the end of a string!

for(i=0; i < n; i++) {
if(buf[i] < 32) // replace unreadable control-codes by dots
buf[i] = '.';
} else {
return buf2;
return buf;

Posted by Anonymous on 6 April, 2004 - 12:37 pm
how to implement this protocol to read the registers in plc....????

plz any body out there help me to implement this protocol in c or VB 6.0 plzzzz,i need some code to implement this using com1 port of pc,for sending queries from computer and rechieving data from plc only.plz help me by providing some code for this.
the required data of protocol are as follows only i need is the code.


1. Baud Rate = 9600.
2. Number of data bits= 8.
3. Parity= ODD.
4. Number of stop bits= 1.

|10h 02h HEADER | length | comman byte | data field | cs check |


LN : Number of commands from & including command bytes but excluding CS byte.
CS : Binary addition of the characters from header of data.

Data Field Includes :

TT - type of bit/word (register)
00 for O/P
01 for I/P
nn - hexadecimal num 'n' indicating num of bits/words.
AL AH - Offset address in Hex.
DL DH- Data in Hex returned by PLC.


01 - Read 'n' consecutive words
15 - Force
11 - Write 'n' consecutive words
1A - CPU command
CC - =01 for put PLC in RUN mode
=02 for put PLC in STOP mode



1. CPU commands:
10 02 02 1A CC CS
10 02 01 5A CS

2. Read commands
10 02 05 01 nn TT AL Ah CS
10 02 2n+1 41 DL1 DL2 DL3...DLn DHn CS

3.Write commands
10 02 15 TT AL AH CS
10 02 01 55 CS

Posted by Satish on 13 November, 2009 - 3:41 am
Hey I found this useful link. Just give it a try l

Its not really complete from what I could see. But yea it gives you a basic idea. Helped me a lot. Just try.

Your use of this site is subject to the terms and conditions set forth under Legal Notices and the Privacy Policy. Please read those terms and conditions carefully. Subject to the rights expressly reserved to others under Legal Notices, the content of this site and the compilation thereof is © 1999-2014 Nerds in Control, LLC. All rights reserved.

Users of this site are benefiting from open source technologies, including PHP, MySQL and Apache. Be happy.

Political T.V. commercials prove one thing: some candidates can tell
all their good points and qualifications in just 30 seconds.
Advertise here
our advertisers
Help keep our servers running...
Patronize our advertisers!
Visit our Post Archive