home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / COMM.LIB < prev    next >
Text File  |  1994-03-08  |  3KB  |  81 lines

  1. // Comm.lib - Serial communications library
  2. // ver.1
  3. //
  4. //**** CommOpen(): Open Comm port
  5. // SYNTAX: int CommOpen(string CommPortName,int Error)
  6. // WHERE: CommPortName: port name, such as "COM1" or "COM2"
  7. //        Error: Set errorcode if returning 0
  8. // RETURN: Handle for this comm port.  NULL if could not open
  9. // NOTES: You should be sure to close com port when finished
  10. //
  11. //
  12. //**** CommClose(): Close Comm port
  13. // SYNTAX: void CommClose(int CommHandle)
  14. // WHERE: CommHandle: value returned from previous call to CommOpen
  15. //
  16. //
  17. //**** CommWrite(): Write to comm port
  18. // SYNTAX: int CommWrite(int CommHandle,byte[] BufferArea,int BufferLength,int BytesWritten)
  19. // WHERE: CommHandle: value returned from previous call to CommOpen
  20. //        BufferArea: data bytes to write
  21. //        BufferLength: How many bytes to write
  22. //        BytesWritten: Set to how many bytes were written
  23. // RETURN: 0 for success, else error code
  24. //
  25. //
  26. //**** CommRead(): Read from comm port
  27. // SYNTAX int CommRead(int CommHandle,byte[] BufferArea,int MaxBufferLength,int BytesRead)
  28. // WHERE: CommHandle: value returned from previous call to CommOpen
  29. //        BufferArea: data bytes to read to (created if necessary)
  30. //        MaxBufferLength: Maximum bytes to read
  31. //        BytesRead: Set to how many bytes were read
  32. // RETURN: 0 for success, else error code
  33. //
  34. //
  35.  
  36. #include <FileIO.lib>
  37. #include <DevIOctl.lib>
  38.  
  39. /*****************************************************************
  40.  *********        END OF DESCRIPTION FOR COMM.LIB        *********
  41.  *****************************************************************/
  42.  
  43. #define  COMM_IOCTL_CATEGORY  1
  44.  
  45. CommOpen(CommPortName,Error)
  46. {
  47.    Error = DosOpen(CommPortName,_RetCommFileHandle,_ActionTaken,0,FILE_NORMAL,
  48.                    OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW,
  49.                    OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYREADWRITE,NULL );
  50.    if ( Error )
  51.       _RetCommFileHandle = 0;
  52.    return _RetCommFileHandle;
  53. }
  54.  
  55. CommClose(CommHandle)
  56. {
  57.    DosClose(CommHandle);
  58. }
  59.  
  60. CommRead(CommHandle,BufferArea,BufferLength,BytesRead)
  61. {
  62.    return DosRead(CommHandle,BufferArea,
  63.                   min(QueryCharsInReceiveQueue(CommHandle),BufferLength),
  64.                   BytesRead);
  65. }
  66.  
  67. CommWrite(FileHandle,BufferArea,BufferLength,BytesWritten)
  68. {
  69.    return DosWrite(FileHandle,BufferArea,BufferLength,BytesWritten);
  70. }
  71.  
  72. QueryCharsInReceiveQueue(CommHandle)
  73. {
  74.    #define QUERY_RCV_FUNCTION 0x68
  75.    _retSize = 0;
  76.    _rc = DosDevIOCTL(CommHandle,COMM_IOCTL_CATEGORY,QUERY_RCV_FUNCTION,
  77.                      NULL,0,0,_data,4,_retSize);
  78.    return ( 0 == _rc  &&  4 == _retSize ) ? BLObGet(_data,0,UWORD16) : 0 ;
  79. }
  80.  
  81.