sdm_modbus icon indicating copy to clipboard operation
sdm_modbus copied to clipboard

RASPBERRY PI 4 using Python last version don't work

Open JorgeGoncalves1 opened this issue 3 years ago • 14 comments

I did a fresh installation of my Raspberry pi 4, with Debian. I wrote the program in Python and I can't get a connection.

ONLY VERSION 0.4.3 WORK. CAN YOU SOLVE ?

i'm using a USB TO RS485 converter.

import sdm_modbus device = sdm_modbus.SDM230(device="/dev/serial0", baud=9600) print(device)

if device.connected(): print("Working") else: print("NOT Working")

Regards George

JorgeGoncalves1 avatar May 17 '22 18:05 JorgeGoncalves1

Does one of the example scripts work? What output do you get? Have you tried reading any values or are you just getting an unexpected value on connected()?

nmakel avatar Jun 09 '22 19:06 nmakel

The example don't get connection. But if I install version 0.4.3 it does work.

Regards

⁣Obter o BlueMail para Android ​

Em 9/06/2022, 20:32, em 20:32, niels @.***> escreveu:

Does one of the example scripts work? What output do you get? Have you tried reading any values or are you just getting an unexpected value on connected()?

-- Reply to this email directly or view it on GitHub: https://github.com/nmakel/sdm_modbus/issues/14#issuecomment-1151535689 You are receiving this because you authored the thread.

Message ID: @.***>

JorgeGoncalves1 avatar Jun 09 '22 19:06 JorgeGoncalves1

And the other two questions I asked?

nmakel avatar Jun 09 '22 19:06 nmakel

The example script works, I get correct readings, but, only in version 0.4.3.

Last version don't get connection neither readings.

⁣Obter o BlueMail para Android ​

Em 9/06/2022, 20:32, em 20:32, niels @.***> escreveu:

Does one of the example scripts work? What output do you get? Have you tried reading any values or are you just getting an unexpected value on connected()?

-- Reply to this email directly or view it on GitHub: https://github.com/nmakel/sdm_modbus/issues/14#issuecomment-1151535689 You are receiving this because you authored the thread.

Message ID: @.***>

JorgeGoncalves1 avatar Jun 09 '22 19:06 JorgeGoncalves1

Since 0.4.3 many many things have changed. Without more debugging output it will be difficult to narrow down what's wrong.

Can you check the pymodbus version that is installed? Can you try adding an explicit connect() call?

Could you also try running the test suite?

nmakel avatar Jun 09 '22 20:06 nmakel

Pymodbus version:

@.***:~ $ pip install pymodbus Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple Requirement already satisfied: pymodbus in ./.local/lib/python3.9/site-packages (2.5.3) Requirement already satisfied: pyserial>=3.4 in /usr/lib/python3/dist-packages (from pymodbus) (3.5b0) Requirement already satisfied: six>=1.15.0 in /usr/local/lib/python3.9/dist-packages (from pymodbus) (1.16.0)

No dia 09/06/2022, às 21:14, niels @.***> escreveu:

pymodbus

JorgeGoncalves1 avatar Jun 09 '22 20:06 JorgeGoncalves1

