简体   繁体   English

如何在Linux中通过USB从i2c设备检索数据

[英]How to retrieve data from an i2c device over usb in linux

I have a temperature sensor, which is connected using an USB-I2C adapter ( http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm ) I attached this device to my linux computer (suse10). 我有一个温度传感器,该温度传感器使用USB-I2C适配器( http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm )连接,并将该设备连接到linux计算机(suse10)。 I typed dmesg and saw 我输入dmesg并看到

usb 3-3: new full speed USB device using ohci_hcd and address 10
usb 3-3: new device found, idVendor=0403, idProduct=6001
usb 3-3: new device strings: Mfr=1, Product=2, SerialNumber=3
usb 3-3: Product: FT232R USB UART
usb 3-3: Manufacturer: FTDI
usb 3-3: SerialNumber: A7007K93
usb 3-3: configuration #1 chosen from 1 choice
ftdi_sio 3-3:1.0: FTDI USB Serial Device converter detected
drivers/usb/serial/ftdi_sio.c: Detected FT232BM
usb 3-3: FTDI USB Serial Device converter now attached to ttyUSB0

But I have no idea how to read the current temperature. 但是我不知道如何读取当前温度。

updated 1: Actually the I2C bus can attach up to 127 sensors. 更新1:实际上,I2C总线最多可以连接127个传感器。 But I have no idea how to list the addresses of available sensors. 但是我不知道如何列出可用传感器的地址。

Can anybody give me some hints? 有人可以给我一些提示吗? Thanks in advance 提前致谢

Your adapter allows you to send I2C commands over a virtual serial port. 您的适配器允许您通过虚拟串行端口发送I2C命令。 A serial port has been created for you. 已为您创建了一个串行端口。 You need to open it and send commands to it. 您需要打开它并向它发送命令。 The commands are specific to the device you are connected to. 这些命令特定于您所连接的设备。 See the example in the link you provided to get an idea. 请参阅您提供的链接中的示例以获取想法。

It is hard to give you correct instructions without a datasheet. 没有数据表,很难为您提供正确的说明。 Most probably your device will use one byte address and the read procedure is as follows: 您的设备很可能将使用一个字节地址,读取过程如下:

[I2C_AD1] [Device I2C address + Read bit] [Device Address register] [Number of bytes to read]
0x55 0xXX 0x00 0x01

You need to send 4 bytes to the serial port. 您需要向串行端口发送4个字节。 The first one instructs the USB to I2C converter to send a read command. 第一个指令指示USB到I2C转换器发送读取命令。 The second one is the address of the device attached to the I2C bus. 第二个是连接到I2C总线的设备的地址。 I2C devices use 7-bit addresses (0-127). I2C设备使用7位地址(0-127)。 Usually these are given with one bit shifted at the left. 通常这些都是左移一位。 Therefore you need to scan these addresses (iterate from 0 to 127, shift left one bit, set bit0 to 1): 因此,您需要扫描以下地址(从0到127迭代,左移一位,将bit0设置为1):

([0x00 - 0x7F] << 1) | 1

Since we don't have a datasheet I can't tell anything about the last two bytes. 由于我们没有数据表,因此我无法告诉任何有关最后两个字节的信息。 You could try to use dummy values. 您可以尝试使用伪值。 If a device is attached to the scanned I2C address, it should reply with a NACK to an attempt to read a non-existing register. 如果将设备连接到扫描的I2C地址,则该设备应以NACK答复以尝试读取不存在的寄存器。 Read commands sent to an I2C address that doesn't correspond to an actual device should be ignored. 发送到与实际设备不对应的I2C地址的读取命令应被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM