Modbus communication does not work - insufficient permission

Hello community,

I’m struggling since days to get a new power meter connected to OpenHAB running on my Raspi with ModBus binding. When activating the relevant things, I get in the log:
Could not get port identifier, maybe insufficient permissions.

To exclude HW or permission issues, I installed mbpoll on a second SD-Card after taking a fresh backup of the original and there the communication works. Meanwhile I’m lost and have no further clue to get it running. Any help would be highly appreciated.

My used setup:

  • Hardware: Raspberry Pi4 with an RS485 USB dongle
  • running serial communication on RS232 extention on GPIO14/15
  • OS: latest openHABian
  • Java openjdk version “11.0.12” (I did not change)
  • openHAB version: 3.3
  • SDM72DM-V2 power meter

Used thing configuration - no items defined so far:

Bridge modbus:serial:powermeter [ port="/dev/ttyUSB-RS485" ,id=1 , baud=9600, stopBits="1.0", parity="none", dataBits=8, encoding="rtu" ]{
    Bridge poller inputRegisters [ start=0, length=4, refresh=30000, type="input" ] {
        Thing data input1 [ readStart="0", readValueType="float32" ]
        Thing data input2 [ readStart="2", readValueType="float32" ]
    }
}

That’s the log I get after activating the things above:

2022-08-29 19:54:09.329 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'powermeter.things'
==> /var/log/openhab/events.log <==
2022-08-29 19:54:09.414 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:serial:powermeter' changed from ONLINE to OFFLINE
2022-08-29 19:54:09.417 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:serial:powermeter' changed from OFFLINE to ONLINE
2022-08-29 19:54:09.432 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:poller:powermeter:inputRegisters' changed from UNINITIALIZED to INITIALIZING
2022-08-29 19:54:09.447 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:poller:powermeter:inputRegisters' changed from INITIALIZING to ONLINE
2022-08-29 19:54:09.469 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:data:powermeter:inputRegisters:input1' changed from UNINITIALIZED to INITIALIZING
==> /var/log/openhab/openhab.log <==
2022-08-29 19:54:09.470 [WARN ] [ing.ModbusSlaveConnectionFactoryImpl] - Connect reached max tries 1, throwing last error: Could not get port identifier, maybe insufficient permissions. null. Connection SerialConnection [m_SerialPort=null, m_Parameters.getPortName()=/dev/ttyUSB-RS485]. Endpoint ModbusSerialSlaveEndpoint [getPortName()=/dev/ttyUSB-RS485]
2022-08-29 19:54:09.472 [WARN ] [ing.ModbusSlaveConnectionFactoryImpl] - Error connecting connection SerialConnection [m_SerialPort=null, m_Parameters.getPortName()=/dev/ttyUSB-RS485] for endpoint ModbusSerialSlaveEndpoint [getPortName()=/dev/ttyUSB-RS485]: Could not get port identifier, maybe insufficient permissions. null
2022-08-29 19:54:09.476 [WARN ] [rt.modbus.internal.ModbusManagerImpl] - Could not connect to endpoint ModbusSerialSlaveEndpoint [getPortName()=/dev/ttyUSB-RS485] -- aborting request ModbusReadRequestBlueprint [slaveId=1, functionCode=READ_INPUT_REGISTERS, start=0, length=4, maxTries=3] [operation ID d64221f2-0b36-45e2-adae-285ad155d2e8]
==> /var/log/openhab/events.log <==
2022-08-29 19:54:09.484 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:poller:powermeter:inputRegisters' changed from ONLINE to OFFLINE (COMMUNICATION_ERROR): Error with read: org.openhab.core.io.transport.modbus.exception.ModbusConnectionException: Error connecting to endpoint ModbusSerialSlaveEndpoint [getPortName()=/dev/ttyUSB-RS485]
2022-08-29 19:54:09.490 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:data:powermeter:inputRegisters:input1' changed from INITIALIZING to ONLINE
2022-08-29 19:54:09.494 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:data:powermeter:inputRegisters:input2' changed from UNINITIALIZED to INITIALIZING
2022-08-29 19:54:09.502 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'modbus:data:powermeter:inputRegisters:input2' changed from INITIALIZING to OFFLINE (BRIDGE_OFFLINE): No online bridge
==> /var/log/openhab/openhab.log <==
2022-08-29 19:54:15.955 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'powermeter.things'

As mentioned above, I installed mbpoll to check HW and permissions. Details for this tool can be found here if somebody needs it on his own ;-):
https://github.com/epsilonrt/mbpoll

When using mbpoll I get this:

