Solved

reading/writing to COM1 using MFC

Posted on 1998-09-23
2
572 Views
Last Modified: 2013-11-19
I need to read/write to COM1 using Visual C++, I'm talking
to an external board. The board is connected to a video
camera, essentially the whole thing is a vision system for
little robots (robotic soccer). So the external vision board
sends data on the location of the soccer ball, which I must
read in off of COM1. I will also need to write data once in
awhile. thanks!

Anthony Nicholson
nicholso@ukans.edu
0
Comment
Question by:nicholso
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 

Accepted Solution

by:
dataman earned 100 total points
ID: 1322530
You can find an object which handles serial communication on the following site
(including the source code)
www.codeguru.com

I did not use that specific object but I used other objects from this site and they
are very good.

Hope this helps


0
 
LVL 6

Expert Comment

by:snoegler
ID: 1322531
Dataman, you seem to have answered before i did :) But i'll post this as a comment, though.
The COM port is treated like a file in the Windows environment. To open the COM1 port, you
can use something like this:

HANDLE hComPort=CreateFile("COM1:",
                                                         GENERIC_READ|GENERIC_WRITE,
                                                         0, // dwShareMode
                                                         NULL, //lpSecurityAttributes
                                                         OPEN_EXISTING,
                                                         FILE_FLAG_WRITE_THROUGH,
                                                         NULL);  // hTemplateFile

Important: You have to use OPEN_EXISTING(dwCreationDistribution).

Then, you'll have to set the communication parameters(the example below is for
9600,n,8,1, CTS/RTS flow control)

a) "complex" version (see "easy" version below)

DCB  commDCB;

ZeroMemory(&commDCB,sizeof(commDCB)); // set all unused values to zero
commDCB.DCBlength=sizeof(DCB);
commDCB.BaudRate=9600;
commDCB.fBinary=TRUE; // no eof check
commDCB.fParity=FALSE;// no parity checking (see also commDCB.Parity)
commDCB.fOutxCtsFlow=TRUE; // enable CTS
commDCB.fOutxDsrFlow=FALSE; // no DSR
commDCB.fDtrControl=DTR_CONTROL_DISABLE; // no DTR
commDCB.fDsrSensitivity=FALSE;
commDCB.fNull=FALSE; // do not omit '\0' characters
commDCB.fRtsControl=RTS_CONTROL_HANDSHAKE; // RTS handshake enabled
commDCB.fAbortOnError=FALSE; // no aborting if error occurs
commDCB.ByteSize=8; // 8 Bits
commDCB.Parity=NOPARITY;
commDCB.StopBits=ONESTOPBIT;
commDCB.fOutX=FALSE; // no XON/XOFF handshaking on sending
commDCB.fInX=FALSE; // no XON/XOFF handshaking on receiving
SetCommState(hComFile,&commDCB);

b) "easy" version:

DCB commDCB;
BuildCommDCB("baud=9600 parity=N data=8 stop=1");
// Pls. see the online help on 'BuildCommDCB' for further details, as it is a quite large
// subject

At last, you should the the timeouts(the example sets them to 'none' - so ReadFile() and
WriteFile() return immediately if nothing was received/sent).

COMMTIMEOUTS cto;
cto.ReadIntervalTimeout=MAXDWORD;
cto.ReadTotalTimeoutMultiplier=0;
cto.ReadTotalTimeoutConstant=0;
cto.WriteTotalTimeoutMultiplier=0;
cto.WriteTotalTimeoutConstant=0;
SetCommTimeouts(commFile,&cto);

Now, you can read and send data:

BYTE readBuffer[16];
DWORD bytesRead=0;
BOOL success;
success=ReadFile(commFile,(LPVOID)readBuffer,16,&bytesRead,NULL);
if(!success)
  // some error occured - this does *not* happen if there is no input to read!!!

if(bytesRead==0)
  // no data available

// Data received, number of bytes in bytesRead


'WriteFile' sends data in the same manner as above.

You can also use an OVERLAPPED structure if don't want to poll if there is data to read.
But if your application isn't that time critical you could possibly use a WM_TIMER message
to check if there is data available. OVERLAPPED offers event-driven reading, so you
can come closer to realtime, if necessary. If you want, i can explain.

Hope this helped :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
viewing source code from eclipse 13 111
fizzArray2 challenge 1 112
zeroFront challenge 7 136
canBalance challenge 34 111
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question