Features
Submit News Item
SAMBA
Setting up a Mixed Network
eCS newsgroups
Benchmarking
NetBeans Editor
Logo Programming
more...


Membership
BayWarp is sponsored by Blonde Guy
Administrivia
E-Mail Lists
Submissions
Contact BayWarp
Credits


Serial Port Programming

In a couple of our recent meetings at BayWarp, we've been discussing serial port programming on OS/2. This page captures some of our discussions.

The first step is to see if you have a serial port, and what it's characteristics are. At a command prompt, type:

Mode COM1
and look at the output.

The code to open the com port, read and write, and close the com port is quite similar to the code for doing the same to a file.

#define COM1_PORT "COM1"
HFILE COM1_handle;
ULONG rc, actn, Com1Length, BytesWritten;
UCHAR Com1Char, temp_str[128];

rc = DosOpen (COM1_PORT, &COM1_handle, &actn, 0L, 0, 0x01, 0x42, 0L);

rc = DosRead(COM1_handle, &Com1Char, 1, &Com1Length);

rc = DosWrite(COM1_handle, temp_str, strlen(temp_str), &BytesWritten);

Setting up the serial port is more complex. There is good documentation in the OS/2 toolkit.


ULONG	ret;
ULONG	actn;
ULONG   baud_rate;
ULONG	parm_len;
ULONG	data_len;


EXTERN struct {
   BYTE data_bits;
   BYTE parity;
   BYTE stop_bits;
} line_ctrl;

EXTERN struct {
   BYTE ON_mask;
   BYTE OFF_mask;
} modem_ctrl;


EXTERN struct {
   USHORT write_timeout;
   USHORT read_timeout;
   BYTE   flag_1;
   BYTE   flag_2;
   BYTE   flag_3;
   BYTE   err_char;
   BYTE   brk_char;
   BYTE   xon_char;
   BYTE   xoff_char;
} DCB;

      /*  set up communications mode - 9600 baud, 1 start bit, 1 stop bit  */
      /*  8 data bits and odd parity                                       */
      baud_rate = 9600;
      parm_len = (ULONG)sizeof( baud_rate );
      DosDevIOCtl (COM1_handle,	                /* HFILE	*/
       		   0x0001,      		/* Category	*/
                   0x0041,      		/* Function	*/
                   &baud_rate, 			/* ParmList	*/
                   parm_len,    		/* Len of ParmList	*/
                   &parm_len, 			/* Addr of Len of ParmList */
    		   0L, 0L, 0L);			/* Data, Len, Addr of Len of Data */
      line_ctrl.data_bits = 0x08;   /*  8 data bits */
      line_ctrl.parity    = 0x01;   /*  odd parity  */
      line_ctrl.stop_bits = 0x00;   /*  1 stop bit  */
      parm_len = (ULONG)sizeof( line_ctrl );
      DosDevIOCtl (COM1_handle,		        /* HFILE	*/
        	   0x0001,			/* Category	*/
                   0x0042,  			/* Function	*/
                   &line_ctrl, 			/* ParmList	*/
                   parm_len,			/* Len of ParmList	*/
                   &parm_len, 			/* Addr of Len of ParmList */
    		   0L, 0L, 0L);			/* Data, Len, Addr of Len of Data */

      DCB.write_timeout = 100;
      DCB.read_timeout = 100;
      DCB.flag_1 = 0;			/* changed to a 1 */
      DCB.flag_2 = 0;
      DCB.flag_3 = 3;          /* normal read timeout process  */
      DCB.err_char = 0;
      DCB.brk_char = 0;
      DCB.xon_char = 0;
      DCB.xoff_char = 0;
      parm_len = (ULONG)sizeof( DCB );
      DosDevIOCtl (COM1_handle,		        /* HFILE	*/
       		   0x0001,			/* Category	*/
                   0x0053,  			/* Function	*/
                   &DCB, 			/* ParmList	*/
                   parm_len,			/* Len of ParmList	*/
                   &parm_len, 			/* Addr of Len of ParmList */
    		   0L, 0L, 0L);			/* Data, Len, Addr of Len of Data */

 	modem_ctrl.ON_mask = 0x01;
 	modem_ctrl.OFF_mask = 0xFF;
	parm_len = (ULONG)sizeof( modem_ctrl );
	data_len = (ULONG)sizeof( ret );
        DosDevIOCtl (COM1_handle,		/* HFILE	*/
    		     0x0001,			/* Category	*/
                     0x0046,  			/* Function	*/
                     &modem_ctrl, 		/* ParmList	*/
                     parm_len,			/* Len of ParmList	*/
                     &parm_len, 		/* Addr of Len of ParmList */
    	             &ret, 			/* Data*/
    		     data_len, 			/* Len of Data */
    		     &data_len);		/* Addr of Len of Data */
   }

Here is how to check for a character on the serial port.

        
USHORT COM1Poll()
{
    BYTE com1_input;
    ULONG data_len;
   
    data_len = (ULONG)sizeof( com1_input );
    DosDevIOCtl (COM1_handle,		        /* HFILE	*/
    	         0x0001,		        /* Category	*/
                 0x0067,  			/* Function	*/
                 0L, 				/* ParmList	*/
                 0L,				/* Len of ParmList	*/
                 0L, 				/* Addr of Len of ParmList */
    	         &com1_input, 			/* Data*/
    		 data_len, 			/* Len of Data */
    		 &data_len);			/* Addr of Len of Data */
   return com1_input;
}

BayWarp
The San Francisco Bay Area OS/2 User Group