Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为并行端口多路复用python中的数字字符串_Python_Parallel Processing_Port_Multiplexing - Fatal编程技术网

为并行端口多路复用python中的数字字符串

为并行端口多路复用python中的数字字符串,python,parallel-processing,port,multiplexing,Python,Parallel Processing,Port,Multiplexing,我正试着做类似的事情。 问题是我无法构建这样做的循环。 这是我的密码: 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(nu

我正试着做类似的事情。 问题是我无法构建这样做的循环。

这是我的密码:

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
如果有人能帮助我,那将是令人欣慰的

numBin中的最高有效位定义了要打开的显示。不太重要的定义了这个数字。 例如:

字符串是{'7','1','5','4','8'}。 所以最后一次显示的第一个数字是“7”。所以我们取二进制7,即“0111”,并将该二进制字符串与第一个显示位置“0001”连接起来。所以我们创建了一个二进制数:“00010111”。我们将该数字转换为十进制数并发送到并行端口。并行端口打开las显示屏并显示数字7。 第二次,它必须在第二和第一个位置显示“7”和“1”,以此类推

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”表示显示器关闭,数字表示其自身处于显示位置,如您在电路中所见

import parallel 
import time
p=parallel.Parallel()                        # object to use the parallel port
print ("Enter a string of numbers: ")
numStr = bytearray(raw_input())
p.setData(0)                                 # clear the displays
while True:                                  # refresh as fast as you need to
    for i,n in enumerate(numStr,4):
        p.setData(1<<i | n&0xf)

这是按位或与数字的ascii码的最后4位相加,以给出您需要写入并行端口的值。查看您的电路,实际上无法同时显示不同的数字。我在一块演示FPGA板上有一个这样的电路,必须创建一个软件驱动程序,以比眼睛能检测到的更快的速度将显示器上的数字闪烁到正确的位置

下面是一个粗略的算法,使用一个模拟对象来模拟并行端口和显示,以便进行测试。它必须在支持无换行的回车的终端上运行

您应该能够放入并行库,但可能需要调整控制位以匹配硬件:

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)

很难猜测连接到并行端口的是什么。您有电路图或一些规格吗?并行端口有8条数据线和3个其他输出可用。我不认为你可以直接从输出端驱动led非常明亮,所以我猜还有更多的电路丢失数据引脚2到5连接到7448 BCD,这些引脚在7段显示器中显示数字。其他的针脚6到9为数字选择位置。我已经这样做了。无论如何,谢谢。问题是它必须滚动字符串,然后在enumeratenumStr[x:x+4]中使用i,n,4:并在需要移位1时更改x的值position@aerojun,如果您已经完成了,为什么不发布工作代码?它和您的不一样。它只显示1234。如果你想看的话,就给你:它很管用!谢谢。我要学习代码来理解它。另外,我可以为我的处境中的人分享代码吗?
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)