advertisement
from the serious department...
serial port in visual c++
Communications systems and equipment. topic
Posted by saeed vahabi on 14 December, 2005 - 9:51 pm
I have an AVR Microcontroller which send data to pc from serial port (rs-232) and get data from serial port (rs-232). also i should say that our AVR Microcontroller is in touch with pc just with 2 pins, [pin 3 (TD=Transmit data) and pin 2 (RD=Receive data)]. for example, Microcontroller send two number (A=1,B=2) to pc from serial port (rs-232) and visual c++ get it and add those number (A+B=3) then sends it to Microcontroller from serial port (rs-232). please give me an example which do this.


Posted by wk on 6 January, 2006 - 1:04 am

Sealevel Systems (http://www.sealevel.com) includes code samples on the software CD that ships with all serial products. 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.

----- Begin Code Sample -----


#include "stdafx.h"
#include
#include
#include
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;
}


Posted by Ilyas on 8 January, 2006 - 11:43 am
It is easy to use MSComm control as compared to API.


Posted by Kashif on 8 February, 2006 - 12:26 am
Thanks for commenting.

If it is possible through the MSComm control then how?

Please guide me coz i am doing the same thing.

Kashif

affirmativeus@yahoo.com


Posted by Shrividya on 26 June, 2009 - 3:11 am
Hello,

i tried ur code but using WINAPI. when i execute the application, i am getting read file status = 1 which is fine but when i try to read the INBUFFER data i am not able to get proper data. i written a for loop to read the INBUFFER data

can you please guide me where i am going wrong

Reagrds
Ms. Shrividya
(shri.88vidya [at] rediffmail.com, rnd [at] eecindia.com)


Posted by dog on 25 October, 2009 - 9:38 pm
SetCommTimeouts() returns 0 on failure


Posted by dlchambers on 6 August, 2012 - 12:08 pm
The CommTimeouts in this code cause a huge read/write latency.
Changing the values as follows reduced my latency from ~200ms to ~6ms.

// A ReadIntervalTimeout value of MAXDWORD, combined with zero values for both the ReadTotalTimeoutConstant
// and ReadTotalTimeoutMultiplier members, specifies that the read operation is to return immediately with
// the bytes that have already been received, even if no bytes have been received.

CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;


Posted by Jegan on 18 August, 2012 - 9:20 pm
d:\computer programming with applications\cpa\wb\wb\wb1.cpp(1): fatal error C1083: Cannot open include file: 'stdafx.h': No such file or directory
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

i get this error when i run this program
can any one help me

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-2013 Nerds in Control, LLC. All rights reserved.

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


Fortune
Don't hate yourself in the morning -- sleep till noon.
Advertise here
Advertisement
our advertisers
Help keep our servers running...
Patronize our advertisers!
Visit our Post Archive