<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Awooga!!! &#187; pl2303</title>
	<atom:link href="http://awooga.nl/tag/pl2303/feed" rel="self" type="application/rss+xml" />
	<link>http://awooga.nl</link>
	<description>how hard can it be?</description>
	<lastBuildDate>Tue, 05 Apr 2011 12:46:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Serial killers?</title>
		<link>http://awooga.nl/serial-killers</link>
		<comments>http://awooga.nl/serial-killers#comments</comments>
		<pubDate>Mon, 26 Jan 2009 19:25:11 +0000</pubDate>
		<dc:creator>Q</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[apc]]></category>
		<category><![CDATA[currentcost]]></category>
		<category><![CDATA[dell]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[monitoring]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[pl2303]]></category>
		<category><![CDATA[prolific]]></category>
		<category><![CDATA[serial]]></category>
		<category><![CDATA[smartups]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[udev]]></category>
		<category><![CDATA[ups]]></category>
		<category><![CDATA[usb]]></category>

		<guid isPermaLink="false">http://awooga.nl/?p=194</guid>
		<description><![CDATA[Ever since I added the Current Cost to my Ubuntu server, I was running into the problem that upon booting up, the order of my two (one for the Current Cost unit, and one for an APC SmartUPS) Prolific PL2303 serial ports changed. Each boot, the actual device hanging of the /dev/ttyUSB* nodes would be [...]]]></description>
			<content:encoded><![CDATA[<p>Ever since I added the <a href="http://awooga.nl/lies-damned-lies-and-statistics" class="liinternal">Current Cost</a> to my <a href="http://www.ubuntu.com/products/whatisubuntu/serveredition" target="_blank" class="liexternal">Ubuntu server</a>, I was running into the problem that upon booting up, the order of my two (one for the Current Cost unit, and one for an <a href="http://www.apcc.com/products/family/index.cfm?id=165" target="_blank" class="liexternal">APC SmartUPS</a>) <a href="http://www.prolific.com.tw/eng/Products.asp?ID=59" target="_blank" class="liexternal">Prolific PL2303</a> serial ports changed.  Each boot, the actual device hanging of the /dev/ttyUSB* nodes would be a complete random choice.  And that is not good :(</p>
<p>So I investigated writing some udev rules for them, but unfortunately, the PL2303&#8242;s are completely identical to the server, except that their position on the USB host would change.  And although I tried to find out a consistent way of determining the correct udev rule, I miserably failed.  The only way I was able to find out which device was which, was to issue a</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>ttyUSB0</pre></div></div>

<p>and waiting if some output would appear on the terminal.  The Current Cost unit spits out its data every 6 seconds, so if something showed up, that would be the right one to pick for the cacti data source.  The UPS would not show anything at all using the above command.</p>
<p>Reading a bit further on <a href="http://www.reactivated.net/writing_udev_rules.html" target="_blank" class="liexternal">udev rules</a> I noticed that you can also use external programs to name devices.  This got me thinking.  I would need a small program that would listen on the /dev/ttyUSB* node and timeout if nothing was received within a reasonable time.</p>
<p>Let&#8217;s start off with the udev rule that invokes the script:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># /etc/udev/rules.d/60-local.rules</span>
<span style="color: #666666; font-style: italic;"># Determine APC UPS and Current Cost USB ports</span>
<span style="color: #007800;">KERNEL</span>==<span style="color: #ff0000;">&quot;ttyUSB*&quot;</span>, \
    ATTRS<span style="color: #7a0874; font-weight: bold;">&#123;</span>idVendor<span style="color: #7a0874; font-weight: bold;">&#125;</span>==<span style="color: #ff0000;">&quot;067b&quot;</span>, ATTRS<span style="color: #7a0874; font-weight: bold;">&#123;</span>idProduct<span style="color: #7a0874; font-weight: bold;">&#125;</span>==<span style="color: #ff0000;">&quot;2303&quot;</span>, \
    <span style="color: #007800;">PROGRAM</span>=<span style="color: #ff0000;">&quot;/usr/local/bin/usb-dev-test.pl %k&quot;</span>, \
    SYMLINK+=<span style="color: #ff0000;">&quot;%c&quot;</span></pre></div></div>

<p>The %k parameter will be ttyUSB0, ttyUSB1, etc, and the script usb-dev-test.pl should output a single word, which will be substituted by udev in the %c parameter.  With the help of <a href="http://search.cpan.org/dist/Device-SerialPort/" target="_blank" class="liexternal">Device::SerialPort</a> it becomes trivial ;)</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/usr/bin/perl</span>
<span style="color: #666666; font-style: italic;"># /usr/local/bin/usb-dev-test.pl</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">use</span> Device<span style="color: #339933;">::</span><span style="color: #006600;">SerialPort</span> <span style="color: #000066;">qw</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">:</span>PARAM <span style="color: #339933;">:</span>STAT <span style="color: #cc66cc;">0.07</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$argument</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">$ARGV</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">$0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$port</span><span style="color: #339933;">=</span>Device<span style="color: #339933;">::</span><span style="color: #006600;">SerialPort</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">new</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;/dev/$argument&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$STALL_DEFAULT</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">8</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># how many seconds to wait for new input</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$timeout</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$STALL_DEFAULT</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">read_char_time</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #666666; font-style: italic;"># don't wait for each character</span>
<span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">read_const_time</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># 1 second per unfulfilled &quot;read&quot; call</span>
&nbsp;
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$chars</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">my</span> <span style="color: #0000ff;">$buffer</span><span style="color: #339933;">=</span><span style="color: #ff0000;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$timeout</span><span style="color: #339933;">&gt;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">my</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$count</span><span style="color: #339933;">,</span><span style="color: #0000ff;">$saw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">$port</span><span style="color: #339933;">-&gt;</span><span style="color: #006600;">read</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;"># will read _up to_ 255 chars</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$count</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$chars</span><span style="color: #339933;">+=</span><span style="color: #0000ff;">$count</span><span style="color: #339933;">;</span>
    <span style="color: #0000ff;">$buffer</span><span style="color: #339933;">.=</span><span style="color: #0000ff;">$saw</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Check here to see if what we want is in the $buffer</span>
    <span style="color: #666666; font-style: italic;"># say &quot;last&quot; if we find it</span>
    <span style="color: #b1b100;">last</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
  <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">$timeout</span><span style="color: #339933;">--;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$timeout</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;apcups&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066;">print</span> <span style="color: #ff0000;">&quot;currentcost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And look at the result&#8230;  Magic!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-la</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>ttyUSB<span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>currentcost <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>apcups
lrwxrwxrwx <span style="color: #000000;">1</span> root root         <span style="color: #000000;">7</span> Jan <span style="color: #000000;">26</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">10</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>apcups -<span style="color: #000000; font-weight: bold;">&gt;</span> ttyUSB0
lrwxrwxrwx <span style="color: #000000;">1</span> root root         <span style="color: #000000;">7</span> Jan <span style="color: #000000;">26</span> <span style="color: #000000;">13</span>:<span style="color: #000000;">10</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>currentcost -<span style="color: #000000; font-weight: bold;">&gt;</span> ttyUSB1
crw-rw-rw- <span style="color: #000000;">1</span> root dialout <span style="color: #000000;">188</span>, <span style="color: #000000;">0</span> Jan <span style="color: #000000;">26</span> <span style="color: #000000;">14</span>:09 <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>ttyUSB0
crw-rw-rw- <span style="color: #000000;">1</span> root dialout <span style="color: #000000;">188</span>, <span style="color: #000000;">1</span> Jan <span style="color: #000000;">26</span> <span style="color: #000000;">11</span>:<span style="color: #000000;">54</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>ttyUSB1</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://awooga.nl/serial-killers/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

