Link to home
Start Free TrialLog in
Avatar of sheeparegreat
sheeparegreat

asked on

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

Simon

Avatar of Francoz
Francoz

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';
 memcpy(fsample,c,1);
 //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
Avatar of sheeparegreat

ASKER

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?

ASKER CERTIFIED SOLUTION
Avatar of Francoz
Francoz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

https://www.experts-exchange.com/questions/20567915/Convert-file-to-bitstream-and-send-thru-serial-port.html
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.

PROTOCOL SO FAR:

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