Before I start, thank you for all the hard work that you have put into this ad keeping it up to date and sorry for the long post :(

I can confirm that when I use sdm_modbus v0.5.2 and pymodbus v2.5.3 and I run the following code :

from gettext import install
import sdm_modbus
device = sdm_modbus.SDM630(device="/dev/ttyUSB0", stopbits=1, parity="N", baud=9600, timeout=1, retries=3, unit=1)
print(device)

if device.connected():
    print("Working")
else:
    print("NOT Working")

I get the following reply "NOT Working"

SDMModbus1

But as soon as I downgrade to sdm_modbus v0.4.3 and still use pymodbus v2.5.3 I get a "Working" response.

SDMModbus2

The main issue that I am currently experiencing on both SDM_Modbus versions is the below error when I add the below variable to example-rtu.py and change line 20 to meter = sdm_modbus.SDM630( instead of meter = sdm_modbus.SDM120( : device = sdm_modbus.SDM630(device="/dev/ttyUSB0", stopbits=1, parity="N", baud=9600, timeout=1, unit=1) and receive the response :

usage: test.py [-h] [--stopbits STOPBITS] [--parity {N,E,O}] [--baud BAUD] [--timeout TIMEOUT] [--unit UNIT] [--json] device
test.py: error: the following arguments are required: device

SDMModbus3

The full code :

#!/usr/bin/env python3

import argparse
import json
import sdm_modbus

device = sdm_modbus.SDM630(device="/dev/ttyUSB0", stopbits=1, parity="N", baud=9600, timeout=1, unit=1)

if __name__ == "__main__":
    argparser = argparse.ArgumentParser()
    argparser.add_argument('device', type=str, help="Modbus device")
    argparser.add_argument("--stopbits", type=int, default=1, help="Stop bits")
    argparser.add_argument("--parity", type=str, default="N", choices=["N", "E", "O"], help="Parity")
    argparser.add_argument("--baud", type=int, default=9600, help="Baud rate")
    argparser.add_argument("--timeout", type=int, default=1, help="Connection timeout")
    argparser.add_argument("--unit", type=int, default=1, help="Modbus unit")
    argparser.add_argument("--json", action="store_true", default=False, help="Output as JSON")
    args = argparser.parse_args()

    meter = sdm_modbus.SDM630(
        device=args.device,
        stopbits=args.stopbits,
        parity=args.parity,
        baud=args.baud,
        timeout=args.timeout,
        unit=args.unit
    )

    if args.json:
        print(json.dumps(meter.read_all(scaling=True), indent=4))
    else:
        print(f"{meter}:")
        print("\nInput Registers:")

        for k, v in meter.read_all(sdm_modbus.registerType.INPUT).items():
            address, length, rtype, dtype, vtype, label, fmt, batch, sf = meter.registers[k]

            if type(fmt) is list or type(fmt) is dict:
                print(f"\t{label}: {fmt[str(v)]}")
            elif vtype is float:
                print(f"\t{label}: {v:.2f}{fmt}")
            else:
                print(f"\t{label}: {v}{fmt}")

        print("\nHolding Registers:")

        for k, v in meter.read_all(sdm_modbus.registerType.HOLDING).items():
            address, length, rtype, dtype, vtype, label, fmt, batch, sf = meter.registers[k]

            if type(fmt) is list:
                print(f"\t{label}: {fmt[v]}")
            elif type(fmt) is dict:
                print(f"\t{label}: {fmt[str(v)]}")
            elif vtype is float:
                print(f"\t{label}: {v:.2f}{fmt}")
            else:
                print(f"\t{label}: {v}{fmt}")

I am using the Eastrom SDM630 Modbus nd I have confirmed that the configuration on he meter matches that on the script.

coenraadr avatar Jun 10 '22 22:06 coenraadr

Before I start, thank you for all the hard work that you have put into this ad keeping it up to date and sorry for the long post :(

Thanks, no worries, great that you included everything.

usage: test.py [-h] [--stopbits STOPBITS] [--parity {N,E,O}] [--baud BAUD] [--timeout TIMEOUT] [--unit UNIT] [--json] device
test.py: error: the following arguments are required: device

This error simply means you haven't provided /dev/ttyUSB0 to the script as a command line argument. I see you've defined device at the beginning of the script, but this will be done later on using the provided arguments. Remove line 7, and then eun the script again with a device path as a positional argument.

As to the reason why the lastest version doesn't work in the example code you used, still no clue. Have you tried doing this on a normal PC?

nmakel avatar Jun 11 '22 18:06 nmakel

Thank you @nmakel for the assist.

I can get all readings from my SDM630 now !!!!

My only issue is I am trying to get it to work on an ESP8266 on MicroPython and I am having allot of issues.

Will it work in MicroPython ?

coenraadr avatar Jun 17 '22 07:06 coenraadr

I am using in raspberry pi only.

⁣Obter o BlueMail para Android ​

Em 17/06/2022, 08:07, em 08:07, coenraadr @.***> escreveu:

Thank you @nmakel for the assist.

I can get all readings from my SDM630 now !!!!

My only issue is I am trying to get it to work on an ESP8266 on MicroPython and I am having allot of issues.

Will it work in MicroPython ?

-- Reply to this email directly or view it on GitHub: https://github.com/nmakel/sdm_modbus/issues/14#issuecomment-1158563806 You are receiving this because you authored the thread.

Message ID: @.***>

JorgeGoncalves1 avatar Jun 17 '22 08:06 JorgeGoncalves1

Will it work in MicroPython ?

No idea. Not tested. Besides possible performance issues on that SOC, you may have trouble with getting all of the required libraries working. By all means do let me know if you're succesful :+1:

nmakel avatar Jun 22 '22 19:06 nmakel

Still not working on raspberry por with last os.

Can you please send me a Python script?

⁣Obter o BlueMail para Android ​

Em 22/06/2022, 20:04, em 20:04, niels @.***> escreveu:

Will it work in MicroPython ?

No idea. Not tested. Besides possible performance issues on that SOC, you may have trouble with getting all of the required libraries working. By all means do let me know if you're succesful :+1:

-- Reply to this email directly or view it on GitHub: https://github.com/nmakel/sdm_modbus/issues/14#issuecomment-1163499051 You are receiving this because you authored the thread.

Message ID: @.***>

JorgeGoncalves1 avatar Jun 26 '22 08:06 JorgeGoncalves1

I have run the code on a raspberry.

Regards

⁣Obter o BlueMail para Android ​

Em 11/06/2022, 19:58, em 19:58, niels @.***> escreveu:

Before I start, thank you for all the hard work that you have put into this ad keeping it up to date and sorry for the long post :(

Thanks, no worries, great that you included everything.

usage: test.py [-h] [--stopbits STOPBITS] [--parity {N,E,O}] [--baud

BAUD] [--timeout TIMEOUT] [--unit UNIT] [--json] device

test.py: error: the following arguments are required: device

This error simply means you haven't provided /dev/ttyUSB0 to the script as a command line argument. I see you've defined device at the beginning of the script, but this will be done later on using the provided arguments. Remove line 7, and then eun the script again with a device path as a positional argument.

As to the reason why the lastest version doesn't work in the example code you used, still no clue. Have you tried doing this on a normal PC?

-- Reply to this email directly or view it on GitHub: https://github.com/nmakel/sdm_modbus/issues/14#issuecomment-1152981799 You are receiving this because you authored the thread.

Message ID: @.***>

JorgeGoncalves1 avatar Oct 11 '22 07:10 JorgeGoncalves1

It is well running on my fresh installation on a PI4B with 8GBram and the latest official Raspios 2022-09-22-raspios-bullseye-arm64.img.xz: $ uname -a Linux rpihaus 5.15.76-v8+ #1597 SMP PREEMPT Fri Nov 4 12:16:41 GMT 2022 aarch64 GNU/Linux $ python -V Python 3.9.2 $ pip3 -V pip 20.3.4 from /usr/lib/python3/dist-packages/pip (python 3.9) $ python ertu.py /dev/ttyUSB1 --stopbits 1 --parity N --baud 9600 --unit 1 SDM630(/dev/ttyUSB1, connectionType.RTU: stopbits=1, parity=N, baud=9600, timeout=1, retries=3, unit=0x1):

Input Registers: L1 Voltage: 222.81V L2 Voltage: 226.05V L3 Voltage: 222.93V L1 Current: 4.23A ...and lot more output lines.

lantasticus avatar Dec 10 '22 14:12 lantasticus