Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

milner

Programmer
Apr 15, 1999
28
0
0
CA
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.
 
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. <br>
<br>
Essentially, you are going to define some settings such as:<br>
<br>
#define COM1 0<br>
#define DATA-READY 0x100<br>
<br>
then you will set up a setting that you will pass to the bios call:<br>
<br>
#define SETTINGS (_COM_1200 ¦ _COM_CHR7 ¦ _COM_STOP1 ¦ _COM_NOPARITY)<br>
or<br>
#define SETTINGS (0x80 ¦ 0x02 ¦ 0x00 ¦ 0x00)<br>
<br>
Which bios call you use will determine how you set up the settings. (You will need to see your online documentation for better examples.)<br>
<br>
_bios_serialcom(_COM_INIT, COM1, SETTINGS);<br>
or<br>
bioscom(0, SETTINGS, COM1);<br>
<br>
For Windows 9X or NT, you cannot access the bios directly, so you have to go through something like a virtual driver.<br>
<br>
If your assignment is for the &quot;or DOS&quot; part, this will help, otherwise, you'll need more/better help than I just supplied.<br>

 
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. <br>
<br>
<br>
<br>
idComDev = OpenComm(&quot;COM1&quot;, 1024, 128);<br>
if (idComDev &lt; 0) {<br>
ShowError(idComDev, &quot;OpenComm&quot;);<br>
return 0;<br>
}<br>
<br>
err = BuildCommDCB(&quot;COM1:9600,n,8,1&quot;, &dcb);<br>
if (err &lt; 0) {<br>
ShowError(err, &quot;BuildCommDCB&quot;);<br>
return 0;<br>
}<br>
<br>
err = SetCommState(&dcb);<br>
if (err &lt; 0) {<br>
ShowError(err, &quot;SetCommState&quot;);<br>
return 0;<br>
}<br>
<br>
Let me know if you need more detailed info.<br>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top