danjperron / PicMultiPurpose

Small PIC cpu using RS-485 with configurable IO
3 stars 2 forks source link

Problem with Modbus Comunication #1

Closed rosvig closed 7 years ago

rosvig commented 7 years ago

Hi Daniel i'm doing your project "PicMultipurpose" but i have a problem on the Step 4 "Cpu Verification". I assembled the circuit according to the schematic you have posted. On the Step 1 i do the setup the raspberry pi, then i program the PIC and burner respond that was programmed correctly. Check the result:

pi@labpi:~ $ sudo python burnLVP/burnLVPx.py PicMultiPurpose/PicMultiPurposeV1.0.hex
Raspberry Pi GPIO
File " PicMultiPurpose/PicMultiPurposeV1.0.hex " loaded
Scan CPU
Check PIC12/16...
Cpu Id   = 0x0
Revision =  0x5
Found  PIC12F1840 from Cpu Family  PIC12/16
Cpu Id: 0x1b80  revision: 5
Bulk Erase Program , Data. .... done.
Program blank check................................Passed!
Data Blank check........Passed!
Writing Program................................Done.
Program check ................................Passed!
Writing Data.Done.
Data check .Passed!
Writing Config..Done.
Config Check..Passed!
Program verification passed!
pi@labpi:~ $

On the Step 4 (CPU Verification) i used your code TestSlave127.py connecting the Pic with raspberry as the schematic (Baudarate= 57600) (register=160) (Slave=127) and i had this result:

