Python Forum
On error - go to the next task
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
On error - go to the next task
#1
There is a code that interrogates the device via modbus, in the code several devices are interrogated. When the device is turned off, there is no data, therefore an error is obtained and the program is closed, but the following devices can work. How to do that, if there is an error in the answer, then proceed to polling the next device?
Now I have done this:
request = client.read_holding_registers(62, 1, unit=0x01)
if request.isError():
    print('Modbus Error:', request)
else:
    result1 = request.registers
    written_as = result1[0] * 0.1
    print(f'{written_as:.1f}')
But this method is just going on to poll the next register. And upon reaching:
a1 =  float(result1[0] * 0.1)
    b1 =  float(row[2]) #row2- 1продукт
    silos13 = b1 - a1 - bool(b1 > a1) * 6553.5
    print ("Расход:"f'{silos13:.1f}')
I get:
Traceback (most recent call last):
  File "wl601.py", line 106, in <module>
    a1 =  float(result1[0] * 0.1)
NameError: name 'result1' is not defined
And the program closes
Reply
#2
you want to use a try and except block
something like:
try:
    a1 =  float(result1[0] * 0.1)
    b1 =  float(row[2]) #row2- 1продукт
    silos13 = b1 - a1 - bool(b1 > a1) * 6553.5
    print ("Расход:"f'{silos13:.1f}')
except NameError:
    # add exception code here.
Reply
#3
Thank you. The simplest and most important working solution. Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  count certain task in task manager[solved] kucingkembar 2 1,119 Aug-29-2022, 05:57 PM
Last Post: kucingkembar
  Schedule a task and render/ use the result of the task in any given time klllmmm 2 2,086 May-04-2021, 10:17 AM
Last Post: klllmmm
  How to create a task/import a task(task scheduler) using python Tyrel 7 3,711 Feb-11-2021, 11:45 AM
Last Post: Tyrel
  Windows 10 Task Scheduler Error mypython 1 2,484 Aug-11-2018, 11:01 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020