Here is a sample application written in C# that uses the alphasign.xsl file from the BBXML package. It reads your XML command file, transforms it to Alpha Sign protocol, and writes that output to the serial port.
Install the .NET 2.0 SDK and add the directory containing the
csc.exe compiler to your PATH. On my system, that directory is
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727. You
could also use the pre-configured command window that gets set
up in your Start Menu when you install.
Copy the alphasign.xsl file from the BBXML package to your working directory.
csc src\AlphasignXMLApp.cs
AlphaSignXMLApp.exe -port COM1 -xsl alphasign.xsl -xml hello.xml
If all is well, it should send "hello" to your sign. You can send the other file, clearmsg.xml, to clear the display.
AlphaSignXMLApp.exe -port COM1 -xsl alphasign.xsl -xml clearmsg.xml
There are two main steps for using XML with the Alpha sign.
The sample application code is AlphaSignXMLApp.cs. You might notice that it is very similar to the Java example. In fact, I just ported my Java program to C# and the .NET class library.
using System;
using System.Xml.Xsl;
using System.Xml;
using System.IO.Ports;
using System.IO;
using System.Text;
// copy alphasign.xsl
// from http://pobox.com/~dfranklin/bbxml/bbxml/xml/alphasign.xsl
// compile: csc AlphasignXMLApp.cs
// run: AlphaSignXMLApp.exe -port COM1 -xsl alphasign.xsl -xml hello.xml
public class AlphaSignXMLApp
{
private String serialPortName;
private String xslFileName;
private String xmlFileName;
/**
* Command line args:
* <ul>
* <li>-port port_name</li>
* <li>-xsl alphasign.xsl</li>
* <li>-xml yourfile.xml</li>
* </ul>
*/
public static void Main(String[] args)
{
AlphaSignXMLApp app = new AlphaSignXMLApp();
for (int i = 0; i < args.Length; i++) {
if ("-port" == args[i]) {
app.setSerialPortName(args[++i]);
} else if ("-xsl" == args[i]) {
app.setXSLFileName(args[++i]);
} else if ("-xml" == args[i]) {
app.setXMLFileName(args[++i]);
}
}
app.run();
}
public void setSerialPortName(String serialPortName)
{
this.serialPortName = serialPortName;
}
public String getSerialPortName()
{
return serialPortName;
}
public void setXSLFileName(String xslFileName)
{
this.xslFileName = xslFileName;
}
public String getXSLFileName()
{
return xslFileName;
}
public void setXMLFileName(String xmlFileName)
{
this.xmlFileName = xmlFileName;
}
public String getXMLFileName()
{
return xmlFileName;
}
public void run()
{
try {
// create a buffer for the XSLT output
MemoryStream memStream = new MemoryStream(1024);
transform(memStream);
memStream.Seek(0, SeekOrigin.Begin);
// write the buffer to the serial port
writeToSerialPort(memStream);
} catch (Exception e) {
Console.WriteLine(e);
}
}
/**
* Transform the XML file using the XSL file, writing result to the
* specified OutputStream.
*/
public void transform(Stream stream)
{
XslCompiledTransform xslt = new XslCompiledTransform();
XsltSettings xsltSettings = new XsltSettings();
// it complains if we don't do this (I guess it's a security feature)
xsltSettings.EnableDocumentFunction = true;
xslt.Load(xslFileName, xsltSettings, null);
// The output encoding specified in alphasign.xsl is UTF-8.
// By default Transform includes a Byte Order Mark, which we do not want.
// We fix that by passing false to the UTF8Encoding constructor.
// Alternatively, alphasign.xsl could specify <xsl:output encoding="ASCII"/>,
// which is actually more appropriate for the Alpha sign, but that causes
// the transform to fail completely when the input text contains non-ASCII
// characters. I prefer to let it output a few garbage characters rather
// than fail completely.
XmlTextWriter writer = new XmlTextWriter(stream, new UTF8Encoding(false));
xslt.Transform(xmlFileName, writer);
}
/**
* Read from InputStream and write to the serial port.
*/
public void writeToSerialPort(Stream inStream)
{
// get, open, and configure the serial port
SerialPort serialPort
= new SerialPort(serialPortName, 9600, Parity.Even, 7, StopBits.Two);
serialPort.Handshake = Handshake.None;
serialPort.Open();
// write the data
int b;
while (-1 != (b = inStream.ReadByte())) {
byte[] buf = new byte[1];
buf[0] = (byte)b;
serialPort.Write(buf, 0, 1);
}
serialPort.Close();
}
}
In a more practical real-world application, you would probably follow these steps.