Serial interface (RS232) with C++

S

Thread Starter

setienne

Hi everybody

I do want to send and receive ASCII-code over a RS232 interface. The communication speed should be as fast as possible. (Real-time application)

Is there somebody who allready has solved this problem? Or somebody who knows how to solve it?

Please write me a mail!
[email protected]
Thanks!

Kind regards
Etienne
 
C

Curt Wuollet

Take a look at the source for any Linux serial application. Nearly anything will send and recieve ascii with a serial port as fast as possible, the maximum baud rate is a trivial load for most processors these days. The Linux source is usually well done and is freely available, even in China.

Regards

cww
 
<p>Sealevel Systems (http://www.sealevel.com) includes code samples on the software CD that ships with all serial products. Sealevel serial products, depending on models, can support up to 921K bps, or faster for RS-422/485.

<p>This C++ code sample shows you how to open a COM port, and send & receive data. In this example, data is sent and read at 460K bps, but standard windows ports are limited to 115K bps. If you are using a Real Time OS, you can also use overlapped I/O and double buffers, but that example is too long to demonstrate here.
<pre>
----- Begin Code Sample -----

#include "stdafx.h"
#include <stdio.h>
#include <conio.h>
#include <windows.h>
int main(int argc, char* argv[])
{
char INBUFFER[500];
char OUTBUFFER[20];
DWORD bytes_read = 0; // Number of bytes read from port
DWORD bytes_written = 0; // Number of bytes written to the port
HANDLE comport = NULL; // Handle COM port
int bStatus;
DCB comSettings; // Contains various port settings
COMMTIMEOUTS CommTimeouts;
strcpy(&OUTBUFFER[0], "The quick brown fox jumped over the lazy dog. \n\r\0");
// Open COM port
if ((comport =
CreateFile("\\\\.\\COM5", // open com5:
GENERIC_READ | GENERIC_WRITE, // for reading and writing
0, // exclusive access
NULL, // no security attributes
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE)
{
// error processing code goes here
}
// Set timeouts in milliseconds
CommTimeouts.ReadIntervalTimeout = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 100;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 100;
bStatus = SetCommTimeouts(comport,&CommTimeouts);
if (bStatus != 0)
{
// error processing code goes here
}
// Set Port parameters.
// Make a call to GetCommState() first in order to fill
// the comSettings structure with all the necessary values.
// Then change the ones you want and call SetCommState().
GetCommState(comport, &comSettings);
comSettings.BaudRate = 460800;
comSettings.StopBits = ONESTOPBIT;
comSettings.ByteSize = 8;
comSettings.Parity = NOPARITY;
comSettings.fParity = FALSE;
bStatus = SetCommState(comport, &comSettings);
if (bStatus != 0)
{
// error processing code goes here
}
while(!kbhit())
{
bStatus = WriteFile(comport, // Handle
&OUTBUFFER, // Outgoing data
48, // Number of bytes to write
&bytes_written, // Number of bytes written
NULL);
if (bStatus != 0)
{
// error processing code here
}
bStatus = ReadFile(comport, // Handle
&INBUFFER, // Incoming data
500, // Number of bytes to read
&bytes_read, // Number of bytes read
NULL);
if (bStatus != 0)
{
// error processing code goes here
}
// code to do something with the data goes here
}
CloseHandle(comport);
return 0;
}
</pre>
 
N

Nico Vangoethem

if (bStatus != 0) { /* error processing code goes here */ }

... is not correct. The return values for SetCommTimeOuts() are as follows:

- If the function succeeds, the return value is nonzero.
- If the function fails, the return value is zero.


if (bStatus == 0) { /* error processing code goes here */ }

... should work
 
Ok, which DCB parameters need to be set to indicate "Flow Control = None"?? I it isn't as simple as DCB.FlowControl = Hardware or Xon/Xoff or None.
 
Top