RS232 Interface

Set baud rate

The baud rate is initially set to 19200 and may be changed with these parameters.
request = 0
wIndex = 0
wValue = baud rate according to the table or the the formula
65536 - (24 * 10^6 / (32 * Baudrate))

Depending on the programming language, the variable wValue is defined as signed or unsigned integer. The range is either between 0 and 65535 or between -32768 and 32767. The conversion is made, by substracting 65536 from all values greater than 32767.

If wValue > 32767 then
wValue = wValue - 65536

In the table below the number in brackets is the converted value:
19200 Baud = 65497 (-39)
14400 Baud = 65484 (-52)
9600 Baud = 65458 (-78)
4800 Baud = 65380 (-156)
2400 Baud = 65224 (-312)
1200 Baud = 64911 (-625)
300 Baud = 63036 (-2500)

Example to set the baud rate to 9600:

send SERVICE vendor request 0 wValue 65458 wIndex 0
send SERVICE vendor request 0 wValue -78 wIndex 0

Read back handshake/decimal value
request = 0
wIndex = 1 for Handshake or = 2 for decimal value
wValue = 0

When characters are sent via the RS232 interface, they have a long path to travel- from the Macintosh application through various drivers and the operating system via the USB interface in Service USB, and then on to the external RS232 device. If the external device does not have the capacity to receive characters, it informs the Service USB via the handshake line (Pin 7). Service USB only relocates characters via the RS232 interface if the external device is ready (hardware handshake). Sometimes, however, the external device is not ready, and the Macintosh application wants to send characters via the USB to the RS232 interface. In this case, up to 128 bytes can be stored in the intermediate buffer-i.e. the Macintosh application has sent the characters off, the "send" command has been carried out without error, and the Macintosh program assumes that the characters have also been put out via RS232. If the RS232 receiver is not ready at that moment, the characters remain in the intermediate buffer until the receiver is ready. If the intermediate buffer is full, the "send" command then returns after a given (adjustable) period of time with an error message. It may therefore be useful to check the handshake line from Macintosh and also to check whether the receiver is ready to receive characters, and to inform the user with an error message that the RS232 device is not ready.

Service USB models manufactured 2008 or later has 16 bits for the digital inputs. Bit 8 corresponds to the handshake of the RS232. For software compatibility or if you use an older interface, this mode maps the handshake input to bit 4.

Example for mapping the handshake line to bit 4:
send SERVICE vendor request 0 wValue 0 wIndex 1
Afterwards, the condition of the digital input can be read using various commands mentioned above.

Example to get the status of the handshake line:
compare SERVICE input value mask "XXX0XXXX".
Bit 4 corresponds to the handshake line. All other bits should be ignored. The RS232 device is ready if bit 4 logically shows 0. So this example sends back „true", if the RS232-device is ready.


Important !

Don't forget to change this status if you wish to use the digital inputs. The following command restores the original status:
send SERVICE vendor request 0 wValue 0 wIndex 2 device id 0


Replace characters

Service USB can be prompted to replace one character with another while sending. This makes it possible to put a character-set table in the Service USB if the device (i.e., a receipt printer) uses a different character-set table than the Macintosh. This character exchange works for data sent to the external RS232 device. Characters that are being received are not changed.

In the course of UTF-8 and Unicode this command becomes obsolete. It´s obtained for compatibility reasons.

request = 0
wIndex = 3
wValue = 0xAABB
AA = from
BB = to
Example, to output an U instead of an X
X = ASCII Hexadezimalwert 58
U = ASCII Hexadezimalwert 55

Therefore, the hexadecimal value to be put out for wValue is 5855. Depending on the programming environment, this value must still be converted to the decimal number system. For example, the following AppleScript command only accepts decimal numbers:

send SERVICE vendor request 0 wValue 22613 wIndex 3 device id 0

The value for wValue must be in the range from -32768 to 32767. Numbers greater than 32767 may therefore have to be converted to the pre-character value range. If the number is greater than 32767, the number must be subtracted from 65536 to obtain a valid value.
If wValue > 32767 then
wValue = wValue - 65536


Send characters

To send characters out of the RS232 interface, use:
write bytes to SERVICE pipe x data y data size z
The following parameters apply:
pipe = must always be 0
data = the data to be sent
data size = the length of the data to be sent in bytes. If this parameter equals to 0, all data are put out.
timeout = lapsed time attempted to put out the data via RS232, in increments of 1/60ths of a second.

If the external RS232 device is not ready, and the internal character buffer (maximum 128 bytes) is full, the command cannot send any characters. The command then attempts to send for as long as "timeout" is reached and then returns a message with the error number 13001 (errServiceUSB_Timeout).

write bytes to SERVICE pipe 0 data "Test" timeout 100


Receive characters

Received characters are stored in a 1000 byte (approx.) intermediate buffer within Macintosh . This intermediate buffer can be read using the command:

read bytes from SERVICE
The following parameters apply to this function:
pipe = must always be 0.
for = the number of characters to be read. If this parameter equals 0, all characters are read.
device id = serial number of the Service USB. Can be 0, if only one
Service USB is connected.
timeout can be 0.


Find out the number of characters to be received

Sometimes it is useful to find out if characters have already been received. The command
read bytes available in SERVICE pipe
delivers the number of characters currently stored in the Macintosh buffer.

read bytes available in SERVICE pipe 0
A notification service can also be started under AppleScript and the framework (Xcode).

register SERVICE device notification notification type bytes in read pipe notification value 0

Hint: In the folder Xcode/SUSBTerminal/build there is a simple terminal program to send and receive characters via RS232. This will make the first steps with connected RS232 devices easier.