RS485 Slave.txt

Warning: if you clip this code into your program, look out for possible html code slipping in.

REM   Simple slave program for testing RS485 networking with an SBC2000-074.
REM
REM   Copyright 1998 Vesta Technology, Inc.
REM   All rights reserved.


REM   This bit will be set when the COM port is idle, and clear when the
REM   COM port is busy.

GLOBAL XMTR_EMPTY AS BIT = 0x98,1


REM   Set up a subroutine to control the Push-To-Talk signal going to the RS485
REM   board. We are using pin 1 of the DIO connector, which is bit 4 of port D.

SUBROUTINE PTT(state AS INTEGER)
STATIC PTT_BIT AS BIT = 8,4
	PTT_BIT = state
END


REM   Declare the variables we need.

GLOBAL addr AS INTEGER, loop_cnt AS INTEGER
GLOBAL j AS INTEGER

REM   Declare a couple of constants.
REM
REM   NOTE:   The constant "my_addr" is used to determine the 'station number'
REM           of a particular board. If you are going to hook up more than one
REM           slave station on an RS485 network with this application, make
REM           certain that each one has a different value for this.

CONSTANT CR AS STRING = "\013"
CONSTANT my_addr AS INTEGER = 2


REM   By default, at powerup the I/O bits are configured as inputs, which means
REM   that they will be at a high level. This means that push-to-talk will be
REM   asserted. We have to release it in order to see any incoming messages.

PTT(0)


REM   Now for the main loop of the application. Wait for a transmission from the
REM   master station. See if it matches my address. If so, send a response
REM   consisting of the total number of messages I've seen (to all stations, not
REM   just myself.

DO WHILE 1
	PIPE PRINT COMM0
	loop_cnt = loop_cnt + 1
	INPUT addr
	IF addr = my_addr
		PTT(1)			: REM   Assert push-to-talk
		PRINT my_addr, CR	: REM   A response consists of the station
		PRINT loop_cnt, CR	: REM   ... number and a value.

		REM   A short loop to give the last character time to be completely
		REM   transmitted before releasing push-to-talk.
		DO
			REM
		LOOP UNTIL XMTR_EMPTY
		PTT(0)

		REM   Print some local status. Whenever we receive a message
		REM   meant for us, update a display with our station number
		REM   and our current total message count.

		PIPE PRINT LCD
		LCD_Command(0x80)
		PRINT my_addr
		LCD_Command(0x8A)
		PRINT loop_cnt
	ENDIF
LOOP