Do It Yourself

If you want to add some device that XTension doesn't currently support, you can roll your own using a wonderfully simple method using AppleScript and some special tools that XTension provides.

This method lets you connect to a native serial port, or a USB-serial adaptor, or even a ethernet based serial port, and you can switch between those connections without changing your AppleScript !!

In general, what we do is to create an 'interface' in the XTension communication prefs, establishing the 'port' and serial preferences, and create two 'well-known' scripts that will handle the opening of the port, and respond to input received from that port.

For example, I have a very old serial port multiplexer “the PIPE”, that I designed and built over 25 years ago that lets me switch between 16 ports, regardless of the serial port settings. In this example, I've set up two iMacs with USB-serial adaptors, both of which then connect to two ports on “the PIPE”. It's not obvious here, but “the PIPE” is already configured to pass any data received from one port to the other port… just a 'pass-thru' connection.

Here I'll show how to create the port, send data from one iMac to the other, receive the data, and conditionally send a message back to the first iMac. (note that the two iMacs are named “Wallace” and “Perelandra”…)

Let's create the first new interface handler on “Wallace”.
In the XTension FILE menu, choose Preferences, and the Communications Tab. Then choose NEW…
Here, I've entered the name of the interface, “PIPE”, and set the Port and the Serial settings already:

Notice that you must specify the baud rate, parity, bits per character, and stop bits. You also have the option of telling the interface to use hardware handshake, and whether to force DTR. (CTS is by default set on, because we assume that we have a full-duplex connection.)

Notice also that if you choose to have a remote TCP type connection, you probably have to set up the remote IP interface using the instructions for that 'bridge'. For example, with the Barionet, you use the web browser.

Now, you will want to set up the scripts for this DIY interface. CLICK on the Received Data Script EDIT button :

Notice that there are two 'handlers' here. The first one will be invoked every time you enable this DIY port, or whenever it is automatically restarted after some failure. In this handler, you will want to specify any handling preferences that are different from the defaults.

XTension provides a verb for setting these :

You can use these verbs anytime, but it's the most logical to use them here in the on init() handler.

The on DataAvailable() handler is the workhorse of the DIY interface system.

Here you put all the code that knows what to do when data is received from your device.

In this example, I simply write whatever I receive to the XTension Log, but this is a bit different on the 'other side' of this little show-and-tell…

Now, on the iMac named Perelandra, which is also connected to the PIPE by a USB-Serial adaptor, I have created a similar DIY port, and set up the on DataAvailable handler like this :

This handler will also write the received data to the Log, but will additionally test the input to see whether it contains the word : “Hello”. If so, it will send a message back to the PIPE, which will be passed thru the PIPE to the port that “Wallace” is connected, and thus it will be received by the DataAvailable handler which will write it to the Log on “Wallace”.

Now, to start this all off, we're going to use the XTension verb “send data” and the ad hoc “Command Line” feature of XTension, to send a string out the PIPE DIY interface :

which will be passed thru the PIPE, to the iMac “Perelandra”, which will receive it via its DIY interface. There, the incoming data will be passed to the DataAvailable handler, which, as above, will write the message to the Log :

The handler will then see that there is the keyword “Hello” in the message, and will send back the “Hello Wallace” message…

Which will be passed to the DataAvailable handler on “Wallace”, which will write it to the Log :

Oh yes, don't be confused by that first line in the above snippet…that is just what XTension displays in response to the send data command … it's a text representation of the HexaDecimal value sent to the DIY interface. If you don't understand 'hex', or the difference between binary, hexadecimal, and 'displayed binary/hex', then you are probably in for some interesting times trying to debug your new DIY interface :)

Now I've probably made this example too complex by showing two copies of XTension, but this is something that you can set up yourself, without the PIPE in between. You just connect the two USB-serial adaptors together using a simple null modem (you do know what this if you're even thinking of doing a DIY interface ?)…

Update: Notice that in the above example I am working with an interface that is TEXT only. IF you have an interface that uses ASCII HEX codes, you do not want to put spaces between the HEXITS. Also, for Binary protocols, note that James has implemented the entire AppleScript class for Hex, Octal and Binary codes. See the XTension Dictionary using Script Editor.

supported_hardware/diy.txt · Last modified: 2011/11/11 11:24 by Michael Ferguson
CC Attribution-No Derivative Works 3.0 Unported Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0