CHAPTER 7
SOFTWARE PROGRAMMING


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, ¡K etc.

In the following example, we will introduce user how to write an application program. In section 7.1, we will specify how to write high level language when the board is set to standard COM1: and COM2:, Suppose your setting is not standard COM ports, the section 7.2 introduces the user how to write a driver. The PCCOM software is a powerful package for serial communication, we will introduce it in section 7.3.
 

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,¡K..¡¦ 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 $ (I)
275     PRINT ¡§DATA¡¨ ; A$, B$
280     IF A$ <> B$ THEN PRINT ¡§ERROR¡¨
290    IF A$ <> B$ THEN GOTTO 310
300 NEXT
310 CLOSE
320 END

2.    File Transfer ( Basic Version)

10    REM
20    REM SERIAL DRIVER DEMO PROGRAM FOR BASIC
21    REM LANGUAGE
30    REM
40    REM THIS PROGRAM WILL TRANSMIT 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 DEVICE NAME IS COM1
160    DATA ¡§COM1¡¨
170    DATA ¡§SERIAL DRIVER DEMO PROGRAM FOR BASIC LANGUAGE¡¨
180    DATA ¡§ THIS MESSAGE IS RECEIVED 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 EASY TO USE¡¨
240    DATA ¡§ SEE REFERENCE MANUAL FOR MORE INFORMATION¡¨
250    DATA ¡§ COPYRIGHT BY DECISION-COMNPUTER¡¨
260    DATA ¡§GOOD LUCK !¡¨
******************************************************

10    REM
20    REM SERIAL DRIVER DEMO PROGRAM FOR BASIC
21    REM LANGUAGE
30    REM
40    REM THIS PROGRAM WILL RECEIVE  MESSAGES
50    REM 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

When the adapter is set to always enable mode, the communication protocol is RS422. Please refer the data book for UART chip to programming UART registers under RS422 mode.

The PCCOM software provides device driver for different operating systems under RS422 mode, please refer APPENDIX.

To write your own driver for RS485 mode, the most important things are driver and receiver mode selection. In the following we will demonstrate how to write a driver by yourself.

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.

Program RS_485_test_PROGRAM;

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;

procedure  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 b1 <> 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¡K1000) (ms) : ¡¥ ) ;
readln(delaytime);
test;
writeln(¡¥Done¡K¡K.¡¦);
end;


7.3 PCCOM Software Package

The PCCOM software can be used to drive your RS422 or  RS485 port, to drive RS485 or 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.