pi@labpi:~ $ python
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import minimalmodbus
>>> instrument = minimalmodbus.Instrument('/dev/ttyAMA0',127)
>>> instrument.serial.baudrate=57600
>>> instrument.read_register(160,0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 258, in read_register
    return self._genericCommand(functioncode, registeraddress, numberOfDecimals=numberOfDecimals, signed=signed)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 697, in _genericCommand
    payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 795, in _performCommand
    response = self._communicate(request, number_of_bytes_to_read)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 930, in _communicate
    raise IOError('No communication with the instrument (no answer)')
IOError: No communication with the instrument (no answer)

The communication not work 😢 i I don't understand what the problem might be Do you have some suggestion for i can understand what's wrong?

Thank you 👍

danjperron commented 7 years ago

What is your Raspberry Pi ? If it is a RPi3 then the serial port is not ttyAMA0 but ttyS0. On the newest OS you could use serial0 which is a link to the correct port no matter what is your Pi.

Simply replace /dev/ttyAMA0 to /dev/serial0

P.S. don't forget to disable the debug console from the serial port and enable the serial port!

Le 6 juil. 2017 à 05:07, rosvig notifications@github.com a écrit :

Hi Daniel i'm doing your project "PicMultipurpose" but i have a problem on the Step 4 "Cpu Verification". I assembled the circuit according to the schematic you have posted. On the Step 1 i do the setup the raspberry pi, then i program the PIC and burner respond that was programmed correctly. Check the result:

pi@labpi:~ $ sudo python burnLVP/burnLVPx.py PicMultiPurpose/PicMultiPurposeV1.0.hex Raspberry Pi GPIO File " PicMultiPurpose/PicMultiPurposeV1.0.hex " loaded Scan CPU Check PIC12/16... Cpu Id = 0x0 Revision = 0x5 Found PIC12F1840 from Cpu Family PIC12/16 Cpu Id: 0x1b80 revision: 5 Bulk Erase Program , Data. .... done. Program blank check................................Passed! Data Blank check........Passed! Writing Program................................Done. Program check ................................Passed! Writing Data.Done. Data check .Passed! Writing Config..Done. Config Check..Passed! Program verification passed! pi@labpi:~ $ On the Step 4 (CPU Verification) i used your code TestSlave127.py connecting the Pic with raspberry as the schematic (Baudarate= 57600) (register=160) (Slave=127) and i had this result:

pi@labpi:~ $ python Python 2.7.9 (default, Sep 17 2016, 20:26:04) [GCC 4.9.2] on linux2 Type "help", "copyright", "credits" or "license" for more information.

import minimalmodbus instrument = minimalmodbus.Instrument('/dev/ttyAMA0',127) instrument.serial.baudrate=57600 instrument.read_register(160,0) Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 258, in read_register return self._genericCommand(functioncode, registeraddress, numberOfDecimals=numberOfDecimals, signed=signed) File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 697, in _genericCommand payloadFromSlave = self._performCommand(functioncode, payloadToSlave) File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 795, in _performCommand response = self._communicate(request, number_of_bytes_to_read) File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 930, in _communicate raise IOError('No communication with the instrument (no answer)') IOError: No communication with the instrument (no answer) The communication not work 😢 i I don't understand what the problem might be Do you have some suggestion for i can understand what's wrong?

Thank you 👍

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/danjperron/PicMultiPurpose/issues/1, or mute the thread https://github.com/notifications/unsubscribe-auth/AEJf3EDFbn5MgpgIEo6piYglkOgO9MA0ks5sLKPHgaJpZM4OPWzZ.

danjperron commented 7 years ago

Also I assumed that you are reading my document on google drive

https://docs.google.com/document/d/1NexyDgWcd7yxAVQS2JbSQji1RmOajrBP0WLHMTnVKnw/edit?usp=sharing

Another possibility is to check the Raspberry Pi communication using minicom. At least you will know if the raspberry Pi is correctly set.

1 - First install minicom sudo apt-get update sudo apt-get install minicom

2- Connect the TX and RX pin of the raspberry Pi together (no PIC)

3- Start minicom with the correct port and baud rate sudo minicom -D /dev/serial0 -b 57600

4 - everything you type should be echo on the screen. If not something is wrong.

5- To quit just do [ctrl]-A Z , Q for quit and Y for yes

danjperron commented 7 years ago

Also my PicModule.py module assume 115200 baud with /dev/ttyAMA0

I will need to check it if it is really 115200 or 57600 baud

Please change the line to def init(self,SlaveAddress,Baud=57600,Device='/dev/serial0'):

Not sure about 115200 or 57600. I will need to verify it but I will be away this weekend. Going 3 degree up north, Montréal to Amos. I will check it Monday on my return.

B.T.W. If you don't have /dev/serial0 then you set the serial port wrong.

rosvig commented 7 years ago

Daniel thank you for your time and your help but i did different test, and checked the code, was wrongly connecting Rx and Tx, Now the step 4 is working, and now i will continue to test the complet circuit. I will let you know how this test goes. 😄

rosvig commented 7 years ago

Modbus communication is working 👍 I connected the sensor DS18B20 and i'm trying to read it. I proved to read with register 0 and 1 but the answers is always 0. Is there something that i have do before to read the sensor? or maybe i'm reading the wrong registers?

danjperron commented 7 years ago

Did you config the module to read the ds18b20.

please compile ConfigPic.c

gcc -I /usr/include/modbus configPIC.c -o configPIC -l modbus

And set the correct pin configuration.

./configPic -d /dev/serial0 -b 57600

P.S. you need to add a 4k7 pull up resistor for the DS18B20. I hope that you don't have a parasitic version (end with a P).

Please check my google drive document. It is one of the post that I sent you.

danjperron commented 7 years ago

Also the register to read sensor aren't 0 and one

For the DS18B20 is a read long self.module.read_long(Pin*16,4,False) check Function4 in main.c line 92 (sorry it is in french).

Please check PicModule.py to read the DS18B20.

I think that my factor is wrong to get the correct temperature if it's not on 12bits.

I did fix it on my MultiIO10 version. factor should always be 0.0625.

Ok I fix the bug in PicModule about the factor and change the default for PicModule.py to be 57600 baud and /dev/serial0

danjperron commented 7 years ago

oops to compile configPic.c you need to install some libraries

sudo apt-get install libmodbus5 libmodbus-dev

rosvig commented 7 years ago

Hi Daniel, i hope you are enjoying your trip :+1: I puted the resistor of 4,7K on the sensor DS18B20 but don't yet to read the register of the temperature. I will want to do the configuration and the read of manually sensor with modbus command, without using any program I writed 32 on the register 256 and 257, i tried to read the register 0 but always i read 0 and the register 16 but i didn't have no answer. Could you tell me which register i can get the value of temperature, please?

Thank you.

danjperron commented 7 years ago

Well it is function3  read holding register . I can't verify right now but check my picmodule.py on the readDS18B20 function. In minimal modbus it is a read_registers.read_registers(Pin*16,3,4) I just have my tablet link to my phone  so i can't help you more .

Sent from my Galaxy Tab® S2 -------- Message d'origine --------De : rosvig notifications@github.com Date : 17-07-07 1:11 PM (GMT-05:00) À : danjperron/PicMultiPurpose PicMultiPurpose@noreply.github.com Cc : danjperron danjperron@videotron.ca, Comment comment@noreply.github.com Objet : Re: [danjperron/PicMultiPurpose] Problem with Modbus Comunication (#1) Hi Daniel, i hope you are enjoying your trip 👍

I puted the resistor of 4,7K on the sensor DS18B20 but don't yet to read the register of the temperature.

I will want to do the configuration and the read of manually sensor with modbus command, without using any program

I writed 32 on the register 256 and 257, i tried to read the register 0 but always i read 0 and the register 16 but i didn't have no answer.

Could you tell me which register i can get the value of temperature, please? Thank you.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/danjperron/PicMultiPurpose","title":"danjperron/PicMultiPurpose","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/danjperron/PicMultiPurpose"}},"updates":{"snippets":[{"icon":"PERSON","message":"@rosvig in #1: Hi Daniel, i hope you are enjoying your trip :+1: \r\nI puted the resistor of 4,7K on the sensor DS18B20 but don't yet to read the register of the temperature.\r\nI will want to do the configuration and the read of manually sensor with modbus command, without using any program\r\nI writed 32 on the register 256 and 257, i tried to read the register 0 but always i read 0 and the register 16 but i didn't have no answer.\r\nCould you tell me which register i can get the value of temperature, please?\r\n\r\nThank you."}],"action":{"name":"View Issue","url":"https://github.com/danjperron/PicMultiPurpose/issues/1#issuecomment-313740171"}}}

danjperron commented 7 years ago

Ok First try to set it up using configPIC. I don't see why you want to change address on the fly.

I know that you want modbus command but it's so easy with my PicModule

>>> import PicModule
>>> m1=PicModule.PicMbus(127,Baud=57600,Device='/dev/serial0')
>>> m1.readConfig(0)
0
>>> m1.config(0,m1.IOCONFIG_DS18B20)
>>> m1.readDS18B20(0)
24.75

I assume that you didn't change the module address. If you want to change the module address with only minimalmodbus this is what you need to do.

Holding register 160 hold the modbus slave address. To write a new value use the preset register function 6.

>>> import minimalmodbus
>>> m1 = minimalmodbus.Instrument('/dev/serial0',127)
>>> m1.serial.baudrate=57600
>>> m1.serial.timeout=0.02
>>> m1.serial.flushInput()
>>> m1.write_register(160,1,0,6)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 296, in write_register
    self._genericCommand(functioncode, registeraddress, value, numberOfDecimals, signed=signed)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 697, in _genericCommand
    payloadFromSlave = self._performCommand(functioncode, payloadToSlave)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 798, in _performCommand
    payloadFromSlave = _extractPayload(response, self.address, self.mode, functioncode)
  File "/usr/local/lib/python2.7/dist-packages/minimalmodbus.py", line 1082, in _extractPayload
    responseaddress, slaveaddress, response))

