Moog Animatics SmartMotor™ Developer's Guide,Rev. L
Page 105 of 909
empty, GETCHR will return a value of (negative 1.) If GETCHR is assigned to a byte ab[], then
the value gets cast from the range -1 to +255 to the signed range -128 to +127. This causes -
1 (empty buffer) to have the same value as char 255, since 255 gets cast to -1. It is
recommended you assign GETCHR to a word or long to perform comparisons.
The LEN variable holds the number of characters in the buffer. A program must see that the
LEN is greater than zero before issuing a command like a=GETCHR. Likewise, it is necessary
to arrange the application so that, overall, data will be pulled out of the buffer as fast as it
comes in.
The ability to configure the communication ports for any protocol as well as to both transmit
and receive data allows the SmartMotor to interface with a vast array of RS-232 and RS-485
devices. Some of the typical devices that would interface with SmartMotors over the
communication interface are:
l
Other SmartMotors
l
Bar Code Readers
l
Light Curtains
l
Terminals
l
Printers
The following is an example program that repeatedly transmits a message to an external
device (in this case another SmartMotor) and then takes a number back from the device as a
series of ASCII letter digits, each ranging from 0 to 9. A carriage return character marks the
end of the received data. The program uses that data as a move position.
AT=500 'Preset acceleration.
VT=1000000 'Preset velocity
PT=0 'Zero out position.
O=0 'Declare origin
G 'Servo in place
OCHN(RS2,0,N,9600,1,8,D)
PRINT("RPA",#13)
C0
IF LEN 'Check for chars
a=GETCHR 'Get char
IF a==13 'If carriage return
G 'Start motion
PT=0 'Reset buffered P to zero
PRINT("RP",#13) 'Next
ELSE PT=PT*10 'Shift buffered P
a=a-48 'Adjust for ASCII
PT=PT+a 'Build buffered P
ENDIF
ENDIF
GOTO(0) 'Loop forever
The ASCII code for zero is 48. The other nine digits count up from there so the ASCII code can
be converted to a useful number by subtracting the value of 0 (ASCII 48). The example
assumes that the most significant digits will be returned first. Any time it sees a new digit, it
multiplies the previous quantity by 10 to shift it over and then adds the new digit as the
least-significant one. After a carriage return is seen (ASCII 13), motion starts. After motion
Part 1: Programming: Using Data Mode