Serial Port Communications in Visual C++

Posted on 2003-03-30
Medium Priority
Last Modified: 2013-12-14
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


Question by:sheeparegreat
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
  • 3
  • 3

Expert Comment

ID: 8236224
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.

Expert Comment

ID: 8237211
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

Author Comment

ID: 8237427
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?

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

Francoz earned 300 total points
ID: 8237518
                  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.

Author Comment

ID: 8237863
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)

Expert Comment

ID: 8237959
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.


Author Comment

ID: 8238251
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

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

765 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