Improve company productivity with a Business Account.Sign Up

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

Need to write a DOS program that sends string to COM port!

I need a sample DOS program written in C to send 3 byte string to COM port1 or 2.

Does anyone have a sample code about this issue?

I need the following functions to

open a port
send 3 byte string to a port and wait for return respond
close the port that has been opened.

  • 3
  • 2
1 Solution

If you use MSVC 1.x the bios_serialcom() function is the one you need.  Other compilers may call it differently.  Check <bios.h> fro details.

From the docs:

#include <bios.h>
unsigned _bios_serialcom(unsigned service, unsigned serial_port, unsigned data);
service - Communications service
serial_port - Serial port to use
data - Port configuration bits

The _bios_serialcom routine uses INT 0x14 to provide serial communications services. The serial_port parameter is set to 0 for COM1, to 1 for COM2, and so on.

The _bios_serialcom routine may not be able to establish reliable communications at baud rates in excess of 1,200 baud (_COM_1200) due to the overhead associated with servicing computer interrupts. Faster data communication rates are possible with more direct programming of serial-port controllers. See C Programmer's Guide to Serial Communications for more details on serial-communications programming in C.

The service parameter can be set to one of the following manifest constants:

_COM_INIT - Sets the port to the parameters specified in the data parameter

_COM_SEND - Transmits the data characters over the selected serial port

_COM_RECEIVE - Accepts an input character from the selected serial port

_COM_STATUS - Returns the current status of the selected serial port

The data parameter is ignored if service is set to _COM_RECEIVE or _COM_STATUS. The data parameter for _COM_INIT is created by combining (with the OR operator) one or more of the following constants:

_COM_CHR7 - 7 data bits
_COM_CHR8 - 8 data bits
_COM_STOP1 - 1 stop bit
_COM_STOP2 - 2 stop bits
_COM_NOPARITY - No parity
_COM_EVENPARITY - Even parity
_COM_ODDPARITY - Odd parity
_COM_110 - 110 baud
_COM_150 - 150 baud
_COM_300 - 300 baud
_COM_600 - 600 baud
_COM_1200 - 1200 baud
_COM_2400 - 2400 baud
_COM_4800 - 4800 baud
_COM_9600 - 9600 baud

The default value of data is 1 stop bit, no parity, and 110 baud.

The function returns a 16-bit integer whose high-order byte contains status bits. The meaning of the low-order byte varies, depending on the service value. The high-order bits have the following meanings:

15 - Timed out
14 - Transmission-shift register empty
13 - Transmission-hold register empty
12 - Break detected
11 - Framing error
10 - Parity error
9  - Overrun error
8  - Data ready

When service is _COM_SEND, bit 15 will be set if data could not be sent.

When service is _COM_RECEIVE, the byte read will be returned in the low-order bits if the call is successful. If an error occurs, any of the bits 9, 10, 11, or 15 will be set.

When service is _COM_INIT or _COM_STATUS, the low-order bits are defined as follows:

7 - Receive-line signal detected
6 - Ring indicator
5 - Data set ready
4 - Clear to send
3 - Change in receive-line signal detected
2 - Trailing-edge ring indicator
1 - Change in data-set-ready status
0 - Change in clear-to-send status

Note that this function works only with IBM personal computers and true compatibles.

          You can use inport and outport functions defined in dos.h header file.
>> You can use inport and outport functions defined in dos.h header file.
inport() and outport() perform I/O from the *processor* ports, not the serial ports.
Hi alexo,
                      I   think we can use inport and outport for serial ports also by specifying their address or port ID . isn't it.
Not really.  Although there are processor ports corresponding to the serial ports, there is a protocol involved.  You cannot just dump/read the data using in/out instructions and expect it to work.
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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