Forum: PC-Programmierung Need help with reading from i2c bus


von Martin L. (tempusertempuser)


Lesenswert?

Hello,

how can i read out my i2c device?

the definition is

Command
0xD0
arrayfield 0 first 8bit
arrayfield 1 last 8bit

von Andreas B. (bitverdreher)


Lesenswert?

This is in depend what's written in the datasheet of your secret I2C 
device.

von Martin L. (tempusertempuser)


Lesenswert?


von Andreas B. (bitverdreher)


Lesenswert?

Send first the device address and the command:
0x0F 0xD0
then read the both bytes

von Martin L. (tempusertempuser)


Lesenswert?

How can I do this with python and later than also fit the two bytes 
together??

I am lost in the jungle

von Andreas B. (bitverdreher)


Lesenswert?

https://letmegooglethat.com/?q=python+i2c

General you should learn the programming language before what you want 
to use.
This forum is not a programming service.

von Martin L. (tempusertempuser)


Lesenswert?

Andreas B. schrieb:
> https://letmegooglethat.com/?q=python+i2c
>
> General you should learn the programming language before what you want
> to use.
> This forum is not a programming service.

This is my try but the result this wrong
1
#!/usr/bin/python3
2
import smbus
3
import requests
4
import time
5
6
bus = smbus.SMBus(1)
7
address = 0x0F
8
x = bus.read_i2c_block_data(address, 0xD0,3)
9
voltagein = x[2] <<8 | (x[1])
10
#print (x[1])
11
#print (x[2])
12
13
voltagein_string = "%i" % voltagein
14
print  (voltagein_string)
15
bus.close()
16
del x
17
del bus

von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

Martin L. schrieb:
> voltagein = x[2] <<8 | (x[1])
Try:
1
voltagein  = (x[0] * 256) + x[1]

: Bearbeitet durch User
von Martin L. (tempusertempuser)


Lesenswert?

Matthias S. schrieb:
> Martin L. schrieb:
>> voltagein = x[2] <<8 | (x[1])
> Try:
>
1
> voltagein  = (x[0] * 256) + x[1]
2
>

This prints 2 times the same value
1
bus = smbus.SMBus(1)
2
address = 0x0F
3
x = bus.read_i2c_block_data(address, 0xD0,2)
4
print ("x")
5
print (x)
6
#voltagein = (x[1]<<8) | (x[0])
7
voltagein  = (x[0] * 256) + x[1]
8
voltagein_string = "%i" % voltagein
9
print ("voltage in")
10
print (voltagein)
11
bus.close()
12
del x
13
del bus
14
15
time.sleep(2)
16
bus = smbus.SMBus(1)
17
address = 0x0F
18
x = bus.read_i2c_block_data(address, 0xD1,2)
19
print ("x")
20
print (x)
21
#powerextern = x[0] | (x[1]<<8)
22
powerextern = (x[0] * 256) + x[1]
23
pwerextern_string = "%i" % powerextern
24
print ("powerstring")
25
print (pwerextern_string)
26
bus.close()
27
del x
28
del bus

1
x
2
[208, 137]
3
voltage in
4
53385
5
x
6
[209, 132]
7
powerstring
8
53636

: Bearbeitet durch User
von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

Does look right for a 48V System.

von Martin L. (tempusertempuser)


Lesenswert?

but it should be ~5V

Beitrag #7752374 wurde vom Autor gelöscht.
von Hmmm (hmmm)


Lesenswert?

Martin L. schrieb:
> x
> [208, 137]

Martin L. schrieb:
> x
> [209, 132]

Seems like the first byte is always the command. What does your debug 
output look like after reading 3 bytes?

von Martin L. (tempusertempuser)


Lesenswert?

Hmmm schrieb:
> Martin L. schrieb:
>> x
>> [208, 137]
>
> Martin L. schrieb:
>> x
>> [209, 132]
>
> Seems like the first byte is always the command. What does your debug
> output look like after reading 3 bytes?
1
[sudo] password for openhabian: 
2
x
3
[208, 126, 20]
4
voltage in
5
53374
6
x
7
[209, 159, 20]
8
powerstring
9
53663

von Hmmm (hmmm)


Lesenswert?

Martin L. schrieb:
> [208, 126, 20]

powerextern = (x[2] * 256) + x[1]

von Martin L. (tempusertempuser)


Lesenswert?

Martin L. schrieb:
> Hmmm schrieb:
>> Martin L. schrieb:
>>> x
>>> [208, 137]
>>
>> Martin L. schrieb:
>>> x
>>> [209, 132]
>>
>> Seems like the first byte is always the command. What does your debug
>> output look like after reading 3 bytes?
>
>
1
> [sudo] password for openhabian:
2
> x
3
> [208, 126, 20]
4
> voltage in
5
> 53374
6
> x
7
> [209, 159, 20]
8
> powerstring
9
> 53663
10
> 
11
>



