How to Read Modbus Data with Python - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hello everybody, I bought this modbus 485  temperature and humidity sensor last week,   I want to show you how you can use the Chipsee  industrial panel PC to control this little thing.   And I will show you how to read the data, and  then, in the next video let's see how to build   a graphical user interface in ReactJS to display  those data, the temperature changes, in real time.   Today we will focus on reading data from this  sensor and with the help of our Chipsee panel PC. You probably have heard about Modbus protocol,  but maybe never used it, but that's OK, I didn't   know that either when I first got this sensor,  luckily Python has a library called pymodbus,   and when you buy the sensor, you can  always get a datasheet from the seller,   so those two things should get you up and running  quickly. I'll show you how to wire these things,   and I've picked the most important information  from the datasheet. I'll explain some concepts I   think are very crucial to use this sensor,  but I won't get into too much detail. So when we look at this sensor, we can see it  has four ports, this one accepts a 5 to 30 volts   power input, this is positive input and this is  negative input, and our Chipsee device also has   a 5 volts VCC and GND, so we can use our Chipsee  PC to power this sensor. And when I look around,   it also has this RS485 ports, so does this  485 ports need to have a shared ground   as this DC input ports, like the RS232 which needs  three wires and one of them is the ground. But   when I think about this, I also know that RS485  is actually a differential signal, which means   485 signals do not enforce a common ground,  so we know this DC input is only power input,   and it does not play any role in the signals it  transmits. So the conclusion is we can connect   the DC input to our Chipsee PC's VCC and GND, and  we do not need to worry about sharing the ground. Then that's two ports, we have another two  ports we need to connect, it's labeled A   plus and B minus, so how do we connect them to  our Chipsee PC? When we look at our Chipsee PC,   there are 4 RS485 labels, the 5- 5+, the 3- and  3+, so which ones do we use? These are actually   two RS485 ports, and the 5+ and 5- is one port,  the 3+ and 3- is another port. So we can pick   any one of them, I picked the three's, you can  pick whatever you want, and I'll show you the   difference in the software program later, only one  variable needs to change if you picked the five's. So, I've used a screwdriver to connect the  dupont wires. And I often use a multimeter   to test the wires before I dive into  programming, because there was one time   when one of the wires is not connected properly,  and I debugged the code for a really long time,   and later I found it's not the program that's  buggy but the hardware. Lessons learned hard. And we can see there is a little red  LED, which means it's powered on,   it also means the Chipsee PC is  providing enough power to the sensor. Now let's see how to program and read  temperature data. We start with a Python   virtual environment to avoid messing up  with our other Python packages. The first   step is to install the pymodbus library,  and pymodbus uses pyserial under the hood,   since our sensor is modbus rtu and uses RS485, so  we also need to install pyserial, there is also   modbus TCP of course but that's not our topic  for today. If you're interested in learning   these things you can subscribe to this youtube  channel, and I'll get to that in the future. When we write pymodbus programs, one  very important technique is logging.   There is a code snippet I use to log  the modbus communication details.   I'll comment out them first and I'll  show you later why it's very useful. Let's import the modbus client first, with  from pymodbus.client import ModbusSerialClient.   Then we initialize the serial client, with  client=ModbusSerialClient(port=, timeout=,   baudrate=, bytesize=, parity=, stopbits=), then  we connect to the sensor with client.connect(). This is the time we go to the hardware  documentation, this port argument is which   serial port our Chipsee PC you are using, let's  go to Chipsee's software documentation and find   out what it is, we are using the 5 inch product  of Chipsee RK3568 line, in the table we can see   there is a RS485_3, this name matches the printed  name on the metal case, so this is a printed name,   in the same table we can see it has a node name,  the node name is what the operating system calls   this 485 port, it's /dev/ttyS3, and this node  name is also what we will put inside the Python   function call. So the port here, we will fill  "/dev/ttyS3". If you did not use the RS485_3,   instead you used RS485_5, you can take a look at  the table, and find that the _5 name corresponds   to the node name /dev/ttyS4, so you will need to  fill the Python parameter with S4, instead of S3. OK, then that's it about our Chipsee document,  in this client initialization function,   we have another 5 parameters to fill, the  timeout, I usually use 1 or 2 seconds,   because we're using the serial port, which is  usually very stable, unlike TCP which sometimes   has some latency, so I will fill 2 seconds  here. Then we have 4 more parameters to fill. These four parameters describe how our Chipsee  PC and the sensor talk to each other. Well,   we can program our Chipsee PC, but we  don't quite know how to program that   sensor. It's best to instruct our Chipsee PC  to talk the language of that sensor. Let's   read the sensor's datasheet and see what is  the accent it speaks or what is the protocol.   This datasheet is written by a Chinese  company. The English is not very accurate,   so I picked the most important parts I think  we will need to know in an Excel sheet. The manufacturer of the sensor says, the sensor  supports Modbus RTU which means our RS485 port,   and the format is 8 data bits, no parity, 1  stop bit. This is a pretty common protocol,   also people call it 8-N-1, where N means  no. And the baudrate by default is 9600.   These are the 4 values we will need to fill in  that function call. So let's just fill them in,   baudrate is 9600, bytesize is 8, parity  is "N", and stop bit is 1. If you're   using another modbus device, you can often  find similar information on your datasheet.   And this is what the device I bought  needs to communicate with my Chipsee PC. OK now we have initialized the modbus  rtu client, let's start the connection   which means opening the RS485 port with  client.connect(), it returns a boolean true,   which means we have successfully connected the  Chipsee RS485 port to the sensor's RS485 port. So the next step is to read data from the sensor,   and this is also the most confusing step  if you have never used a Modbus device.   So let's get back to our sensor's datasheet, and  see if we can infer what command we need to send   from our Chipsee PC to let the sensor  to let it return the temperature data. OK the datasheet says it supports Modbus function  code 03, and 06, and what the hack is that, I   don't know, but we can google the Modbus function  code, here is a table I found on the Internet, we   can see the code 3 means read holding registers,  again what the hack is read holding register,   we don't know that either, but if it says read, we  can assume we can get some data if we are reading   something, maybe that's the temperature value  that's stored in those holding registers. And   except for read holding registers, Modbus also  has codes for read coils, read input registers,   read contacts. I don't know what exactly those  mean but maybe the Python pymodbus library knows,   so let's check the pymodbus document to  see how we can read the holding registers. Luckily, pymodbus does have a function  right after the name read_holding_registers,   it takes three arguments, the first is  address, the second is count, the third   is slave which is an integer. And the doc also  says its code is 0x03 which is a hexadecimal 3.   Now we are left with three parameters we need  to fill, let's guess what they are. The slave   is Modbus slave ID, and back to our sensor  datasheet, it says the address is 1~254,   default address is 1, so maybe that's the slave  id of this sensor, so we fill this slave with 1. Now we're left with two arguments to fill,  the first is address, the second is count,   the document says address is the start address  to read from, if we go back to our datasheet,   you can see these blue cells, 00, and 00, the  manufacturer says if you want to read temperature,   the master, which refers to our Chipsee PC or our  Python program, needs to send these values to the   sensor, so there is also a start address to read  from, but it has two values, one is high byte,   and the other is low byte. It's a little bit  confusing, there are two values that all claim it   is the start address to read from, but which one  should I use to fill that Python function call?   I don't know for sure, but good thing is they are  all zeroes, if I choose the high byte, it's zero,   if I choose the low byte, it's still zero if  I combine these two bytes, which is 16 bits,   that also a word in computer science, so my first  instinct would be to just fill a zero in that   function call. And in fact, the correct way is  to combine the two 00s, and it becomes a word,   which means 0x0000, that's a hexadecimal in  turn that gives us a zero in decimal world.
Info
Channel: Chipsee
Views: 3,684
Rating: undefined out of 5
Keywords: modbus, pymodbus, Chipsee
Id: iSrZv7BxjJs
Channel Id: undefined
Length: 11min 33sec (693 seconds)
Published: Thu Aug 31 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.