{"id":815,"date":"2006-12-21T07:44:36","date_gmt":"2006-12-21T15:44:36","guid":{"rendered":"http:\/\/arcanius.silverfir.net\/wp\/java-and-com-ports"},"modified":"2006-12-21T08:27:42","modified_gmt":"2006-12-21T16:27:42","slug":"java-and-com-ports","status":"publish","type":"post","link":"https:\/\/arcanius.silverfir.net\/blog\/java-and-com-ports\/","title":{"rendered":"Java and Com Ports"},"content":{"rendered":"<p>Win32Driver&#8230;<br \/>\njavax.comm&#8230;<\/p>\n<p><a href=\"http:\/\/practicalembeddedjava.com\/tools\/javatools.html#javaxcomm\">This is ridiculous!<\/a><\/p>\n<p>It shouldn&#8217;t be this hard, Sun.<\/p>\n<p>One of my implementations:<\/p>\n<p><!--more--><\/p>\n<pre>\r\n<code>\r\nimport java.io.*;\r\nimport java.util.*;\r\nimport javax.comm.*;\r\nimport com.sun.comm.Win32Driver;\r\n\r\n\/**\r\n * Open a serial port using Java\r\n *\r\n * @author Ryan McElroy\r\n *\/\r\npublic class WinComPort implements LowLevelInterface\r\n{\r\n\t\/\/ Implementation-specific stuff\r\n\tprivate List&lt;CommPortIdentifier&gt; portList;\r\n\tprivate CommPortIdentifier portId;\r\n\tprivate SerialPort serialPort;\r\n\tprivate OutputStream outputStream;\r\n\tprivate InputStream inputStream;\r\n\tprivate String portName;\r\n\tprivate ComPortListener cpl;\r\n\t\r\n\t\/\/ General stuff\r\n\tprotected Parser parser;\r\n\tpublic PrintStream so;\r\n\t\r\n\tpublic WinComPort(String port, int baud)\r\n\t{\r\n\t\tso = System.out;\r\n\t\tportName = port;\r\n\t\tinit();\r\n\t\tif (validate() &amp;&amp; open() &amp;&amp; setBaud(baud))\r\n\t\t{\r\n\t\t\tcpl = new ComPortListener();\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\tthrow new NullPointerException(\\&quot;Couldn\\'t open com port\\&quot;);\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void setParser (Parser p)\r\n\t{\r\n\t\t\/\/ cpl is inner class, so this works beautifully\r\n\t\tparser = p;\r\n\t\tif (cpl != null)\r\n\t\t{\r\n\t\t\tcpl.start();\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void finalize()\r\n\t{\r\n\t\tdisconnect();\r\n\t}\r\n\t\r\n\tpublic void disconnect()\r\n\t{\r\n\t\tif (cpl != null)\r\n\t\t{\r\n\t\t\tsynchronized (this)\r\n\t\t\t{\r\n\t\t\t\tcpl.running = false;\r\n\t\t\t\ttry\r\n\t\t\t\t{\r\n\t\t\t\t\twait();\r\n\t\t\t\t}\r\n\t\t\t\tcatch (InterruptedException e)\r\n\t\t\t\t{\r\n\t\t\t\t\te.printStackTrace();\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t\tcpl = null;\r\n\t\tinputStream = null;\r\n\t\toutputStream = null;\r\n\t\tserialPort.close();\r\n\t\tserialPort = null;\r\n\t\t\r\n\t}\r\n\t\r\n\t\/**\r\n\t * Sets portId to com port if com port is valid\r\n\t * @return FLIC_SUCCESS or FLICERR_PORT_NOT_FOUND\r\n\t *\/\r\n\tprivate boolean validate()\r\n\t{\r\n\t\tportList = getComPorts();\r\n\t\tportId = validatePort(portList, portName);\r\n\t\t\r\n\t\treturn (portId != null &amp;&amp; portId.getPortType() == CommPortIdentifier.PORT_SERIAL);\r\n\t}\r\n\t\r\n\t\/**\r\n\t * Sets baud rate of port\r\n\t *\/\r\n\tprivate boolean setBaud(int baud)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\tserialPort.setSerialPortParams(baud, SerialPort.DATABITS_8,\r\n\t\t\t\t\tSerialPort.STOPBITS_1, SerialPort.PARITY_NONE);\r\n\t\t}\r\n\t\tcatch (UnsupportedCommOperationException e)\r\n\t\t{\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\treturn true;\r\n\t}\r\n\t\r\n\t\/**\r\n\t * Opens serial port and sets of inputSteam and outputStream\r\n\t *\/\r\n\tprivate boolean open()\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\tserialPort = (SerialPort) portId.open(\\&quot;Plus Protocol\\&quot;, 2000);\r\n\t\t}\r\n\t\tcatch (PortInUseException e)\r\n\t\t{\r\n\t\t\tso.println(\\&quot;Port In Use\\&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\ttry\r\n\t\t{\r\n\t\t\toutputStream = serialPort.getOutputStream();\r\n\t\t\tinputStream = serialPort.getInputStream();\r\n\t\t}\r\n\t\tcatch (IOException e)\r\n\t\t{\r\n\t\t\tso.println(\\&quot;IO Exception\\&quot;);\r\n\t\t\treturn false;\r\n\t\t}\r\n\t\treturn true;\r\n\t}\r\n\r\n\t\/**\r\n\t * Sends the specified plus packet\r\n\t * @param packet the packet (a byte array) to send\r\n\t * @param length the length of the packet in the byte array\r\n\t *\/\r\n\tpublic synchronized void putBytes(byte[] packet, int offset, int length)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\toutputStream.write(0x0D);\r\n\t\t\t\r\n\t\t\t\/\/ wait 100ms (double the required 50ms wakeup)\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\tThread.sleep(100);\r\n\t\t\t}\r\n\t\t\tcatch (InterruptedException e)\r\n\t\t\t{\r\n\t\t\t\t\/\/ don\\'t care...\r\n\t\t\t}\r\n\t\t\toutputStream.write(packet, offset, length);\r\n\t\t}\r\n\t\tcatch (IOException e)\r\n\t\t{\r\n\t\t\t\/\/ don\\'t care?\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic synchronized void putByte(byte b)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\toutputStream.write(b);\r\n\t\t}\r\n\t\tcatch (IOException e)\r\n\t\t{\r\n\t\t\t\/\/ don\\'t care?\r\n\t\t}\r\n\t}\r\n\t\r\n\tpublic void flush()\r\n\t{\r\n\t\t\/\/ not needed for serial\r\n\t}\r\n\t\r\n\tprivate static void init()\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\t\/\/ Initialize the native com port drivers \r\n\t\t\tWin32Driver w32Driver = new Win32Driver();\r\n\t\t\tw32Driver.initialize();\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tSystem.out.println(\\&quot;Could not initialize Win32 Drivers: \\&quot; + e);\r\n\t\t}\r\n\t}\r\n\t\r\n\tprivate static CommPortIdentifier validatePort(\r\n\t\t\tList&lt;CommPortIdentifier&gt; valid, String name)\r\n\t{\r\n\t\tfor (CommPortIdentifier id : valid)\r\n\t\t{\r\n\t\t\tif (id.getName().equalsIgnoreCase(name))\r\n\t\t\t\treturn id;\r\n\t\t}\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic static List&lt;CommPortIdentifier&gt; getComPorts()\r\n\t{\r\n\t\tEnumeration ids = null;\r\n\t\tList&lt;CommPortIdentifier&gt; ports = new ArrayList&lt;CommPortIdentifier&gt;();\r\n\r\n\t\ttry\r\n\t\t{\r\n\t\t\tids = CommPortIdentifier.getPortIdentifiers();\r\n\t\t}\r\n\t\tcatch (Exception e)\r\n\t\t{\r\n\t\t\tSystem.out.println(\\&quot;Could not get list of com ports! \\&quot; + e);\r\n\t\t}\r\n\t\tfinally\r\n\t\t{\r\n\t\t\twhile (ids.hasMoreElements())\r\n\t\t\t{\r\n\t\t\t\tports.add((CommPortIdentifier) ids.nextElement());\r\n\t\t\t}\r\n\t\t}\r\n\t\treturn ports;\r\n\t}\r\n\t\r\n\tprivate class ComPortListener extends Thread\r\n\t{\r\n\t\tpublic static final int TIMEOUT = 1000;\t\/\/ in ms\r\n\t\t\r\n\t\tprivate int sleepTime;\r\n\t\t\r\n\t\tpublic boolean running;\r\n\t\t\r\n\t\tpublic ComPortListener ()\r\n\t\t{\r\n\t\t\trunning = true;\r\n\t\t}\t\t\r\n\t\t\r\n\t\tpublic void run()\r\n\t\t{\r\n\t\t\tSystem.out.println(\\&quot;Running\\&quot;);\r\n\t\t\twhile (running)\r\n\t\t\t{\r\n\t\t\t\tint available;\r\n\t\t\t\ttry\r\n\t\t\t\t{\r\n\t\t\t\t\tavailable = inputStream.available();\r\n\t\t\t\t\t\/\/ so.println(\\&quot;available: \\&quot; + available);\r\n\t\t\t\t}\r\n\t\t\t\tcatch (IOException e)\r\n\t\t\t\t{\r\n\t\t\t\t\tso.println(e);\r\n\t\t\t\t\tcontinue;\r\n\t\t\t\t}\r\n\t\t\t\tif (available == 0)\r\n\t\t\t\t{\r\n\t\t\t\t\tsleepTime++;\r\n\t\t\t\t\tif (sleepTime * 100 &gt;= TIMEOUT)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tparser.reset(true);\r\n\t\t\t\t\t\tsleepTime = 0;\r\n\t\t\t\t\t}\r\n\t\t\t\t\ttry\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tThread.sleep(100);\r\n\t\t\t\t\t}\r\n\t\t\t\t\tcatch (InterruptedException e)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\/\/ don\\'t care?\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t\telse\r\n\t\t\t\t{\r\n\t\t\t\t\t\/\/System.out.print(\\&quot;!\\&quot;);\r\n\t\t\t\t\tsleepTime = 0;\r\n\t\t\t\t\tbyte[] buf = new byte[512];\r\n\t\t\t\t\tint size = 0;\r\n\t\t\t\t\ttry\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tsize = inputStream.read(buf, 0, available);\r\n\t\t\t\t\t\t\/\/so.print(\\&quot;(sz:\\&quot;+size+\\&quot;)\\&quot;);\r\n\t\t\t\t\t\t\/\/System.out.print(\\&quot;\\\\n[\\&quot;);\r\n\t\t\t\t\t\tfor (int i=0; i &lt; size; i++)\r\n\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\/\/so.printf(\\&quot;%X,\\&quot;, (buf[i] &amp; 0x000000FF));\r\n\t\t\t\t\t\t\tparser.newByte(buf[i]);\r\n\t\t\t\t\t\t\tif (parser.isValid())\r\n\t\t\t\t\t\t\t{\r\n\t\t\t\t\t\t\t\tparser.dispatch();\r\n\t\t\t\t\t\t\t}\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\t\/\/System.out.println(\\&quot;]\\&quot;);\r\n\t\t\t\t\t}\r\n\t\t\t\t\tcatch (IOException e)\r\n\t\t\t\t\t{\r\n\t\t\t\t\t\tSystem.out.println(e);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tSystem.out.println(\\&quot;Stopped\\&quot;);\r\n\t\t\tsynchronized(WinComPort.this)\r\n\t\t\t{\r\n\t\t\t\tWinComPort.this.notifyAll();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Win32Driver&#8230; javax.comm&#8230; This is ridiculous! It shouldn&#8217;t be this hard, Sun. One of my implementations:<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[8],"class_list":["post-815","post","type-post","status-publish","format-standard","hentry","category-everything","tag-technology"],"_links":{"self":[{"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/posts\/815","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/comments?post=815"}],"version-history":[{"count":0,"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/posts\/815\/revisions"}],"wp:attachment":[{"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/media?parent=815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/categories?post=815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arcanius.silverfir.net\/blog\/wp-json\/wp\/v2\/tags?post=815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}