I am writing a C/C++ program using Microsoft Visual C++ 6.0, and I
have to do what seems like a trivial task. I want to output a line of
ASCII text to an RS-232 serial COM port (either COM1 or COM2), and I
also need to to set the port characteristics to 4800 baud, 8 data
bits, 1 stop bit, no parity.
If necessary I (presumably) can set the COM port values using a DOS
command (e.g., MODE COM1 BAUD=4800 PARITY=0 DATA=8 STOP=1). But I'm
having problems getting my C/C++ program to write to the port. For
example, I tried to do the following:
FILE *out1;
out1 = fopen("/dev/tty1", "w");
fprintf(out1, "Transmit two floating point values: %f, %f\n", value1, value2);
fclose(out1);
Visual C/C++ doesn't seem to like "/dev/tty1"; this may be an obsolete
formulation. But when I look through technical support websites and
other programs which utilize C++ code for serial interfaces, I find
some enormously complicated code involving handles and other functions
which seem like overkill. In some cases I don't even have the
libraries for those functions.
Isn't there some simple way to accomplish this trivial task? Or if
not, what is the least complicated way to accomplish this trivial
task? I just want to get on with writing my program, I don't want to
spend hours becoming an expert in serial interface functions.
I'm on a tight schedule, so any help would be greatly appreciated.

Hi, dpwiener-ga:
What operating system will your program urn under? You mention using
a DOS command, but I suspect your target is a 32-bit version of
Windows.
The general idea is correct. One needs to configure the serial port
and then open the serial port for reading and writing as a file.
However MS Windows will put obstacles in your way for directly
accessing the hardware.
If you have Visual Basic 6.0 as well as Visual C++, then you will
already have an ActiveX control that can be used for the serial port
operations. If not I can point you to documentation for doing
everything from scratch in C++.
regards, mathtalk-ga

Clarification of Question by
dpwiener-ga
on
30 Jun 2004 13:32 PDT

Hello mathtalk-ga. My OS is Windows 2000 Professional. I do not have
Visual Basic, but if necessary I do have Compaq Visual Fortran
Professional Edition 6.6.a. (I'd prefer not to have to resort to
Fortran.)
Thank you for your suggestion, corwin02-ga. Visual C++ says it can't
open serial.h, so I haven't been able to try out that code. The
tutorial you pointed me to may or may not be able to solve my problem,
but I'm still hoping that there's an easy solution which doesn't
require me to spend hours becoming an expert on what ought to be a
trivial function.
Twenty two years ago I could easily accomplish this on my Kaypro II
computer. I suspect I could have easily done it with real DOS or
Windows 95. Why is it suddenly so damn difficult?

Hi, dpwiener-ga:
The point would not be to throw a new language into the project, but
rather to make use of existing software components that can configure
the serial port for you under Win2K. Such a component that is bundled
with VB is mscomm.ocx:
[MSComm Control]
http://msdn.microsoft.com/library/en-us/comm98/html/vbobjcomm.asp
Using the serial port in VC++ 6.0 is easier than under C#/.Net, though
not as simple as doing it in Basic under MSD0S. However one relies
even then on an operating system supplied device driver that manages
the characteristics of the serial port as a "file" or byte-stream.
Under Win2K the operating system supplied device driver is called
serial.sys, and the serial.h header file that corwin02-ga and andyt-ga
refer to should be installed with the DDK for Visual C++ 6.0. If
you've installed the DDK (driver developer's kit) it should be in this
location:
%DDKROOT%\src\kernel\serial\serial.h
Relative to the code snippet you posted, a more correct approach would
be to return a file handle by calling CreateFile("COM1",...). For a
quirk of this Win32 API call for serial ports beyond COM9, see here:
[HOWTO: Specify Serial Ports Larger than COM9]
http://support.microsoft.com/?id=115831
regards, mathtalk-ga

Clarification of Question by
dpwiener-ga
on
30 Jun 2004 19:25 PDT

I do not have a DDK, and I don't know that I can get it within my
compressed time-frame for solving this problem.

Okay, let's rule out doing things (serial port management) from
scratch in C++ and think about reusing an existing component. As I've
mentioned, Microsoft bundled an ActiveX/COM control called mscomm.ocx
that is frequently used in VS6 projects, although it doesn't seem to
have been part of the distribution you have. You could nonetheless
get the control and Add Reference it to your project as a library,
thereby exposing its interfaces within the Visual Studio editor (even
if you are writing C++ code).
As an alternative you could use the Code Project's CSerial class, as
suggested by corwin02-ga and andyt-ga. I acknowledge that you got
some linker errors in trying to use that in your project, but it might
be fruitful to drill down and eliminate those problems. It certainly
looks like it might just be an Add Reference issue.
First though, can you verify that you are using the Visual Studio
environment rather than a command line version of VC++?
regards, mathtalk-ga

Clarification of Question by
dpwiener-ga
on
01 Jul 2004 07:16 PDT

Yes, I am using the Visual Studio environment.

Clarification of Question by
dpwiener-ga
on
03 Jul 2004 09:39 PDT

I finally resorted to using Fortran (in conjunction with my C++
program) to perform the serial COM port writing function. It was a
bit of a hassle getting it to work and to properly link. However, I
think it will accomplish my objective. I still have a bit more
testing to do, but I decided to call it a night at 2 am and finish up
later (my deadline was to have it ready to run early July 5th).
So I'm going to cancel this question. I really appreciate the help
and suggestions I received. And I still think it's ridiculous that it
has gotten so much more difficult to perform such a trivial task in
C/C++. It should be as easy as writing to a monitor or a file. Oh
well.

Important Disclaimer:
Answers and comments provided on Google Answers are general
information, and are not intended to substitute for
informed professional medical, psychiatric, psychological, tax,
legal, investment, accounting, or other professional
advice. Google does not endorse, and expressly disclaims
liability for any product, manufacturer, distributor,
service or service provider mentioned or any opinion
expressed in answers or comments. Please read carefully the
Google Answers Terms of Service.

If you feel that you have found inappropriate content,
please let us know by emailing us at
answers-support@google.com
with the question ID listed above. Thank you.