LoginSignup
0
0

More than 5 years have passed since last update.

raspberry pi で、MCP23017 の入力ポートを使う popen版

Last updated at Posted at 2017-05-29
mcp23017_input.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#   mcp23017_input.py
#
#                   May/29/2017
#
# ----------------------------------------------------------------
import sys
import time
#
from subprocess import Popen,PIPE
#
# ----------------------------------------------------------------
# [4]:
def i2c_set_proc (command):
    try:
        p1 = Popen(command,shell=True,stdout=PIPE,stderr=PIPE)
        stdout_data, stderr_data = p1.communicate()
    except Exception as ee:
        sys.stderr.write("*** error *** in i2c_set_proc ***\n")
        sys.stderr.write(str (ee) + '\n')
#
# ----------------------------------------------------------------
# [6]:
def i2c_get_proc (command):
    rvalue = ""
    try:
        p1 = Popen(command,shell=True,stdout=PIPE,stderr=PIPE)
        stdout_data, stderr_data = p1.communicate()
        rvalue = stdout_data.decode('utf-8')
    except Exception as ee:
        sys.stderr.write("*** error *** in i2c_get_proc ***\n")
        sys.stderr.write(str (ee) + '\n')
#
    return  rvalue
# ----------------------------------------------------------------
sys.stderr.write("*** start ***\n")
command = "i2cset -y 1 0x20 0x01 0xFF"
i2c_set_proc(command)
#
command = "i2cset -y 1 0x20 0x0D 0xFF"
i2c_set_proc(command)
#
command = "i2cget -y 1 0x20 0x13"
value=i2c_get_proc(command)
sys.stderr.write("value = " + value + "\n")
#
sys.stderr.write("*** end ***\n")
# ----------------------------------------------------------------
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0