openhabian@openhab:~ $ mbpoll -a 1 -b 9600 -P none -v -1 -t3 -c 2 -r 1 /dev/ttyUSB-RS485
debug enabled
Set function=3
Set number of values=2
Set start reference=1
Set mode to RTU for serial port
Set device=/dev/ttyUSB-RS485
mbpoll 1.4-12 - FieldTalk(tm) Modbus(R) Master Simulator
Copyright © 2015-2019 Pascal JEAN, https://github.com/epsilonrt/mbpoll
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions; type 'mbpoll -w' for details.

Opening /dev/ttyUSB-RS485 at 9600 bauds (N, 8, 1)
Set response timeout to 1 sec, 0 us
Protocol configuration: Modbus RTU
Slave configuration...: address = [1]
                        start reference = 1, count = 2
Communication.........: /dev/ttyUSB-RS485,       9600-8N1
                        t/o 1.00 s, poll rate 1000 ms
Data type.............: 16-bit register, input register table

-- Polling slave 1...
[01][04][00][00][00][02][71][CB]
Waiting for a confirmation...
<01><04><04><43><62><58><FB><35><9D>
[1]:    17250
[2]:    22779

From this test with mbpoll I assume:

  • HW and wiring is OK
  • Symlink is working fine with ttyUSB-RS485
  • configuration 9600 bauds (N, 8, 1) is correct for my power meter
  • sufficient privileges for openhabian to communicate with ttyUSB-RS485 should be given

And now as said above, I’m at the end of my wisdom :scream:

It’s not a very useful permissions check unless it is run under the same user. It looks like you tested under user openhabian? OH runs under user openhab.

Did you add EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/ttyUSB-RS485" to /etc/default/openhab as stated here Serial Port Configuration | openHAB

That’s right but if I did not mixed up everything since I’m absolutely not an linux expert, for me it’s running as openhabian. I think, that was the case in OH2.5 and I kept it. When using users list in the console I only get openhabian - see below. Is there another way to check?

openhab> users list
openhabian (administrator)
openhab>

Yes, ttyUSB-RS485 is there. Since I’m using ttyAMA the complete line is like this:

EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/ttyUSB-RS485:/dev/ttyUSB0:/dev/ttyAMA0"

Stupid me!

in the file there was another line right below with EXTRA_JAVA_OPTS which I did not enter and never changed. It seems, it was overwriting my entered line. Now after setting this line to commet, it seems to be working.

Do you know, what this line is doing and if it’s needed?

## added JuSt
EXTRA_JAVA_OPTS="-Dgnu.io.rxtx.SerialPorts=/dev/ttyUSB-RS485:/dev/ttyUSB0:/dev/ttyAMA0"
## added Ende

## EXTRA_JAVA_OPTS="-Xms192m -Xmx320m"

Anyhow, many thanks for your time and help!

openhabian is ‘just’ the user of the linux shell. openhabian has sudo privileges. That means for linux administration there is an easy way to become administrator/root. E.g. to execute openhabian-config you need sudo / root privileges because openhabian-config e.g. will install software packages etc.

openhabian is ( most probably ) listead as administrator because you added openhabian to be your administrator account for the UI / browser interface. openhabian is not administrator in the default distribution.

The user openhab is the user htat is used to run the software openhab under during runtime.
You can check that be running

ps -ef | grep [j]ava

in a shell.
The returned result will show all java processes e.g.

