Page 1 of 1

Read Several GPIOS (Inputs)

Posted: Mon Feb 08, 2016 7:01 pm
by chrisoma
Hello
I'm looking for help about how to read the level of 9 inputs with a single instruction... (using Python)
Is it possible..?
Actually the application that I'm trying to resolve is a Legacy Printer Capture (read parallel output with Digital I/O's), but the read frequency its too low with 8 or nine instructions. (about 2K reads per second and I need to get over 20K)
Is there someone that already go thru this scenario...?

Re: Read Several GPIOS (Inputs)

Posted: Mon Feb 08, 2016 7:20 pm
by joan
It's one thing to read the GPIO, but how are you going to make sense of the bits? How do you know when a new "sample" is ready to be read?

Re: Read Several GPIOS (Inputs)

Posted: Mon Feb 08, 2016 8:30 pm
by liudr
Use Arduino instead. Easy to get 9 pins read in two statements, down to microsecond delay between the first 8 and the last one.

Re: Read Several GPIOS (Inputs)

Posted: Mon Feb 08, 2016 8:37 pm
by joan
liudr wrote:Use Arduino instead. Easy to get 9 pins read in two statements, down to microsecond delay between the first 8 and the last one.
You can read 32 pins on the Pi in 40 nanoseconds. The problem will be how to make sense of the data.

Re: Read Several GPIOS (Inputs)

Posted: Mon Feb 08, 2016 9:30 pm
by chrisoma
8 pins for the data, and the last one is the Strobe signal (STO), (this legacy device don't support handshake protocol).
Data bits only receive ASCII characters as info. A simple int to cad convertion do the job to translate the data.

These are the instructions I'm using , but takes to long.

D0= GPIO.input(insput1)
D1= GPIO.input(insput2)
D2= GPIO.input(insput3)
D3= GPIO.input(insput4)
D4= GPIO.input(insput5)
D5= GPIO.input(insput6)
D6= GPIO.input(insput7)
D7= GPIO.input(insput8)
cad = D0+(D1*2)+(D2*4)+(D3*8)+(D4*16)+(D5*32)+(D6*64)+(D7*128)

Re: Read Several GPIOS (Inputs)

Posted: Mon Feb 08, 2016 11:52 pm
by joan
You are probably being too ambitious for Python. Have you considered C instead?

The following Python manages about 18k on a Pi2. I'd expect less than half that on other Pi models.

Code: Select all

#!/usr/bin/env python

import time
import struct
import pigpio # http://abyz.co.uk/rpi/pigpio/python.html

#     0  1  2  3   4   5   6   7
BITS=[4, 7, 8, 9, 10, 11, 14, 15]

STROBE=17

COUNT=40000

pi = pigpio.pi() # Connect to Pi.
if not pi.connected:
   exit(0)

h = pi.notify_open()

pi.notify_begin(h, 1<<STROBE)

try:
   f = open("/dev/pigpio"+ str(h), "rb")

   f_out = open("log", "w")

   n = 0

   start = time.time()

   while True:

      chunk = f.read(12)

      if len(chunk) == 12:

         seqno, flags, tick, level = struct.unpack('HHII', chunk)

         if level & (1<<STROBE): # Strobe bit set?
            cad = 0
            if level & BITS[0]:
               cad |= 1
            if level & BITS[1]:
               cad |= 2
            if level & BITS[2]:
               cad |= 4
            if level & BITS[3]:
               cad |= 8
            if level & BITS[4]:
               cad |= 16
            if level & BITS[5]:
               cad |= 32
            if level & BITS[6]:
               cad |= 64
            if level & BITS[7]:
               cad |= 128
            f_out.write(str(cad)+"\n")
            
            n += 1

            if n >= COUNT:
               break
      else:
         break

   stop = time.time()

   print(COUNT, stop-start)

   f.close()
   f_out.close()

except IOError:
   pass

pi.notify_close(h)

pi.stop() # Disconnect from Pi.

Re: Read Several GPIOS (Inputs)

Posted: Tue Feb 09, 2016 6:13 am
by rpdom
I'd probably do it in C. It would handle the speed easily and be able to read GPIO 0-31 in one go. Then you just need to shuffle the bits around into the right order.

Since someone mentioned something about supporting a legacy parallel printer app on here a while back, I've been toying with the idea of building an interface board and code that will support it,

Re: Read Several GPIOS (Inputs)

Posted: Tue Feb 09, 2016 6:16 pm
by liudr
joan wrote:
liudr wrote:Use Arduino instead. Easy to get 9 pins read in two statements, down to microsecond delay between the first 8 and the last one.
You can read 32 pins on the Pi in 40 nanoseconds. The problem will be how to make sense of the data.
With an operating system such as raspbian? Is it sustainable?