home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / developmen / oplman / 3APROG.ZIP / BC_PRINT.TXT < prev    next >
Text File  |  1994-09-06  |  9KB  |  295 lines

  1.  
  2.  
  3.  
  4. Serial/parallel ports and printing
  5.  
  6.  
  7.  
  8. You can use LPRINT in an OPL program to send information (for 
  9. printing or otherwise) to any of these devices:
  10.  
  11. *) A parallel port  "PAR:A"
  12.  
  13. *) A serial port  "TTY:A"
  14.  
  15. *) A file on the Series 3a or an attached computer.
  16.  
  17. You can also read information from the serial port.
  18.  
  19. OPL does not provide access to the advanced page formatting and font 
  20. control features of the Series 3a.
  21.  
  22. Using the parallel port
  23.  
  24. In your OPL program, set up the port with the statement "LOPEN "PAR:A"". 
  25.  
  26.  
  27. Provided the port is not already in use, the connection is now ready. 
  28. LPRINT will send information down the parallel 3 Link lead  
  29. for example, to an attached printer.
  30.  
  31. *** Example
  32.  
  33. PROC prints:
  34.   OPEN "clients",A,a$
  35.   LOPEN "PAR:A"
  36.   PRINT "Printing..."
  37.   DO
  38.     IF LEN(A.a$)
  39.       LPRINT A.a$
  40.     ENDIF
  41.     NEXT
  42.   UNTIL EOF
  43.   LPRINT CHR$(12); :LCLOSE
  44.   PRINT "Finished" :GET
  45. ENDP
  46.  
  47.  
  48.  
  49. Using the serial port
  50.  
  51. In your OPL program, set up the port with the statement "LOPEN "TTY:A"".
  52.  
  53. Now LPRINT should send information down the serial 3 Link 
  54. lead  for example, to an attached printer. If it does not, 
  55. the serial port settings are not correct.
  56.  
  57. *** Serial port settings
  58.  
  59. "LOPEN "TTY:A"" opens the serial port with the following default 
  60. characteristics: 
  61.  
  62. 9600 baud
  63. no parity
  64. 8 data bits
  65. 1 stop bit
  66. RTS handshaking.
  67.  
  68. *) If your printer (or other device) does match these 
  69. characteristics, the LOPEN statement sets the port up correctly, 
  70. and subsequent LPRINT statements will print there successfully.
  71.  
  72. *) If your printer does not match these characteristics, 
  73. you must use a procedure like the one listed below to change the characteristics 
  74. of the serial port, before LPRINTs will print successfully 
  75. to your printer.
  76.  
  77. Printers very often use DSR (DSR/DTR) handshaking, and you may need 
  78. to set the port to use this.
  79.  
  80. *** Setting the serial port characteristics 
  81.  
  82. Calling the procedure
  83. The "rsset:" procedure listed below provides a convenient way 
  84. to set up the serial port.
  85.  
  86. Each time you use an  "LOPEN "TTY: ""  statement, follow 
  87. it with a call to the "rsset:" procedure. Otherwise  "the"  
  88. LOPEN will use the default characteristics.
  89.  
  90. Passing values to the procedure
  91. Pass the procedure the values for the five port characteristics, like 
  92. this:
  93.  
  94. rsset:(baud%,parity%,data%,stop%,hand%,&0)
  95.  
  96. The final parameter, which should be "&0" here, is only 
  97. used when reading from the port.
  98.  
  99. To find the value you need for each characteristic, use the tables 
  100. below. You must give values to all five parameters, in the 
  101. correct order.
  102.  
  103. Baud =            50   75   110  134  150  300  600  1200
  104. value =            1    2    3    4    5    6    7    8
  105.  
  106.                     1800 2000 2400 3600 4800 7200 9600 19200
  107.                     9    10   11   12   13   14   15   16
  108.  
  109. Parity =        NONE EVEN ODD
  110. value =         0    1    2
  111.  
  112. Data bits =    5, 6, 7 or 8
  113.  
  114. Stop bits =    2 or 1
  115.  
  116. Handshaking =        ALL  NONE XON  RTS  XON+RTS
  117. value =                11   4    7    0    3
  118.  
  119.                         DSR  XON+DSR   RTS+DSR
  120.                         12   15        8
  121.  
  122. The "rsset:" procedure:
  123.  
  124. PROC rsset:(baud%,parity%,data%,stop%,hand%,term&)
  125.   LOCAL frame%,srchar%(6),dummy%,err%
  126.   frame%=data%-5
  127.   IF stop%=2 :frame%=frame% OR 16 :ENDIF
  128.   IF parity% :frame%=frame% OR 32 :ENDIF
  129.   srchar%(1)=baud% OR (baud%*256)
  130.   srchar%(2)=frame% OR (parity%*256)
  131.   srchar%(3)=(hand% AND 255) OR $1100
  132.   srchar%(4)=$13
  133.   POKEL ADDR(srchar%(5)),term&
  134.   err%=IOW(-1,7,srchar%(1),dummy%)
  135.   IF err% :RAISE err% :ENDIF
  136. ENDP
  137.  
  138. Take care to type this program in exactly as it appears here.  
  139.  
  140. Example of calling the procedure
  141.  
  142. PROC test:
  143.   PRINT "Testing port settings"
  144.   LOPEN "TTY:A"
  145.   LOADM "rsset"
  146.   rsset:(8,0,8,1,0,&0)
  147.   LPRINT "Port OK" :LPRINT
  148.   PRINT "Finished" :GET
  149.   LCLOSE
  150. ENDP
  151.  
  152. "rsset:(8,0,8,1,0,&0)"  sets 1200 Baud, no parity, 8 data bits, 
  153. 1 stop bit, and RTS/CTS handshaking.
  154.  
  155. Advanced use
  156. The section of the "rsset:" procedure which actually sets the 
  157. port is this:
  158.  
  159. srchar%(1)=baud% OR (baud%*256)
  160. srchar%(2)=frame% OR (parity%*256)
  161. srchar%(3)=(hand% AND 255) OR $1100
  162. srchar%(4)=$13
  163. POKEL ADDR(srchar%(5)),term&
  164. err%=IOW(-1,7,srchar%(1),dummy%)
  165. IF err% :RAISE err% :ENDIF
  166.  
  167. The elements of the array "srchar%" contain the values specifying 
  168. the port characteristics. If you want to write a shorter procedure, 
  169. you could work out what these values need to be for a particular setup 
  170. you want, assign these values to the elements of the array, and then 
  171. use the IOW function (followed by the error check) exactly 
  172. as above.
  173.  
  174. *** Reading from the serial port
  175.  
  176. If you need to read from the serial port, you must also pass a parameter 
  177. specifying terminating mask for the read function. If  "term&"  
  178. is not supplied, the read operation terminates only after reading 
  179. exactly the number of bytes requested. In practice, however, you may 
  180. not know exactly how many bytes to expect and you would therefore 
  181. request a large maximum number of bytes. If the sender sends less 
  182. than this number of bytes altogether, the read will never complete.
  183.  
  184. The extra parameter,  "term&" , allows you to specify that one 
  185. or more characters should be treated as terminating characters. The 
  186. terminating character itself is read into your buffer too allowing 
  187. your program to act differently depending on its value.
  188.  
  189. The 32 bits of  "term&"  each represent the corresponding ASCII 
  190. character that should terminate the read. This allows any of the ASCII 
  191. charcaters 1 to 31 to terminate the read.
  192.  
  193. For example, to terminate the read when Control-Z (ie. ASCII 26) is 
  194. received, set bit 26 of  "term&". To terminate on Control-Z or 
  195.  "<CR>"  or  "<LF>"   which allows text to be read 
  196. a line at a time or until end of file  set the bits 26, 10 
  197. and 13. In binary, this is:
  198.  
  199.        0000 0100 0000 0000 0010 0100 0000 0000
  200.  
  201. Converting to a long integer gives &04002400 and this is the value 
  202. to be passed in  "term&"  for this case.
  203.  
  204. Clearly  "term&"  cannot be used for binary data which 
  205. may include a terminating character by chance. You can sometimes get 
  206. around this problem by using  "term&"  and having the sender 
  207. transmit a leading non-binary header specifying the exact number of 
  208. full-binary data following. You could then reset the serial characteristics 
  209. not to use  "term&" , read the binary data, and so forth.
  210.  
  211. Example reading from serial port
  212.  
  213. This example assumes that each line sent has maximum length 255 characters 
  214. and is terminated by a  "<CR>"  and that Control-Z signals the 
  215. end of all the data.
  216.  
  217. PROC testread:
  218.   LOCAL ret%,pbuf%,buf$(255),end%,len%
  219.   PRINT "Test reading from serial port"
  220.   LOPEN "TTY:A"
  221.   LOADM "rsset"
  222.   REM receive at 2400 without h/shake
  223.   rsset:(11,0,8,1,0,&04002000) REM Control-Z or CR
  224.   pBuf%=ADDR(buf$)
  225.   DO
  226.     REM read max 255 bytes, after leading count byte
  227.     len%=255
  228.     ret%=IOW(-1,1,#UADD(pbuf%,1),len%)
  229.     POKEB pbuf%,len%   REM len% = length actually read
  230.                        REM including terminator char
  231.     end%=LOC(buf$,CHR$(26)) REM non-zero for Control-Z
  232.     IF ret%<0 and ret%<>-43
  233.       BEEP 3,500
  234.       PRINT
  235.       PRINT "Serial read error: ";ERR$(ret%)
  236.     ENDIF
  237.     IF ret%<>-43       REM if received with terminator
  238.       POKEB pbuf%,len%-1 REM remove terminator
  239.       PRINT buf$         REM echo with CRLF
  240.     ELSE
  241.       PRINT buf$;        REM echo without CRLF
  242.     ENDIF
  243.   UNTIL end%
  244.   PRINT "End of session" :PAUSE -30 :KEY
  245. ENDP
  246.  
  247.  
  248. Note that passing -1 as the first argument to I/O keywords 
  249. means that the LOPEN handle is to be used. Also, OPL 
  250. strings have a leading byte giving the length of the rest of the string, 
  251. so the data is read beyond this byte. The byte is then 
  252. poked to the length which was read.
  253.  
  254. Printing to a file
  255.  
  256. *** Printing to a file on a PC or Apple Macintosh
  257.  
  258. As if you were going to transfer a file:
  259.  
  260. *) Physically connect the Series 3a and the other computer.
  261.  
  262. *) Select the `Remote link' option in the System screen and 
  263. press Enter.
  264.  
  265. *) Run the server program (supplied with 3 Link) on the other 
  266. computer.
  267.  
  268. In your OPL program, specify the dest