Serial Port Communication in C or C++

R

Thread Starter

Rodrigo

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!
 
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.

Regards,
 
R

Robert Scott

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
 
D

David Wooden

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?
 
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 [email protected].

- Punita
 
P

Philippe Boudot

<p>To open and configure the COM port:
<pre>
HANDLE hCom;
COMMTIMEOUTS lpTo;
COMMCONFIG lpCC;

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,
OPEN_EXISTING,FILE_FLAG_OVERLAPPED,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 );

GetCommTimeouts(hCom,&lpTo);
lpTo.ReadIntervalTimeout = 0;
lpTo.ReadTotalTimeoutMultiplier = 10;
lpTo.ReadTotalTimeoutConstant = 10;
lpTo.WriteTotalTimeoutMultiplier = 10;
lpTo.WriteTotalTimeoutConstant = 100;
SetCommTimeouts(hCom,&lpTo);

SetupComm(hCom,2048,2048);

To close the port :

CloseHandle(hCom);

To monitor communications events :


SetCommMask(hCom, dwEvtMask );

dwEvtMask is logical or with :
EV_BREAK, EV_CTS, EV_DSR, EV_ERR, EV_RING, EV_RLSD , EV_RXCHAR, EV_RXFLAG, EV_TXEMPTY values

and used WaitCommEvent function

BOOL WaitCommEvent(
HANDLE hFile,
LPDWORD lpEvtMask,
LPOVERLAPPED lpOverlapped
);
</pre>
<p>hope that it will help you.

<p>Philippe Boudot<br>
Electronic Research & Development<br>
CP Industrial Business Unit<br>
Email: [email protected]
 
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.
 
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?

Thanks.
Marcelo.
 
C

cihan uzunoglu

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.
 
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..
 
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.


COMMUNICATION PARAMETERS:

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

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

PARAMETERS USED:

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.

LIST OF COMMANDS :

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
=03 for INITIALIZE PLC.

COMMUNICATION PATTERN :


COMMAND DETAILS :

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
 
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.
 
<p>this is the code for ur project in c. for further queries u can contact mr. prateek gupta from nitk.

<pre>
#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*/
putch(out);

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

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

}

return 0;

}
</pre>
 
Can anyone help me to complete c++ dll function to read com port?

Hi,

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<pre>
#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;


openPort(portNum,baudRate,dataBit,parity,stopBit);
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 < 32) // replace unreadable control-codes by dots
{
buf = '.';
}
}
} else {
return buf2;
}
return buf;
}</pre>
 
Top