python中并行端口的数字字符串复用 [英] Multiplexing string of numbers in python for the parallel port

查看:119
本文介绍了python中并行端口的数字字符串复用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试类似的操作. 问题是我无法建立执行此操作的循环.

I'm trying to do something like this. The problem is that i can't build the loop that does that.

这是我的代码:

import parallel 
import time
p=parallel.Parallel() #object to use the parallel port
print ("Enter a string of numbers: ")
numStr = raw_input() #read line
numList=list(numSTr) #converts string to list
numlen=len(numList) #the length of the list
numBin=[['1','0001'], ['2','0010'],
 ['4','0100'], ['5','0101'],
 ['6','0110'], ['7','0111'],
 ['8','1000'], ['9','1001'],
 ['3','0011'], ['0','0000']] #Less significant bits of the numbers from 0 to 9 in a bidimesional array
p.setData(0) #clear the displays
pos=['0001','0010','0100','1000'] #Unique possible positions for the number from 0 to 9. 
c=(str(pos[])+str((numBin[][1]))) #here if the number in the list numList exist and also is in numBin. It joins the position and the number in binary, creating an number that will be send in decimal to the parallel port.
p.setData(int(c,2)) #send the binary number in decimal

如果有人可以帮助我,那将是令人满足的

If someone can help me, that would be gratifying

numBin中的最高有效位定义了打开什么显示.重要性较低的数字定义. 例如:

The most significant bits that are in numBin, define what display to turn on. And the less significant define the number. For example:

字符串为{'7','1','5','4','8'}. 因此,在最后一个显示中显示的第一个数字是'7'.因此,我们将二进制7设为'0111',并将该二进制字符串与第一个显示位置'0001'相连.因此,我们创建了一个二进制数字:'00010111'.我们将该数字转换为十进制,然后将其发送到并行端口.并口打开las显示器并显示数字7. 第二次,它必须在第二个拳头位置显示"7"和"1",依此类推.

The string is {'7', '1', '5', '4', '8'}. So the first number to show in the last display is '7'. SO we take the binary 7 that is '0111' and join that binary string with the first display position that is '0001'. SO we create a binary number: '00010111'. We conver that number to decimal and send it to the parallel port. The parallel port turns on the las display and shows the number 7. The second time, it must show a '7' and a '1' in the second and fist position and so.

X X X X
X X X 7
X X 7 1
X 7 1 5
7 1 5 4
1 5 4 8
5 4 8 X
4 8 X X
8 X X X
X X X X

"X"表示显示屏处于关闭状态,数字表示它在显示屏上的位置,如您在电路中所见.

The 'X' represents that the display is off and the number represents itself in the display position as you can see in the circuit.

推荐答案

看看您的电路,您实际上无法同时显示不同的数字.我在演示FPGA板上有这样的电路,并且必须创建一个软件驱动程序,以比眼睛能看到的速度快的速度在正确的位置闪烁显示器上的数字.

Looking at your circuit, you can't actually display different numbers simultaneously. I had a circuit like this on a demo FPGA board, and had to create a software driver to flicker the numbers on the display in the correct positions at a speed faster than the eye could detect.

下面是一个粗略的算法,使用Mock对象模拟并行端口和我的测试显示.它必须在支持不带换行符的回车的终端上运行.

Below is a a rough algorithm, using a Mock object to simulate the parallel port and the display for my testing. It must be run on a terminal that supports carriage return without linefeed.

您应该可以放入并行库,但是可能必须调整控制位以匹配您的硬件:

You should be able to drop in your parallel library instead, but may have to adjust the control bits to match your hardware:

import sys

class ParallelMock(object):

    def __init__(self):
        '''Init and blank the "display".'''
        self.display = [' '] * 4
        self._update()

    def setData(self,data):
        '''Bits 0-3 are the "value".
           Bits 4-7 are positions 0-3 (first-to-last).
        '''
        self.display = [' '] * 4
        value = data & 0xF
        if data & 0x10:
            self.display[0] = str(value)
        if data & 0x20:
            self.display[1] = str(value)
        if data & 0x40:
            self.display[2] = str(value)
        if data & 0x80:
            self.display[3] = str(value)
        self._update()

    def _update(self):
        '''Write over the same four terminal positions each time.'''
        sys.stdout.write(''.join(self.display) + '\r')

if __name__ == '__main__':
    p = ParallelMock()

    nums = raw_input("Enter a string of numbers: ")

    # Shift over the steam four-at-a-time.
    stream = 'XXXX' + nums + 'XXXX'
    data = [0] * 4
    for i in range(len(stream)-3):
        # Precompute data
        for pos in range(4):
            value = stream[i+pos]
            data[pos] = 0 if value == 'X' else (1<<(pos+4)) + int(value)
        # "Flicker" the display...
        for delay in xrange(1000):
            # Display each position briefly.
            for d in data:
                p.setData(d)
        # Clear the display when done
        p.setData(0)

这篇关于python中并行端口的数字字符串复用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