• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Serial Port Communications in Visual C++

Hello everyone,

I have to write software that talks to a device via the serial port on a pc.  The brief says that the software must have a GUI.  The device that has been chosen is a 16 character display.  The data sent to the device must be ideally in a binary format.  This is because the data sent will have to be encoded in certain ways for error correction and framing etc.  The serial port is to have no hardware handshaking or parity because this is ment to be achived in the software.

My Question:
How can you write to the serial port in a binary format and how can I change this into a string format for use in a MFC dialog application.

So far I have used a 'serial wrapper' that I have found and have sucessfully sent and recieved characters using a loop back plug that i made.

Any help with this would be most greatfully recieved


  • 3
  • 3
1 Solution
To interact with devices connected at the serial port you can use the standard win32 APIs like CreateFile, WriteFile and ReaFile. The I/O device here is treated as file buffer and data flow is permitted.

If you need the help regarding making the strings into binary format, you can use the functions memcpy to fill the frame.
For example consider the frame below

struct frame
 char startbit;
 chat data[10];
 char stopbit;

Fill the above frame as follows,

 struct frame* fsample;
 char c = 't';
 //continue with other elements

Then send it with the help of WriteFile API. Since you've one GUI for user interaction, always prefer to use one thread to perform communication tasks. Better if it is one separate dll.

Tell me if I can further help you.
It's possible that you can write binairy data to the display not in the way that Francoz say's becouse he's writing char's to the com port and I think that's not the meaning. You will have to dig up some asm or c++ to write directly to the com port and make a char frame like this:

1 = start bit, 2-11 = data,12 = stop bit

1011 0001 1110
|   |    |    |
char char char

it's like boulding a frame and the char's that you'l send binairy it's harder than blindly sending byte's
Greets Captnoord
sheeparegreatAuthor Commented:
Thanks for the input so far.
Francoz's part on using a sturcture to model the frame is a very good idea.  The purpose of the software should really be elaborated.. he goes...

The software should have a text area and a button.  The text to be displayed on the display should be written in the display when the button is pressed.
The reason that binary data and not character data is to be used is that before the data is sent it must be encoded because the data is being sent over an infrared link.  The data is encoded in such a way that it is possible to recover the data even if some of it is corrupted.

Because of this binary data has to be sent from the port that is the output of the error correction, this is why outputting straight characters would not work.

The serial port can only output bytes at a time though i think (because of the setup).  The data wil therefore have to be grouped up into 8 bits before sending.

Is it possible to send bytes?

Thankyou for being patient, this is my very first post.  How does this points system work?...can i award them how i like of does someone have to get them all?

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

                  First of all you should've a small protocol to implement communication. Dont think big...:)
You can use the unsigned char or BYTE to club 8 single bytes together. If you're not able to transmit data with WriteFile API, you'll have to opt for low level API's either in C or assembly. outp, outport are of that kind. They can be used to send bit by bit instead of bytes.

            Your protocol design should find out the max permissible frame length also. Then fill up the frame with BYTEs and try to send. Anyhow I feel the first step for you'll be designing a protocol according to the device specifications.
sheeparegreatAuthor Commented:
The protocol is what im working on now.  I am researching what i can do with the software before i make firm decitions about the protocol.(Fudging it :-)  )

The display works via a microcontroller.  My friend wants me to send him ascii characters as this can be written to the dispay by use of the corressponding hex codes.

We have decided to have a start sequence of bits followed by the data to be displayed and then the stop sequence.  We also needed to have data stuffing in order that the control characters are not found in the data sequence (hence the need for the data bits do be manupulated).

Im thinking of having an array that can used as a 'shift register' to manipulate the data frame and add the extra bits as needed before transmission....

Anyway enough digression

Thanks for your help everyone (especially Francoz)

PS: Is there a way to change from bytes to characters?  I have searched the windows sdk and there is nothing on this (except in vb)
If you're familiar with VC++, BYTE is nothing but
typedef unsigned char. I dont know again what exactly you meant.

See something I've given for the below qn.

sheeparegreatAuthor Commented:
I am new to c++ and Visual C++. I have experience of Java.

The reason for choosing c++ to do this project is the power of manipulation that you have over the data (contrasting vb).

I am trying to send manipulated data through the serial port to the display.  The data recieved at the display must be ascii data after all the decoding etc.


Start frame sequence:   ASCII $   =  "0010 0100"

Stop frame sequence :   ASCII &   =  "0010 0110"

Data stuffing sequence  ASCII ESC =  "0001 1011"

(The data stuffing sequence is to be inserted if the start or stop sequence is detected in the data bits)

The display can display 16 character so the minimum frame size in bytes is 18 (start + data + stop).

My question is:
If the device sends back characters how can i convert the binary sequence into a character?

Hope thats abit clearer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now