<p>你说的是软硬件接口。它们通常通过给寄存器分配一个1字节的值来设置为低和高。一个<a href="http://en.wikipedia.org/wiki/Parallel_port" rel="nofollow">parallel port</a>有8个管脚供数据通过。在C语言、C++语言这样的低级语言中,会有一个登记器,我们称之为“A”,它在8位对应于8个数据引脚的地方。例如:</p>
<p>假设resgister A设置为pins:[7,6,5,4,3,2,1,0]</p>
<p>类C伪码</p>
<pre><code>A=0x00 // all pins are set low
A=0xFF // all pins are high
A=0xF0 // Pins 0:3 are low, Pins 4:7 are high
</code></pre>
<p>这个想法贯穿于<a href="http://en.wikipedia.org/wiki/Parallel_port" rel="nofollow">PyParallel</a></p>
<pre><code>import parallel
p = parallel.Parallel() # open LPT1
p.setData(0x55) #<--- this is your bread and butter here
</code></pre>
<p>setData是你感兴趣的函数。0x55转换为二进制</p>
<p>0b0100101</p>
<p>-或-</p>
<p>[长高高]</p>
<p>所以现在你可以将数据设置为某个字节,但是我如何发送一组数据。。。比如说3字节0x00、0x01、0x02?好吧,当接收机器确认接收到刚刚发送的东西时,你需要注意确认线路。</p>
<p>天真的实现:</p>
<pre><code>data=[0x00, 0x01, 0x02]
while data:
onebyte=data.pop()
p.setDataStrobe('low') #signal that we're sending data
p.setData(onebyte)
while p.getInAcknowledge() == 'high': #wait for this line to go 'low'
# to indicate an ACK
pass #we're waiting for it to acknowledge...
p.setDataStrobe('high')#Ok, we're done sending that byte.
</code></pre>
<p>好吧,这不能直接回答你的问题。假设我只想设置pin 5的高或低。也许我的针上有个LED。然后你只需要一些二进制操作。</p>
<pre><code>portState = 0b01100000 #Somehow the parallel port has this currently set
newportState = portState | 0b00010000#<-- this is called a bitmask
print newportState
>>> 0b011*1*0000
</code></pre>
<p>现在让我们澄清一下。。。</p>
<pre><code>newportState = 0b01110000
clearedPin5 = newportState & 11101111
print clearedPin5
>>> 0b011*0*0000
</code></pre>
<p>如果这些二进制操作是外来的,我建议在avrfreaks上使用这种优秀的<a href="http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=40348&highlight=programming%20101#40348" rel="nofollow">tutorial</a>。在进一步发展之前,我会和他们保持亲密关系。像这样的嵌入式软件概念充满了位掩码和位移。</p>