home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / pocketbk / developmen / oplexamp / S3SERIAL.TXT < prev    next >
Text File  |  1994-07-02  |  4KB  |  91 lines

  1. > Can any help with my problem ? Basically, I want to be able to read and
  2. > write individual bytes to and from the serial port with no handshaking
  3. > fromwithin OPL for the Series 3. I need the input to be buffered also.
  4. READING FROM THE SERIAL PORT ON AN S3 (CLASSIC)
  5. ===============================================
  6.  
  7. (This info works for the S3a too; however it is already included, in
  8. the S3a Programming manual, in a form slightly better for the S3a.)
  9.  
  10. Appendix C of the S3 Programming manual explains how to set up the port 
  11. (with an "rsset:" procedure) and then write to it.
  12.  
  13. Having used the rsset: procedure, though, you can read from the serial 
  14. port with something like this:
  15.     ret%=IOW(-1,1,ADDR(buf$)+1,len%)
  16.  
  17. However, you can also specify that one or more characters should be
  18. treated as terminating characters. (Without this, the read terminates
  19. only after reading exactly the number of bytes requested.)
  20.  
  21. To do this, add a final "term&" parameter onto the "rsset" declaration:
  22.     PROC rsset:(baud%,parity%,data%,stop%,hand%,term&)
  23. and add in this line before the IOW line in rsset:
  24.     POKEL ADDR(srchar%(5)),term&
  25.  
  26. When *writing* to the port, just pass 0 for term& - eg:
  27.   rsset:(8,0,8,1,0,&0)
  28.  
  29. When *reading*, the 32 bits of "term&" control whether the
  30. corresponding ASCII character, from 1 to 31, should terminate the
  31. read. For example, to terminate on Control-Z, <CR> or <LF>, set
  32. bits 26, 10 and 13. In binary, this is:
  33.     0000 0100 0000 0000 0010 0100 0000 0000
  34. Converting to a long integer gives &04002400, the value to use.
  35.  
  36. The terminating character itself is read into your buffer too,
  37. allowing your program to act differently according to its value.
  38.  
  39. This example assumes that each line is a maximum of 255 characters and is 
  40. terminated by a <CR>, and that Control-Z marks the end of the data. It 
  41. calls rsset to receive at 2400 Baud without handshaking.
  42.  
  43. PROC testread:
  44.   LOCAL ret%,pbuf%,buf$(255),end%,len%,pbf2%
  45.   PRINT "Test reading from serial port"
  46.   LOPEN "TTY:A"
  47.   LOADM "rsset"
  48.   REM this example receives at 2400 without h/shake
  49.   rsset:(11,0,8,1,0,&04002000) REM Control-Z or CR
  50.   pbuf%=ADDR(buf$)
  51.   IF pbuf%=$7FFF :pbf2%=$8000 :ELSE pbf2%=pbuf%+1 :ENDIF
  52.   DO
  53.     REM read max 255 bytes, after leading count byte
  54.     len%=255
  55.     ret%=IOW(-1,1,#pbf2%,len%)
  56.     POKEB pbuf%,len%
  57.     REM len% = length actually read, including terminator char
  58.     end%=LOC(buf$,CHR$(26)) REM non-zero for Control-Z
  59.     IF ret%<0 and ret%<>-43
  60.       BEEP 3,500
  61.       PRINT
  62.       PRINT "Serial read error: ";ERR$(ret%)
  63.     ENDIF
  64.     IF ret%<>-43         REM if received with terminator
  65.       POKEB pbuf%,len%-1 REM remove terminator
  66.       PRINT buf$         REM echo with CRLF
  67.     ELSE
  68.       PRINT buf$;        REM echo without CRLF
  69.     ENDIF
  70.   UNTIL end%
  71.   PRINT "End of session" :PAUSE -30 :KEY
  72. ENDP
  73.  
  74. Notes:
  75.     - passing -1 as the first argument to I/O keywords means that
  76. the LOPEN handle is to be used.
  77.     - OPL strings have a leading byte giving the length of the rest
  78. of the string, so the data is read beyond this byte. The byte is then
  79. poked to the length that was read.)
  80.     - The "IF pBuf%=$7FFF..." line, which is used instead of using
  81. #pBuf%+1 in the IOW line, protects against an "Overflow" error
  82. which you'd get if ADDR(buf$) was &7FFF. (The S3a has a "UADD"
  83. instruction - unsigned add - to get round this.)
  84.  
  85. Disclaimer: while I've made every effort to be accurate, and checked
  86. it works, I won't be held responsible for any damage caused. :-)
  87.  
  88. Nick H
  89. at Psion plc, London
  90.