# JavaApplet used to print over Serial Port

What I want to share with you today is a way to print on a serial printer (in my case I used a barcode printer).<br /><br />Anyone is starting reading this post could thik: "<span style="font-style: italic;">Yes but is not too difficult to use serial port with Java</span>". Yes but you have to think to another point: we are talking of a web-application and, our printer, is linked to the client (naturally... maybe my server is <span style="font-weight: bold;">not</span> directly accessible). And another interesting point is that our installed JRE <span style="font-weight: bold;">could blocked</span> the access to computer ports. I say "could" but it surely does if you haven't set the "policy" file or add necessary library to the JRE.<br /><br />Anyway... here you are my experience.<br /><br /><span style="font-weight: bold;">Starting Point</span><br />The starting point was <span style="font-style: italic;">locate the necessary libraries</span>, and after some tests with official Sun <a href="http://java.sun.com/products/javacomm/">javax.comm</a> I've decided to use <a href="http://users.frii.com/jarvi/rxtx/">RXTX</a> library because they sort out with all necessary to use them on all operating systems.<br /><br />And now... all is very simple to do.<br /><br /><span style="font-weight: bold;">Applet</span><br />As I said before, we need "something" printed on client machine. So we surely need a simple applet, maybe hidden.<br /><br />Inside <span style="font-weight: bold;">init</span> method you just need to initialize your ports.<br /><br /><pre><code>@Override<br />public void init() {<br />try {<br />  Enumeration portList = CommPortIdentifier.getPortIdentifiers();<br /><br />  if (portList.hasMoreElements()) {<br />      this.portId = ((CommPortIdentifier) portList.nextElement());<br />  }<br />} catch (Exception e) {<br />  e.printStackTrace();<br />}<br /><br />//Opening port to test<br />try {<br />  CommPort serialPort = this.portId.open("SerialPort", 200);<br />  serialPort.close();<br />} catch (Exception ex) {<br />  ex.printStackTrace();<br />}<br />}<br /></code></pre><br /><br />The second part of this code <span style="font-style: italic;">Opening port to test</span> is <span style="font-weight: bold;">necessary</span> to let your applet always ready to print. During my tests if I didn't try to open the serial port after getting it sometime it does not work.<br /><br /><br />After this thing, what you need is a simple method to print what you need. I post here my code that, as I said, is used to print a barcode.<br /><br /><pre><code>public void printBarCode(String code) {<br />try {<br />  CommPort serialPort = this.portId.open("SerialPort", 200);<br /><br />  PrintStream out = new PrintStream(serialPort.getOutputStream(), true);<br />  out.println("N\n");<br />  out.println("D13\n");<br />  out.println("S2\n");<br />  out.println("B240,2,0,K,4,5,83,B,\"" + code + "\"\n");<br />  out.println("P1\n");<br /><br />  out.close();<br /><br />  serialPort.close();<br />} catch (Exception ex) {<br />  ex.printStackTrace();<br />}<br />}<br /></code></pre><br /><br />All information I'm sending to print was getting from EPL print manual. Are just information you need to set-up printer, page and barcode style.<br /><br />If you need anything special you can consider your applet <span style="font-style: italic;">ready to use</span>. You may just package it in a jar and add the jar to your webserver / web-application.<br /><br /><span style="font-weight: bold;">Adding applet to a page</span><br />Inside your page you can just add the applet using <span style="font-style: italic;">applet</span> tag or <span style="font-style: italic;">object</span> tag. In my case I have used applet one because the object give me some problems. In all tests I've done with applet works correctly both on IE and Firefox with windows and Linux.<br /><br /><pre><code>&lt;applet name=&quot;barcodeprinter&quot; <br /> id=&quot;barcodeprinter&quot; <br /> archive=&quot;/BarCodeApplet.jar&quot; <br /> code=&quot;com.bytecode.priterapplet.BarCodePrinter&quot; <br /> MAYSCRIPT=&quot;true&quot; <br /> style=&quot;width: 1px; height: 1px; background: white;&quot;&gt;<br />&lt;/applet&gt;<br /></code></pre><br /><br /><span style="font-weight: bold;">Note</span> the <span style="font-style: italic;">mayscript</span> parameter. Is used to let your browser (javascript) to interact with your applet. In fact if you don't add it what you can do is just load a page with an applet that starts printing a "static text". Not so useful!<br /><br />More interesting is sure letting the user the selection of the "code" to print. And you can make this with a <span style="font-weight: bold;">very simple</span> javascript code.<br /><br /><pre><code>&lt;script type=&quot;text/javascript&quot;&gt;<br />function inventario(code) {<br />   var applet = document.getElementById(&quot;barcodeprinter&quot;);<br />    if(code != '') {<br />       applet.printBarCode(code);<br />    }<br />}<br />&lt;/script&gt;<br /></code></pre><br /><br />And to call your javascript<br /><br /><pre><code>&lt;a href="#" onclick="inventario('selectedCode');"&gt;Selected Element&lt;/a&gt;<br /></code></pre><br /><br /><span style="font-weight:bold;">Configuring client</span><br />If you will try to use your applet without setting your JRE you couldn't print anything. What you need is just get <span style="font-style:italic;">RXTXcomm.jar</span> to copy in <span style="font-style:italic;">JRE_HOME/lib/ext</span> folder, and the correct library (DLL for Windows, .so for linux) and copy it in <span style="font-style:italic;">JRE_HOME/bin</span> folder. For Mac the procedure is a little bit different but you can find the instruction for all operating system directly inside the file you have downloaded from RXTX website.<br /><br /><span style="font-weight:bold;">Enhancements</span><br />What I've tried after this thing is the dynamic configuration of the client. What you can do is copy the DLL/SO file in a client folder, load dynamically into JRE but, anyway, you have to set, at least, the .policy file on the client. Is a security setting made inside Java: from a webapplet you can't doing what you want on a client computer.<br /><br />Anyway here you are a little example of what I've done<br /><br /><pre><code>if (System.getProperty(&quot;os.name&quot;).toUpperCase().contains(&quot;WINDOWS&quot;)) {<br />   libraryName = DLL_SERIAL_NAME;<br />    libraryFile = System.getProperty(&quot;java.io.tmpdir&quot;) + DLL_SERIAL_NAME;<br />} else if (System.getProperty(&quot;os.name&quot;).toUpperCase().contains(&quot;LINUX&quot;)) {<br />   if (System.getProperty(&quot;os.arch&quot;).contains(&quot;i386&quot;)) {<br />       libraryName = SO_SERIAL_NAME_32;<br />    } else {<br />        libraryName = SO_SERIAL_NAME_64;<br />    }<br /><br />    char fileSeparator = System.getProperty(&quot;file.separator&quot;).charAt(0);<br />    libraryFile = System.getProperty(&quot;java.io.tmpdir&quot;) + fileSeparator + SO_SERIAL_NAME;<br />}<br /><br />if (!(verifyLibraryExistence(libraryFile))) {<br />   copyResourceFromJar(libraryFile, libraryName);<br />}<br />try {<br />     System.load(libraryFile);<br />} catch (Exception e) {<br />     e.printStackTrace();<br />}<br /></code></pre><br /><br />DLL and so files are contained in the applet JAR.<br /><br />Hope to be useful to anyone. And write to me if you have any kind of problem!
