首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从i2cdetect控制台输出中提取i2c地址

如何从i2cdetect控制台输出中提取i2c地址
EN

Stack Overflow用户
提问于 2018-02-15 06:04:28
回答 1查看 679关注 0票数 1

我是linux环境的新手,但由于我的新项目,我正在学习,我喜欢它(来自vxwork时代)。现在我的问题是如何使用bash脚本从命令"i2cdetect“中过滤I2c地址。我听说我可以使用SED或AWKD来扫描和搜索文本。基本上,我想获取每个i2c地址,如0c、5b、5c、UU、6e、6f。

我感谢你给我的任何线索或帮助。

root@plnx_aarch64:/# i2cdetect -y -r 3 0 1 2 3 4 5 6 7 8 9 a b c d e f

00:- 0c

10:

20:

30:

40:

50:- 5b 5c

60:- 6e 6f

70: UU

EN

回答 1

Stack Overflow用户

发布于 2018-02-17 02:14:00

我写了一个函数来做这件事。我不确定是否有更好的方法,但这似乎确实有效。

代码语言:javascript
复制
import re

def get_addresses(i2cdetect_output):
    ''' Takes output from i2cdetect and extracts the addresses
        for the entries.
    '''

    # Get the rows, minus the first one
    i2cdetect_rows = i2cdetect_output.split('\r\n')[1:]
    i2cdetect_matrix = []

    # Add the rows to the matrix without the numbers and colon at the beginning
    for row in i2cdetect_rows:
        i2cdetect_matrix.append(filter(None, re.split(' +', row))[1:])

    # Add spaces to the first and last rows to make regularly shaped matrix
    i2cdetect_matrix[0] = ['  ', '  ', '  '] + i2cdetect_matrix[0]
    i2cdetect_matrix[7] = i2cdetect_matrix[7][:-1] + ['  ', '  ', '  ', '  ', '  ', '  ', '  ', '  ']


    # Make a list of the addresses present
    address_list = []
    for i in range(len(i2cdetect_matrix)):
        for j in range(len(i2cdetect_matrix[i])):
            if i2cdetect_matrix[i][j] not in ('  ', '--', 'UU'):
                address_list.append(str(i) + str(format(j, 'x')))
            if i2cdetect_matrix[i][j] == 'UU':
                address_list.append('UU')

    return address_list
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48797011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档