CHAPTER
7
SOFTWARE PACKAGE It is easy to achieve
communications under MS/DOS by setting the RS422/RS485 board to COM1:
and COM2:. The standard COM1: and COM2: communication statements are
supported by most high level language such as: BASIC, PASCAL, C, ...etc.
In the following example, we will introduce users how to write an application program. In section 7.1, wwe will specify how to write high level language when the board is set to standard COM1: and COM:. Suppose your setting is not standard COM ports, the section 7.2 introduces the user how to write a driver. Section 7.3 provides how to use a SERIAL DRIVER to drive the RS422/RS485 board. The PCCOM software is a powerful package for serial communication, we will introduce it in section 7.4. 7.1 Use Standard COM Ports Since BASIC language provides a buffer for communications, in the follows, we will demonstrate how to program under BASIC language. To start the communication task under BASIC, the statement OPEN "COMn:speed, parity, ....... " AS ID is used, then you can use the file ID to send or receive data from communication port. In the following, we list send and receive programs which are written in the BASIC language. 1. Loopback Test (Basic Version) 5 REM OPEN LOGICAL
DEVICE COM1
10 OPEN "O",#1,"COM1" 25 CNT+0 30 FOR I+&H31 TO &H80 40 CNT=CNT+1 50 A$=CHR$( I ) 60 PRINT #1,A$; 100 NEXT I 110 PRINT CNT 180 CLOSE #1 190 FOR I = 1 TO 1000 :NEXT I 200 OPEN " I ",#2,"COM1" 230 FOR I= &H31 TO &H80 250 A$=CHR$( I ) 271 B$=INPUT$(1,#2) 275 PRINT "DATA";A$, B$ 280 IF A$ <> B$ THEN PRINT "ERROR" 290 IF A$ <> B$ THEN GOTO 310 300 NEXT 310 CLOSE #2 320 END 2.File Transfer (Basic Version) 10 REM
20 REM SERIAL DRIVER DEMO PROGRAM FOR BASIC LANGUAGE 30 REM 40 REM THIS PROGRAM WILL TRASMIT MESSAGES TO 50 REM REMOTE SITE THROUGH DEVICE DRIVER 60 READ A$ 70 OPEN "O", #1,A$ 80 FOR I = 1 TO 10 90 READ M$ 100 PRINT #1,"LINE";I,M$ 110 NEXT I 120 PRINT #1,"@" 130 CLOSE #1 140 PRINT "DATA TRANSMITTED" 150 END 155 REM THE DRVICE NAME IS COM1 160 DATA "COM1" 170 DATA "SERIAL DRIVER DEMO PROGRAM FOR BASIC LANGUAGE" 180 DATA "THIS MESSAGE IS RECIVED FROM REMOTE SITE" 190 DATA "THIS IS LINE #3" 200 DATA "TRANSMIT OK" 210 DATA "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 220 DATA "abcdefghijklmnopqrstuvwxyz" 230 DATA "THIS DEVICE DRIVER IS VERY EASY TO USE" 240 DATA "SEE REFERENCE MANUAL FOR MORE INFORMATION" 250 DATA "COPYRIGHT BY DECISION-COMPUTER" 260 DATA "GOOD LUCK !" 10 REM 20 REM SERIAL DRIVER DEMO PROGRAM FOR BASIC LANGUAGE 30 REM 40 REM THIS PROGRAM WILL RECEIVE MESSAGES 50 REM FROM REMOTE-SITE COMPUTER 60 READ A$ 70 OPEN " I ",#1,A$ 80 INPUT #1,M$ 90 IF M$ = "@" THEN 120 100 PRINT M$ 110 GOTO 80 120 CLOSE #1 130 PRINT "DATA RECEIVED" 140 END 145 REM THE DEVICE NAME IS COM1 150 DATA "COM1" 7.2 Write Your Own Driver If you are going to set the port address thing other than standard COM ports, you must write a driver by yourself or use PCCOM software 9see section 7.4), SERIAL DRIVER package (see section 7.3). In the following, we will demonstrate how to write a driver by yourself. To write your own driver, the most important things are driver and receiver mode selection. When the adapter is set to always enable mode, the communication protocol is RS422. (Please refer the data book for 8250 chip to programming 8250 registers under RS422 mode) When you select the RS485 mode, you may enable the driver for RTS or control register. We assume BASE=1A0, then 1. Enable RTS Turn on
driver
OUT BASE+4,3
Turn off driver OUT BASE+4,0 2. Enable control register Turn on
driver
OUT BASE+7,1
Turn off driver OUT BASE+7,0 The control register can be used to enable receiver. Turn on
receiver
OUT BASE+7,2
Turn off receiver OUT BASE+7,0 A statement such as OUT BASE+7,3 will turn on both driver and receiver. The PASCAL program listed below can be used to test the send and receiver functions. uses crt ; const p1 = $3f8 ; p2 = $2f8 ; var delaytime : integer ; procedure init(p,n : integer) ; begin port[p+3] := $80 port[p ] := Lo(n) port[p+1] := Hi(n) port[p+3] := 3 ; end ; procedurd test ; const baud1 : array[1..3] of integer = ( 96, 12, 2) ; baud2 : array[1..3] of word = (1200,9600,57600) ; var tx,rx dir stat, testloop,baudindex,i : integer ; b1,b2 : byte ; c : char ; begin init (p1,96) init (p2,96) port[p2+4] := 3; { /RTS = Low } port[p2+7] := 0; port[p1+7] := 2; {Enable RX } port[p1+4] :=0; for i := 1 to 20 do begin repeat b1 := random(256) until bq <> b2; port[p2] := b1; delay(delaytime); b2 := port[p1]; writeln(b1:4,b2:12); end; c := readkey; end; begin {main} clrscr; randomize; delaytime := 100; write('Enter delay time (1..1000) (ms) : '); readln(delaytime); test; writeln('Done.......'); end. 7.3 Under Serial Driver The SERIAL DRIVER can be used to drive your RS422 or RS485 port. To drive RS485 or RS422 you need not give the interrupt vector address and active status, because the RS422/RS485 board does not support an interrupt vector. It is easily to control the RS422 mode under the SERIAL DRIVER. However, if you set the RS485 mode, yo must take over the RTS signal or control register. The MCR (byte 1) of I/O control can be used to set RTS to enable the driver (see SERIAL DRIVER chapter 6). To enable the driver by the control register, please use OUT BASE+7 statement. 7.4 Under PCCOM Software Package The PCCOM software can be used to drive your RS422, you need not give interrupt vector address and active status, because the RS422/RS485 board does not support an interrupt vector. It is easy to control the RS422 mode under PCCOM. However, if you set the RS485 mode, you must take over the RTS signal or control register. The OUT BASE+4 statement can be used to set RTS to enable the driver. To enable the driver by the control register, please use OUT BASE+7 statement. |