home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / VISUAL_B / FERRAMEN / VB_COMM / VBCOMM.TXT < prev    next >
Encoding:
Text File  |  1992-04-01  |  4.3 KB  |  118 lines

  1.  
  2.       Communications Control for Visual Basic by Mark Gamber   March 1992
  3.  
  4.    This control provides access to the communications ports through the Windows
  5. API. To use the control, copy VBCOMM.VBX to your Visual Basic directory. Select
  6. the file from the listbox displayed by selecting "Add File..." in the "File"
  7. menu. A "port" button should appear in the toolbox. Selecting a port and
  8. placing it on a form will display another small "port" button. This is only
  9. displayed during design mode and should be treated like a timer control.
  10.  
  11.  
  12. Port settings:
  13.    These settings define the port setup. They may be set at design time or run
  14. time to any value desired within the ranges described:
  15.  
  16.    Comport: 0 = COM1:, 1 = COM2:, 2 = COM3:, 3 = COM4:
  17.  
  18.    Baud:    0 = 300, 1 = 1200, 2 = 2400, 3 = 9600, 4 = 19200
  19.  
  20.    Parity:  0 = Parity Off, 1 = Parity Even, 2 = Parity Odd
  21.  
  22.    Data:    0 = 6 bits, 1 = 7 bits, 2 = 8 bits
  23.  
  24.    Stop:    0 = 1 bit, 1 = 2 bits
  25.  
  26.  
  27.  
  28. Port initialization:
  29.    Before using the port, it must be enabled. Use "Comm1.Enable = 1" to enable
  30. the port and "Comm1.Enable = 0" to disable (close) the port. Attempts to enable
  31. a port already enabled results in error #10001. Bad or unsupported port setup
  32. parameters result in either error #10001 or #10002. If an attempt is made to
  33. disable an unenabled port, error #10004 will occur. All these errors may be
  34. trapped by the Visual Basic program.
  35.  
  36.  
  37.  
  38. Data I/O:
  39.    Sending data involves sending integer values of ASCII codes one at a time
  40. using "DataChr" or by strings using "DataStr". An example of each:
  41.  
  42. Single character:
  43.  
  44.    Comm1.DataChr = asc( "A" )            ' Send letter "A" out the port
  45.  
  46.  
  47. String:
  48.  
  49.    Comm1.DataStr = "ATZ" + chr$( 13 )    ' Send "ATZ" command to modem
  50.  
  51.  
  52.    Receiving data can be a bit trickier. Internally, a timer is used to poll
  53. the port for any characters and, if one or more are received, it calls the
  54. "InQueue" event. The Visual Basic programmer may use the event to read the data
  55. into a string or character by character. As long as there is data to be read
  56. from the port input buffer, this event is called every time the timer goes off.
  57. An example of "InQueue" code might be:
  58.  
  59. "InQueue as Integer"
  60.  
  61.    if InQueue Then                ' If not a false firing...
  62.       a$ = Comm1.DataStr          ' Read data into string
  63.       if len(a$) Then             ' If something was read...
  64.          Picture1,Print a$        ' Do something useful
  65.          while len( a$ )
  66.             a$ = Comm1.DataStr    ' While data is being read...
  67.             if len( a$ ) then
  68.                Picture1.Print a$  ' Be more useful
  69.             end if
  70.          wend
  71.       end if
  72.    end if
  73.  
  74. End of "InQueue"
  75.  
  76. Following the code, InQueue is the number of characters waiting to be read. In
  77. some chance the routine is called with nothing waiting, this should be checked.
  78. Next, we read the port buffer and again check to be sure something was actually
  79. read. If so, do something useful and, as long as data is waiting to be read
  80. from the port, repeat the process. If using very high speeds on relatively slow
  81. machines, the "While" section may cause the program to appear to lock up since
  82. there's always data to receive. Omitting that section is less efficient but
  83. doesn't attempt to clear the port before exiting.
  84.    Data may be received in two ways. Incoming data may be read character by
  85. character using "DataChr" and as a string using "DataStr". An example of each:
  86.  
  87. Single character:
  88.  
  89.    a% = Comm1.DataChr             ' Read character from port
  90.    if a% <> -1 then               ' -1 means nothing was there
  91.       do something useful         ' Otherwise, valid ASCII code
  92.    end if
  93.  
  94.  
  95. String:
  96.    a$ = Comm1.DataStr             ' Read string into a$
  97.    if len( a$ ) then              ' If something was actually read...
  98.       do something fun            ' See code
  99.    endif
  100.  
  101.  
  102. Attempting to read or write an unenabled port results in an error 10004.
  103.  
  104.  
  105.  
  106. This software is free for all to use given two conditions:
  107.  
  108. 1. Ownership is retained by Mark Gamber as of March, 1992
  109.    (Give credit where credit is due)
  110.  
  111. 2. I'm not liable.
  112.    (What? Me Worry?)
  113.  
  114. Bugs, suggestions and whatnot may be directed to PCA MarkG on America Online or
  115. through Internet to pcamarkg@aol.com, E-Mail only. No other messages or mail
  116. may be responded to.
  117.  
  118.