**** it is normal that you have an error since the address is not 127 but it is now 1!

And to read the sensor read_registers function4 . You need to read 3 registers.

>>> m1=minimalmodbus.Instrument('/dev/serial0',1)
>>> m1.serial.baudrate=57600
>>> m1.serial.timeout=0.02
>>> config0 = m1.read_register(0x100,0)
>>> if config0 != 32 :
...    m1.write_register(0x100,32,0,6)
...    time.sleep(5)
... 
>>> sensordata = m1.read_registers(0,3,4)
>>> sensordata
[1, 392, 127]
>>> if sensordata[0] != 1 :
...   print("error reading temperature")
... else:
...   if (sensordata[1]& 0x8000)==0: 
...      print("temperature is {}".format(sensordata[1] * 0.0625))
...   else:
...      print("temperature is {}".format((sensordata[1] - 65536) * 0.0625))
... 
temperature is 24.5

Also I added the check for the config. If IO0 is not 32 (IOCONFIG_DS18B20) I just set it up.

P.S. PicModule.py needs to be in the same directory of your main program.

ondrej1024 commented 7 years ago

Hi Daniel, I'm working with @rosvig on this project and I would like to provide you with some background information. We are developing a Modbus T/H sensor for the Telegea project. Your PicMultiPurpose project is an excellent starting point for that. We have built a prototype based on your design. The idea is to reuse most of the Modbus PIC code but to replace the sensor handling code. We want to use the SHT21 sensor which has an I2C interface. The PIC IO lines IO0 and IO1 will be connected to the sensors DAT and CLK lines. We plant to port the sensor code from this project which uses a PIC16F84A which should be very similar.

ondrej1024 commented 7 years ago

Thanks to your information we are now able to read the DS18B20 data from the PIC module with our Modbus master test application (which uses libmodbus). It wasn't clear that we needed to read 3 bytes, but now it is working.

danjperron commented 7 years ago

Then check https://github.com/danjperron/PIC_MULTI_10_IO

This one is based on PIC16F1827 which has more I/O and a less complicated i2C. Maybe you should be able to configure the i2c on that version. Also the document is better. https://docs.google.com/document/d/1lXswQlaUgRb-156ZBXZssPgz_-Iv_EBm5P-c_YVQfOA/edit?usp=sharing