home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / cserial / os2-seri.c
Encoding:
C/C++ Source or Header  |  1991-08-07  |  4.2 KB  |  242 lines

  1. X-NEWS: lu comp.os.os2.programmer: 525
  2. Path: luga!ucsvc.ucs.unimelb.edu.au!ariel!munnari.oz.au!samsung!dali.cs.montana.edu!rpi!usc!wupost!waikato.ac.nz!comp.vuw.ac.nz!am.dsir.govt.nz!marcamd!mercury!kcbbs!kc
  3. Newsgroups: comp.os.os2.programmer
  4. Subject: Problem With OS/2 Serial Line Program
  5. Message-ID: <1991Jul24.023603.12789@kcbbs.gen.nz>
  6. From: Chris_Sullivan@kcbbs.gen.nz (Chris Sullivan)
  7. Date: 24 Jul 91 02:36:03 GMT
  8. References: <1991Jul18.140638.24515@cse.uta.edu>
  9. Organisation: Kappa Crucis Unix BBS, Auckland, New Zealand
  10. Lines: 230
  11.  
  12. Here's a program that works.  Hope it's helpfull to you.
  13.  
  14.  
  15. /************************************************************************/
  16.  
  17. /*  TERM.CPP  -- from "PC Magazine" October 17, 1989 pg 285-295        */
  18.  
  19. /*  Chris Sullivan  5 June 1991                        */
  20.  
  21. /*  Zortech C++ 2.1                            */
  22.  
  23. /************************************************************************/
  24.  
  25.  
  26.  
  27. #define INCL_DOS
  28.  
  29. #define INCL_SUB
  30.  
  31. #define INCL_DOSDEVICES
  32.  
  33.  
  34.  
  35. #include <os2.h>
  36.  
  37. #include <stdio.h>
  38.  
  39. #include <stdlib.h>
  40.  
  41.  
  42.  
  43. #define SERIAL        0x01
  44.  
  45. #define SETBAUDRATE    0x41
  46.  
  47. #define SETLINECTRL    0x42
  48.  
  49. #define SETDCBINFO    0x53
  50.  
  51. #define GETDCBINFO    0x73
  52.  
  53.  
  54.  
  55. #define STACK_SIZE    2048
  56.  
  57. #define BPS        4800
  58.  
  59. #define KBD_HANDLE    0
  60.  
  61. #define VIO_HANDLE    0
  62.  
  63.  
  64.  
  65. struct {
  66.  
  67.   unsigned char dataBits;
  68.  
  69.   unsigned char parity;
  70.  
  71.   unsigned char stopBits;
  72.  
  73.   }  lineCtrl = { 8,        //8 data bits
  74.  
  75.           0,        //No parity
  76.  
  77.           0 };        //1 stop bits
  78.  
  79.  
  80.  
  81. struct {
  82.  
  83.   unsigned short usWriteTimeOut;
  84.  
  85.   unsigned short usReadTimeOut;
  86.  
  87.   unsigned char  bFlags1;
  88.  
  89.   unsigned char  bFlags2;
  90.  
  91.   unsigned char  bFlags3;
  92.  
  93.   unsigned char  bErrorReplacementChar;
  94.  
  95.   unsigned char  bBreakReplacementChar;
  96.  
  97.   unsigned char  bXONChar;
  98.  
  99.   unsigned char  bXOFFChar;
  100.  
  101.   } dcbInfo;
  102.  
  103.  
  104.  
  105. HFILE hCom;            //COM handle
  106.  
  107. unsigned char inBuffer[256];    //input buffer
  108.  
  109.  
  110.  
  111. void far ComThread(void);
  112.  
  113.  
  114.  
  115.  
  116.  
  117. /************************************************************************/
  118.  
  119. /*  main                                */
  120.  
  121. /************************************************************************/
  122.  
  123. void main(void) {
  124.  
  125.   USHORT        usAction, rc;
  126.  
  127.   USHORT        usBaudRate = BPS;
  128.  
  129.   unsigned        threadID;
  130.  
  131.   static char        comThdStk[STACK_SIZE];
  132.  
  133.   struct _KBDKEYINFO    kbdData;
  134.  
  135.   USHORT        usBytesWritten;
  136.  
  137.  
  138.  
  139. //Open and initialize COM1
  140.  
  141.   if (DosOpen((unsigned char far *)"COM1", &hCom, &usAction, 
  142.  
  143.     0L, 0, 1, 0x12, 0L)) {
  144.  
  145.     printf("COM1 not available or COM0x.SYS not loaded\n");
  146.  
  147.     exit(1);
  148.  
  149.     }
  150.  
  151.  
  152.  
  153. //Set data rate to 4800bps and line format to N81
  154.  
  155.   rc= DosDevIOCtl(0L, &usBaudRate, SETBAUDRATE, SERIAL, hCom);
  156.  
  157.   rc= DosDevIOCtl(0L, &lineCtrl,   SETLINECTRL, SERIAL, hCom);
  158.  
  159.  
  160.  
  161. //Set device control block parameters
  162.  
  163.   rc= DosDevIOCtl(&dcbInfo,    0L, GETDCBINFO, SERIAL, hCom);
  164.  
  165.   dcbInfo.usWriteTimeOut = 6000;        //60 second write timeout
  166.  
  167.   dcbInfo.usReadTimeOut  = 6000;        //60 second readtimeout
  168.  
  169.   dcbInfo.bFlags1 = 0x01;            //enable DTR
  170.  
  171.   dcbInfo.bFlags2 = 0x40;            //enable RTS
  172.  
  173.   dcbInfo.bFlags3 = 0x04;            //wait-for-something reads
  174.  
  175.   rc= DosDevIOCtl(0L, &dcbInfo,    SETDCBINFO, SERIAL, hCom);
  176.  
  177.  
  178.  
  179. //Create a thread to monitor the serial port
  180.  
  181.   rc= DosCreateThread((PFNTHREAD)ComThread, (PTID)&threadID,
  182.  
  183.     (PBYTE)comThdStk+STACK_SIZE);
  184.  
  185.  
  186.  
  187. //Monitor the keyboard and output typed characters
  188.  
  189.   do {
  190.  
  191.     KbdCharIn(&kbdData, IO_WAIT, KBD_HANDLE);
  192.  
  193.     if ((kbdData.chChar != 0) && (kbdData.chChar != 0x1B)) {
  194.  
  195.       VioWrtTTY(&kbdData.chChar, 1, VIO_HANDLE);
  196.  
  197.       DosWrite(hCom, &kbdData.chChar, 1, &usBytesWritten);
  198.  
  199.       }
  200.  
  201.     } while (kbdData.chChar != 0x1B);    //stop on ESCAPE character
  202.  
  203.  
  204.  
  205.   DosExit(EXIT_PROCESS,0);
  206.  
  207. }
  208.  
  209.  
  210.  
  211.  
  212.  
  213. /************************************************************************/
  214.  
  215. /*  Thread to read characters from COM1 and write to screen        */
  216.  
  217. /************************************************************************/
  218.  
  219. #pragma check_stack(off)    //disable stack checking
  220.  
  221.  
  222.  
  223. void far ComThread(void) {  
  224.  
  225.   unsigned short usBytesRead, i;
  226.  
  227.  
  228.  
  229.   while (!DosRead(hCom, inBuffer, 256, &usBytesRead))
  230.  
  231.     if (usBytesRead) {
  232.  
  233.       for (i=0; i<usBytesRead; i++)  inBuffer[i] &= 0x7f;
  234.  
  235.       VioWrtTTY(inBuffer, usBytesRead, VIO_HANDLE);
  236.  
  237.       }
  238.  
  239.   DosExit(EXIT_THREAD, 1);
  240.  
  241. }
  242.