openhab    920     1  3 Aug06 ?        19:46:46 /usr/bin/java -XX:-UsePerfData -Dopenhab.home=/usr/share/openhab -Dopenhab.conf=/etc/openhab -Dopenhab.runtime=/usr/share/openhab/runtime -Dopenhab.userdata=/var/lib/openhab -Dopenhab.logdir=/var/log/openhab -Dfelix.cm.dir=/var/lib/openhab/config -Djava.library.path=/var/lib/openhab/tmp/lib -Djetty.host=0.0.0.0 -Djetty.http.compliance=RFC2616 -Dnashorn.args=--no-deprecation-warning -Dorg.apache.cxf.osgi.http.transport.disable=true -Dorg.ops4j.pax.web.listening.addresses=0.0.0.0 -Dorg.osgi.service.http.port=8080 -Dorg.osgi.service.http.port.secure=8443 -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Xms192m -Xmx320m -Dlog4j2.formatMsgNoLookups=true --add-reads=java.xml=java.logging --add-exports=java.base/org.apache.karaf.specs.locator=java.xml,ALL-UNNAMED --patch-module java.base=/usr/share/openhab/runtime/lib/endorsed/org.apache.karaf.specs.locator-4.3.7.jar --patch-module java.xml=/usr/share/openhab/runtime/lib/endorsed/org.apache.karaf.specs.java.xml-4.3.7.jar --add-opens java.base/java.security=ALL-UNNAMED --add-opens java.base/java.net=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED --add-opens java.base/java.util=ALL-UNNAMED --add-opens java.naming/javax.naming.spi=ALL-UNNAMED --add-opens java.rmi/sun.rmi.transport.tcp=ALL-UNNAMED --add-opens java.base/java.io=ALL-UNNAMED --add-opens java.base/java.lang.reflect=ALL-UNNAMED --add-opens java.base/java.text=ALL-UNNAMED --add-opens java.base/java.time=ALL-UNNAMED --add-opens java.desktop/java.awt.font=ALL-UNNAMED --add-exports=java.base/sun.net.www.protocol.file=ALL-UNNAMED --add-exports=java.base/sun.net.www.protocol.ftp=ALL-UNNAMED --add-exports=java.base/sun.net.www.protocol.http=ALL-UNNAMED --add-exports=java.base/sun.net.www.protocol.https=ALL-UNNAMED --add-exports=java.base/sun.net.www.protocol.jar=ALL-UNNAMED --add-exports=java.base/sun.net.www.content.text=ALL-UNNAMED --add-exports=jdk.xml.dom/org.w3c.dom.html=ALL-UNNAMED --add-exports=jdk.naming.rmi/com.sun.jndi.url.rmi=ALL-UNNAMED --add-exports=java.rmi/sun.rmi.registry=ALL-UNNAMED --add-exports=java.security.sasl/com.sun.security.sasl=ALL-UNNAMED -Dkaraf.instances=/var/lib/openhab/tmp/instances -Dkaraf.home=/usr/share/openhab/runtime -Dkaraf.base=/var/lib/openhab -Dkaraf.data=/var/lib/openhab -Dkaraf.etc=/var/lib/openhab/etc -Dkaraf.log=/var/log/openhab -Dkaraf.restart.jvm.supported=true -Djava.io.tmpdir=/var/lib/openhab/tmp -Djava.util.logging.config.file=/var/lib/openhab/etc/java.util.logging.properties -Dkaraf.startLocalConsole=false -Dkaraf.startRemoteShell=true -classpath /usr/share/openhab/runtime/lib/boot/org.apache.karaf.diagnostic.boot-4.3.7.jar:/usr/share/openhab/runtime/lib/boot/org.apache.karaf.jaas.boot-4.3.7.jar:/usr/share/openhab/runtime/lib/boot/org.apache.karaf.main-4.3.7.jar:/usr/share/openhab/runtime/lib/boot/org.apache.karaf.specs.activator-4.3.7.jar:/usr/share/openhab/runtime/lib/boot/osgi.core-7.0.0.jar:/usr/share/openhab/runtime/lib/jdk9plus/istack-commons-runtime-3.0.10.jar:/usr/share/openhab/runtime/lib/jdk9plus/jakarta.xml.bind-api-2.3.3.jar:/usr/share/openhab/runtime/lib/jdk9plus/javax.annotation-api-1.3.2.jar:/usr/share/openhab/runtime/lib/jdk9plus/jaxb-runtime-2.3.3.jar:/usr/share/openhab/runtime/lib/jdk9plus/org.apache.servicemix.specs.activation-api-1.2.1-1.2.1_3.jar:/usr/share/openhab/runtime/lib/jdk9plus/txw2-2.3.3.jar org.apache.karaf.main.Main

Within that returned result you also find all the options that are set to ‘configure’ the running java process. These options contain besides other your arguments that are set in /etc/default/openhab like the ports/devices to be used and also the Xms option.
E.g. java - What are the -Xms and -Xmx parameters when starting JVM? - Stack Overflow explains what they are used for.

Files in /etc/ shouldn’t / have not to be touched during installation of new packages resp. updates but in case an update of a package is being done normally you will see a message similar to ( this is my wording not the exact one being used ) “hey there is an update that will update a file that you modified/touched. how shall I continue ?”. Then there options like overwrite, keep the modified, have a look into the files and show the differences. It could be that during an update this file was modified.
In the meantime you edited the file again without doing that there would have been the options to check for the files last modification date timestamp. That would have given a clue about when that happened.

Many thanks again for your response. Rossko and you are right, OH is running under user openhab.

This linux rights management is still confusing and using the same name/identifier for different things as done by me is also not really helpful. I have to take more care on this when doing a new setup.

Checking with my second SD shows that /etc/default/openhab was changed when I started with Modbus. So I have most likely entered my modifications on the wrong place and they never got active. Now the serial port and memory modifications are running at the same time and I get the data from my power meter.

1 Like