1
bus = smbus.SMBus(1)
2
address = 0x0F
3
x = bus.read_i2c_block_data(address, 0xD0,9)
4
print ("x")
5
print (x)
6
voltagein = (x[2]<<8) | (x[1])
7
#voltagein  = (x[1] * 256) + x[2]
8
voltagein_string = "%i" % voltagein
9
print ("voltage in")
10
print (voltagein)
11
bus.close()
12
del x
13
del bus
14
15
time.sleep(2)
16
bus = smbus.SMBus(1)
17
address = 0x0F
18
x = bus.read_i2c_block_data(address, 0xD1,9)
19
print ("x")
20
print (x)
21
powerextern = (x[3]<<8) | x[2]
22
#powerextern = (x[0] * 256) + x[1]
23
pwerextern_string = "%i" % powerextern
24
print ("powerstring")
25
print (pwerextern_string)
26
bus.close()
27
del x
28
del bus
1
x
2
[208, 110, 20, 1, 105, 20, 0, 0, 0]
3
voltage in
4
5230
5
x
6
[209, 143, 20, 1, 105, 20, 0, 0, 0]
7
powerstring
8
276

von Martin L. (tempusertempuser)


Lesenswert?

Hmmm schrieb:
> Martin L. schrieb:
>> [208, 126, 20]
>
> powerextern = (x[2] * 256) + x[1]

Das sollte rauskommen
1
********************************
2
*                              *
3
* S.USV solutions              *
4
* www.s-usv.com                *
5
*                              *
6
* Model: Advanced              *
7
* Firmware Version: 2.61       *
8
* Software Version: 2.40       *
9
* Hardware Version: 2.2        *
10
*                              *
11
* Mail notification: Disabled  *
12
*                              *
13
* Timed Boot: Disabled         *
14
* Boot time: 00:00:00          *
15
*                              *
16
* Timed Shutdown: Disabled     *
17
* Shutdown time: 00:00:00      *
18
*                              *
19
* Fri Oct 11 18:18:54 2024     *
20
*                              *
21
********************************
22
*                              *
23
* Powering Source: Primary     *
24
* Charging circuit: OFFLINE    *
25
* Charging current: 300 mA     *
26
*                              *
27
* Voltage in: 5.23 V           *
28
* Battery capacity: 100.00%    *
29
* Battery voltage: 4.20V       *
30
* Power Battery: 000.00 mA     *
31
* Power Extern: 1259.00 mA      *
32
*                              *
33
* Shutdown timer: -1           *
34
* Autostart:  enabled          *
35
* Sleep timer: 1               *
36
*                              *
37
********************************

von Hmmm (hmmm)


Lesenswert?

Martin L. schrieb:
> voltage in
> 5230

5230 mV = 5.230 V

The device seems to send the command byte followed by the low byte and 
the high byte of the value, so just use (x[2] * 256) + x[1] in both 
cases.

von Martin L. (tempusertempuser)


Lesenswert?

Hmmm schrieb:
> Martin L. schrieb:
>> voltage in
>> 5230
>
> 5230 mV = 5.230 V
>
> The device seems to send the command byte followed by the low byte and
> the high byte of the value, so just use (x[2] * 256) + x[1] in both
> cases.

there is no difference between the commands
1
bus = smbus.SMBus(1)
2
address = 0x0F
3
x = bus.read_i2c_block_data(address, 0xD0,3)
4
print ("x")
5
print (x)x
6
[208, 132, 20]
7
voltage in
8
5252
9
x
10
[209, 126, 20]
11
powerstring
12
5246
13
14
voltagein = (x[2]<<8) | (x[1])
15
#voltagein  = (x[1] * 256) + x[2]
16
voltagein_string = "%i" % voltagein
17
print ("voltage in")
18
print (voltagein)
19
bus.close()
20
del x
21
del bus
22
23
time.sleep(2)
24
bus = smbus.SMBus(1)
25
address = 0x0F
26
x = bus.read_i2c_block_data(address, 0xD1,3)
27
print ("x")
28
print (x)
29
#powerextern = (x[3]<<8) | x[2]
30
powerextern = (x[2] * 256) + x[1]
31
#powerextern = (x[0] * 256) + x[1]
32
pwerextern_string = "%i" % powerextern
33
print ("powerstring")
34
print (pwerextern_string)
35
bus.close()
36
del x
37
del bus


1
x
2
[208, 132, 20]
3
voltage in
4
5252
5
x
6
[209, 126, 20]
7
powerstring
8
5246

: Bearbeitet durch User
von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

There are 3 bytes in the response. First byte is the command and can be 
ignored.
The second byte is the LSB and the third byte is the MSB.
So
1
voltage = (x[2] * 256) + x[1]
and
1
voltage = (x[2] << 8) | x[1]
should give the same results

von Andreas B. (bitverdreher)


Lesenswert?

I miss the initialyze of the I2C bus.

von Martin L. (tempusertempuser)


Lesenswert?

Andreas B. schrieb:
> I miss the initialyze of the I2C bus.

How do I do this?

von Matthias S. (Firma: matzetronics) (mschoeldgen)


Lesenswert?

Martin L. schrieb:
> How do I do this?

Never mind, an initialization is obviously already there. Otherwise you 
wouldn't get any results from the I²C bus.
1
bus = smbus.SMBus(1)

: Bearbeitet durch User
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.