×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Need help with Serial IO (com1, com2, etc.) - setting parameters

Need help with Serial IO (com1, com2, etc.) - setting parameters

Need help with Serial IO (com1, com2, etc.) - setting parameters

(OP)
I need to write a class which will handle input and output for a serial port under windows and/or DOS. I need to be able to set the baud rate, parity, stop bits, and size. I need a function that will let me send one byte and another function that will read one byte. Thanks very much.

RE: Need help with Serial IO (com1, com2, etc.) - setting parameters

If you are using Borland's compiler, you might want to check out bios.h . This library some functions that deal with the DOS serial ports, e.g. bioscom, _bios_serialcom. You will have to watch closely since most bios functions will NOT work under Windows, esp. Win9X & NT.

Essentially, you are going to define some settings such as:

#define COM1 0
#define DATA-READY 0x100

then you will set up a setting that you will pass to the bios call:

#define SETTINGS (_COM_1200 ¦ _COM_CHR7 ¦ _COM_STOP1 ¦ _COM_NOPARITY)
or
#define SETTINGS (0x80 ¦ 0x02 ¦ 0x00 ¦ 0x00)

Which bios call you use will determine how you set up the settings. (You will need to see your online documentation for better examples.)

_bios_serialcom(_COM_INIT, COM1, SETTINGS);
or
bioscom(0, SETTINGS, COM1);

For Windows 9X or NT, you cannot access the bios directly, so you have to go through something like a virtual driver.

If your assignment is for the "or DOS" part, this will help, otherwise, you'll need more/better help than I just supplied.

RE: Need help with Serial IO (com1, com2, etc.) - setting parameters

The following example uses the BuildCommDCB and SetCommState functions (Windows API calls) to set up COM1 at 9600 baud, no parity, 8 data bits, and 1 stop bit. These instructions are by no means complete; ie: variables haven't been declared, etc.



idComDev = OpenComm("COM1", 1024, 128);
if (idComDev < 0) {
ShowError(idComDev, "OpenComm");
return 0;
}

err = BuildCommDCB("COM1:9600,n,8,1", &dcb);
if (err < 0) {
ShowError(err, "BuildCommDCB");
return 0;
}

err = SetCommState(&dcb);
if (err < 0) {
ShowError(err, "SetCommState");
return 0;
}

Let me know if you need more detailed info.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close