Note that this Wiki is a work in progress, items may not be updated or may disappear entirely as the pages are updated.

Serial Control by the Direct Method

From SpacialAudio

Jump to: navigation, search

ABSTRACT: The Direct Command Method of Serial Control allows PAL to control external hardware devices that can be controlled by ASCII command strings over an RS-232 serial connection. Devices such as routing switchers and relay control cards can be controlled by this method. No external or third-party software is required outside of Sam Broadcaster. The PAL command sequences are simple and easy to understand and implement. The following article is an update of a support forum post dated April 23, 2009.

The Direct Command Method of Serial Control by PAL

In examining the serial control by PAL, there are two aspects of communication that must be looked at separately. First is the transmission of data, and second is the reception of data. In examining the issues separately from within PAL, a method has been uncovered that allows SAM to transmit data via a RS-232 serial port. Unfortunately, there is no corresponding direct method of receiving data. Therefore, work will need to continue on developing methods of receiving data and bi-directional communications. Nonetheless, the ability to only transmit data presents very powerful options for hardware control from within SAM.

Hardware control by a broadcast automation system usually breaks down into just a few categories. Some of these devices, such as video servers communicating with the VDCP protocol, require bi-directional communication. Fortunately, these devices are not common in radio. More common are routing switchers, GPI inputs and GPI relay outputs. The controllers for these devices usually feature a bi-directional protocol but function sufficiently with only one direction of data. For example, a GPI input controller, servicing cues from satellite receivers, will typically send data to the automation and can function without return communication. Likewise, switchers and GPI output controllers will receive data, act on it, and send data back to the automation. The data that is sent back to the automation is always ignored because the automation does not know what to do if the data is wrong. Given this, the ability to only send data is sufficient to control most serial controlled routing switchers and serial controlled relay output cards.

Example -- Controlling the Extron Switcher

The best way to explain the direct method is with an example. As an example of the direct command method, the control of an Extron MAV-1616-AV video and audio routing switcher will be presented. The Extron is an excellent choice for routing in a small station environment. Most Extron switchers have video. They can be ordered new with audio only but they are inexpensive enough to buy an audio/video model on eBay and just ignore the video. Care should be taken to select a switcher with the correct audio interface as the Extrons come with either low-level RCA connectors or traditional balanced high-level audio. This example should be easily adaptable to other brands and models of hardware.

Information on Extron switchers can be found here:
http://www.extron.com/product/listbytype.aspx?subtype=130
http://www.extron.com/product/product.aspx?id=mavplus1616v&subtype=130

Users wishing to switch between only two sources should consider using HCP.exe with the Winford Engineering relay cards. This solution is far less expensive than buying a routing switcher.

The first step is to connect the switcher to the serial port on the computer. In our example, a USB serial port was used. These are available for only a few bucks each on Ebay. It came up as COM1. By looking at the Extron manual, downloadable from the Extron website, the default speed of the port is 9600, N, 8, 1, with no handshaking. To verify that the connection is working, connect with HyperTerminal. Be sure to set the correct speed and handshaking. From the Extron manual, the commands I, Q, and N should generate a response.


I V16X16 A16X16 Exe1
Q Ver2.05.0001
N N60-240-04



Another option would be to open the switcher control program that came with the switcher, or is downloadable from the Extron web site. Once the port number is entered, the current matrix should be displayed. If there is a display then there is good communication. The switcher can be controlled by clicking in the white squares.

One might want to view the switcher status from the front of the switcher. To do so, press the ESC button on the switcher to clear the display. Press the I/O Audio and I/O Video buttons so both lights are lit solid. Press the View button. Press one of the input buttons, preferably input 4. If the switcher was configured with each input going to it's corresponding output, the input 4 and output 4 buttons should light indicating that input 4 is connected to output 4. Before proceeding, both HyperTerminal and the Extron Switcher control program must be shut down.

The next step is to create a PAL script. Open SAM. Open the PAL window and add a new script called “SerialTest.” Edit this script by adding the following two lines:


const ComPort = 'COM1';
StrToFile(ComPort, '4*1!4*2!4*3!');



Click RUN. If the input 4 light on the switcher is still lit, then outputs 1, 2, 3, and 4 should now be lit. The switcher is now under the control of PAL. Try testing this by changing each instance of the number 4 to the number 6 and running it again.

The first line of this script is the declaration of a constant. It is like a variable but once it has been declared, it can not be changed. This line need only be executed once in a script and would usually be at the very start of the script.

The second line is a function normally used to write text to a file. The first parameter is required to be a constant that was previously declared. Don’t just put COM1 in there. The second parameter is the string that was sent to the switcher. In this case, 4*1! Means send input 4 to output 1. Also, three different commands were strung together and sent at the same time. A PAL script may contain many of these lines.

The trick of this method is in that, since the very beginning of DOS, the names of the hardware ports have also been reserved file names in the operating system. So, to use a PAL command that writes to files to write to a com port is perfectly OK.

There is another function called FileToStr() and one would think that it might be possible to use this function to read from the com port. It does in fact read from the com port. But, it opens the port, reads from it, and closes it so fast that it never catches any data. This function does not have the smarts to open the port, wait for the data to come in and manage the data as it comes in. This is why we can not receive data with this method.

Initializing the Serial Port

Next, save the script, close SAM, reboot the machine, restart SAM and re-run the script. This time, it probably won’t work. Why not? This is because the first time, HyperTerminal and the switcher control program were run and controlled the serial port before it was opened by SAM. They have the smarts to control the speed and handshaking and initialize the port. SAM does not. Since SAM cannot initialize the port, SAM cannot open the port unless some other program initializes it first. The obvious solution would be to run HyperTerminal first before running SAM each time the machine reboots. This is not very practical as HyperTerminal requires some manual steps to operate.

One good solution would be to install a virtual com port redirector. It creates a software image of a com port, COM8, for example. COM8, being virtual, will always work properly regardless of how it is initialized and it will never raise an error. The redirector points all COM8 data to COM1. COM1 is properly initialized by the redirector so the problem is solved. A freeware redirector can be found at http://www.eterlogic.com/Products.VSPE.html.

There is an even easier way to solve this problem that also goes back to the ancient days of DOS. Open a new text file with Notepad and enter this single line: Mode COM1:9600,N,8,1 Save the file as "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\StartCom1.BAT" After the system boots, anyone logging in will execute the MODE command, properly initializing the COM port. The port will then always be ready to be used by SAM to control serial devices with the Direct Method.


Max Headroom
NetworkXXIII