interfacing RS232 in WinXP using VC++

G

Gildas Trebaol

Hello,

If you are still looking for some code to read or write the RS232 COM port, here is a utility that I used in november 2004 for controlling ADAM/NuDAM DAQ modules. The code is developed for Win32, OS-9 and Unix, but I have not tested it on Unix yet (it works on Win32 and OS-9.

You can get it at:
http://gtrebaol.free.fr/rs232_rs485/tty.zip

If you improve it or find some bugs, please send me your updates.

Best regards,

Gildas

NB:
I tested the RTS_CONTROL_TOGGLE mode with RS485 devices, and I noticed that Windows has a significant delay between the end of transmission and the control of the RTS line,
so this mode does not work properly with a RS485 equipment that replies "too fast" for Windows, due to a conflict between the RS422 amplifiers simultaneously active on the RS485 line.
 
K

Kunal Lagwankar

<p>Hello

<p>If you are still looking for a sample code, look below.
<pre>
#include <windows.h>
#include <stdio.h>
#include "globs.h"

void InitCommPort(void)
{
DCB dcb;
char *pcCommPort = "COM1";
COMMCONFIG CC;
DWORD dwSize;
COMMTIMEOUTS timeout;
extern HANDLE hCom;
extern int eflag;

hCom = CreateFile( pcCommPort,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);

if(!GetCommState(hCom,&dcb))
{
printf("\nGetCommState failed with error %d\n",GetLastError());
eflag = -2;
}
else
{
dcb.BaudRate = CBR_9600; // set the baud rate
dcb.ByteSize = 8;
dcb.Parity = NOPARITY; // no parity bit
dcb.StopBits = ONESTOPBIT; // one stop bit
}

GetCommConfig(hCom,&CC,&dwSize);
CC.dcb = dcb;

SetCommConfig(hCom,&CC,dwSize);
GetCommTimeouts(hCom,&timeout);

timeout.ReadIntervalTimeout = 0;
timeout.ReadTotalTimeoutConstant = 500;

timeout.ReadTotalTimeoutMultiplier = 0;
timeout.WriteTotalTimeoutConstant = 0;
timeout.WriteTotalTimeoutMultiplier = 10;

if(!SetCommTimeouts(hCom,&timeout))
{
printf("\nError setting timeout %d\n",GetLastError());
eflag = -1 ;
}
}

int AnalogPoll(unsigned char *Xbuffer, unsigned char *Rbuffer)
{
short retry =0;
DWORD BytesWritten, BytesRead, n;
extern RTU Meter[TERMINALS];
extern HANDLE hCom;
extern int eflag;
unsigned register i;

while( (!WriteFile(hCom,Xbuffer,8,&BytesWritten,NULL)) & (retry < 2) )
retry++;

if ( (retry == 3) || (BytesWritten != 8) )
{
printf("\nWrite to Meter %d failed with %d\n",Meter[0].Request.SlavAddr,GetLastError());
eflag = -1;
return -1;
}
else
{
printf("\nTransmitted data = ");
for(i=0;i<8;i++)
printf("\t%2X",Xbuffer);

n = Meter[0].Request.ReadCount*2 + 5;

retry = 0;
while( (!ReadFile(hCom,Rbuffer,n,&BytesRead,NULL)) & (retry < 2) )
retry++;


if ( (retry == 3) || (BytesRead != n) )
{
printf("\nRead Error %d !\n",GetLastError());
eflag = -1;
return -1;
}
else
printf("\nData Read = ");
for(i=0;i<BytesRead;i++)
printf("\t%2X",Rbuffer);
}
return 0;
}
</pre>
 
Hi,

dear friends i want some help from you. i am working on RFID project. i want to develop such software that RFID reader send some data to serial port of pc continously. this is done by RS232 cable. do u have any source code in c/c++/vc++ or which tool i use, also language so that i can achieve it. please send me mail or reply.

thanking you
vikrant
 
hi,

i have a problem to compile your code source in a platform Windows NT so i have all time an error message indicate a link problem
est_tty.obj : error LNK2001: unresolved external symbol __imp__timeGetTime@0

Debug/test_tty.exe : fatal error LNK1120: 1 unresolved externals

[email protected]
I will be to you t-pieces recognizing
 
Hi,

I'm working on the RS232 connection to Vc++ now, i need to connect them without using MScomm. So, any other functions or classes that support this?

And I need the code for #include "globs.h" too...
Can you please send me? Thanks!
 
Hello Gildas,

I was looking for a RS485 sample code and I found yours. It is a great work!!! But, I am working under win2k Pro using visual C++ 6.0 and it does not compile at the first try. In your sources files there is no OS protection. I have added what I need to run it under win2k and it works now; so good job you have done.

My question:
Is this an RS485 interface? (just to be sure!) Is this for a specific device? (I have seen specials commands <<strcpy( buf, "$28MDB" );>> and others). What can I do or modify to use it for an other device?

Thank you in advance for your help.
Theo
 
I have the same problem so send me the sample code and other comments regarding RS232 interface in vc++
 
Hi,

i have the same problem as vikrant im also working on a RFID project that requires my reader to send data to my PC through the serial port with the use of RS232. could someone please help me in finding any source code in c/c++/vc++/java or which tool i use, also language so that i can achieve it. please send me mail or reply.

Joel
 
Hi,

I also have a same problem with what you have mentioned about RFID project. If you've already got that program codes, can share with me?

Thanks & regards,
may
 
Top