chrisoma
Posts: 4
Joined: Mon Feb 08, 2016 5:52 pm
Location: Mexicali, Mexico

Read Several GPIOS (Inputs)

Mon Feb 08, 2016 7:01 pm

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...?

User avatar
joan
Posts: 14658
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read Several GPIOS (Inputs)

Mon Feb 08, 2016 7:20 pm

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?

User avatar
liudr
Posts: 685
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Read Several GPIOS (Inputs)

Mon Feb 08, 2016 8:30 pm

Use Arduino instead. Easy to get 9 pins read in two statements, down to microsecond delay between the first 8 and the last one.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
joan
Posts: 14658
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read Several GPIOS (Inputs)

Mon Feb 08, 2016 8:37 pm

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.

chrisoma
Posts: 4
Joined: Mon Feb 08, 2016 5:52 pm
Location: Mexicali, Mexico

Re: Read Several GPIOS (Inputs)

Mon Feb 08, 2016 9:30 pm

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)

User avatar
joan
Posts: 14658
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Read Several GPIOS (Inputs)

Mon Feb 08, 2016 11:52 pm

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.

User avatar
rpdom
Posts: 16082
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Read Several GPIOS (Inputs)

Tue Feb 09, 2016 6:13 am

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,

User avatar
liudr
Posts: 685
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Read Several GPIOS (Inputs)

Tue Feb 09, 2016 6:16 pm

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?
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

Return to “Beginners”