PIC as MODBUS slave
Moderator: Benj
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
PIC as MODBUS slave
Hello ,
I've a problem with a flowcode7 component for modbus slave. I use a pic as modbus slave to control some I/O pins and an LCD to display characters that send from a modbus master (I'm playing with ComTest Pro as modbus master emulator).The test code works fine with some modbus commands as SetCoilState(05),ReadHoldingRegister(03), but not for SetHoldingRegister(06).When I send any number or character from modbus master, the LCD shows HEX 0x00 and DECIMAL 16640.
I don't know if there is a problem with code or with flowcode component. I would like to know how to transfer pic's register data to LCD , what is the type of variable that
i must use to assign with LCD component input .Thanks.
I've a problem with a flowcode7 component for modbus slave. I use a pic as modbus slave to control some I/O pins and an LCD to display characters that send from a modbus master (I'm playing with ComTest Pro as modbus master emulator).The test code works fine with some modbus commands as SetCoilState(05),ReadHoldingRegister(03), but not for SetHoldingRegister(06).When I send any number or character from modbus master, the LCD shows HEX 0x00 and DECIMAL 16640.
I don't know if there is a problem with code or with flowcode component. I would like to know how to transfer pic's register data to LCD , what is the type of variable that
i must use to assign with LCD component input .Thanks.
- Attachments
-
- ModBus_Slave_DEMO4.fcfx
- (17.67 KiB) Downloaded 500 times
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hi SILVESTROS9,
To send a PIC register to LCD you first need to create a variable or any name e.g
Then using a C code Block use format
Even though variable called RegisterContents only has the 1st & 8th letters as upper case, when used with a c code block all the letters must be upper case.
Also the register name has to be upper case as well.
To send the register to LCD you will use PrintNumber and place the variable in the expression box.
Martin
To send a PIC register to LCD you first need to create a variable or any name e.g
Code: Select all
RegisterContents
Code: Select all
FCV_REGISTERCONTENTS = OSCCON;
Also the register name has to be upper case as well.
To send the register to LCD you will use PrintNumber and place the
Code: Select all
RegisterContents
Martin
Martin
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
PROBLEM WITH FLOWCODE7 MAIN PAGE
Hi medelec35,
Many thanks for info, I'll make some tests later, but for now I've a problem with flowcode7 start page. When I load flowcode7, there is no main toolbar and components toolbar, only a small toolbar with FILE,VIEW,HELP. When I load a project this toolbar disappears and there is only one toolbar with some icons of basic operations and with no components toolbar. unfortunately , I can't work with that start page. I tried to make some changes , with no result. Any idea on how to restore the initial main page with all toolbars?
thanks.
Many thanks for info, I'll make some tests later, but for now I've a problem with flowcode7 start page. When I load flowcode7, there is no main toolbar and components toolbar, only a small toolbar with FILE,VIEW,HELP. When I load a project this toolbar disappears and there is only one toolbar with some icons of basic operations and with no components toolbar. unfortunately , I can't work with that start page. I tried to make some changes , with no result. Any idea on how to restore the initial main page with all toolbars?
thanks.
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: PIC AS MODBUS SLAVE
Yes there is a way of restoring all the components back to when you first ran Flowcode.
It's Deleting something in the registry which I would like to be the last resort.
First if you go to View menu, is components Toolbox enabled?
If so, can you take a screen shot of Flowcode.
It's Deleting something in the registry which I would like to be the last resort.
First if you go to View menu, is components Toolbox enabled?
If so, can you take a screen shot of Flowcode.
Martin
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello medelec35,
Thanks for reply. I send you 2 screen shots , one for fc7 start page , and another when I load a project.
In your opinion, what's the reason for that , and how can I prevent this?
Thanks.
Thanks for reply. I send you 2 screen shots , one for fc7 start page , and another when I load a project.
In your opinion, what's the reason for that , and how can I prevent this?
Thanks.
- Attachments
-
- FC7 LOAD PROJECT.png
- (160.09 KiB) Downloaded 3077 times
-
- FC7 START.png
- (196.76 KiB) Downloaded 3077 times
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: PIC AS MODBUS SLAVE
Perhaps best to start all windows from scratch.
First close Flowcode V7
Then run registration editor.
Normally just select PC run box then enter regedit
Select HKEY_CURRENT_USER\SOFTWARE\
Delete MatrixTSL key.
Restart Flowcode.
Note: When starting a new flowchart and only Start page can be seen then will not be able to see components until target device is chosen.
So the lower screen shoot looks correct.
Martin
First close Flowcode V7
Then run registration editor.
Normally just select PC run box then enter regedit
Select HKEY_CURRENT_USER\SOFTWARE\
Delete MatrixTSL key.
Restart Flowcode.
Note: When starting a new flowchart and only Start page can be seen then will not be able to see components until target device is chosen.
So the lower screen shoot looks correct.
Martin
Martin
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello medelec35,
OK for flowcode7 start page.Thanks. About LCD with pic, I made some tests with FCV variables but get errors in compiling. Is there some definition that I must enter at supplementary code area , or only enter FCV_VAR under c code icon ? Also, because I would like to send characters to LCD , not only numbers may I use PrintAscii ?
Anyway , so far, I've 0 or 0x00 at LCD, so I think that the problem is at addressing PIC16F887 registers.
OK for flowcode7 start page.Thanks. About LCD with pic, I made some tests with FCV variables but get errors in compiling. Is there some definition that I must enter at supplementary code area , or only enter FCV_VAR under c code icon ? Also, because I would like to send characters to LCD , not only numbers may I use PrintAscii ?
Anyway , so far, I've 0 or 0x00 at LCD, so I think that the problem is at addressing PIC16F887 registers.
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello medelec35,
I send you the test code for your opinion.
Many thanks.
I send you the test code for your opinion.
Many thanks.
- Attachments
-
- ModBus_Slave_DEMO4.fcfx
- (17.18 KiB) Downloaded 435 times
-
- Matrix Staff
- Posts: 9520
- Joined: Sat May 05, 2007 2:27 pm
- Location: Northamptonshire, UK
- Has thanked: 2585 times
- Been thanked: 3815 times
- Contact:
Re: PIC AS MODBUS SLAVE
You have got
the variable after FCV_ must be all in upper case, and you must finish with a semicolon.
Also it has to be a register name within a pic which is what you was asking to retrieve.
For example with your device, register names are: Perhaps its not PIC register your after and just normal variables from the Array name?
Code: Select all
FCV_Array
Also it has to be a register name within a pic which is what you was asking to retrieve.
For example with your device, register names are: Perhaps its not PIC register your after and just normal variables from the Array name?
Martin
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello medelec35,
you've right about pic registers. According Modbus protocol, for command 06(write single register) the data that send the master consist of 2x4 Bits HEX (0x00 low , 0x00 high).With MB cmd 06, flowcode7 writes that data to registers 40001 to 49999 with data addresses 0000-270E. Where in memory banks are that registers and how can we have access , to assign with a var. and send that to LCD? I don't know how flowcode7 works in read/write data to pic registers according to modbus command from master, or if make a typical modbus register map at pic's memory . The string that send the master with command 06 is below:
we send data 0A35 to slave address 01 , to writes register address 02 (MB cmd 06)
string to send 01 06 00 02 0A 35 XX XX ( XX XX =CRC CODE).
Flowcode translate- I suppose - register code 00 02 to holding register number in the area 40001-49999 calculating an offset 40001. and writes the data. The key is in what register of pic stores the data. Is a buffer with HEX address code? I think that developers of flowcode must be know far more about that . So ,in my opinion ,the only way to see characters at LCD is to have the above info.
you've right about pic registers. According Modbus protocol, for command 06(write single register) the data that send the master consist of 2x4 Bits HEX (0x00 low , 0x00 high).With MB cmd 06, flowcode7 writes that data to registers 40001 to 49999 with data addresses 0000-270E. Where in memory banks are that registers and how can we have access , to assign with a var. and send that to LCD? I don't know how flowcode7 works in read/write data to pic registers according to modbus command from master, or if make a typical modbus register map at pic's memory . The string that send the master with command 06 is below:
we send data 0A35 to slave address 01 , to writes register address 02 (MB cmd 06)
string to send 01 06 00 02 0A 35 XX XX ( XX XX =CRC CODE).
Flowcode translate- I suppose - register code 00 02 to holding register number in the area 40001-49999 calculating an offset 40001. and writes the data. The key is in what register of pic stores the data. Is a buffer with HEX address code? I think that developers of flowcode must be know far more about that . So ,in my opinion ,the only way to see characters at LCD is to have the above info.
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello to all,
It's very strange that it's so difficult to display some characters to an LCD. I made many tests with code that I send below, everything works fine. except of one component macro : ModbusSlave1::SetHoldingRegister(0,pic_reg). For anything data that I send from master , LCD always shows 0x00 . I set several values to pic_reg ,to several forms, I'm playing with some calculations from a form to another , all of that was displayed fine at LCD. It seems that pic_reg holds no data. This var. is normally
a UINT 0-65535. The transfer of data (Rx + Tx ) to pic is OK . Maybe it's not transferred to pic_reg , because there is a problem to function SetHoldindRegister of component ModbusSlave. How can check this to solve this problem? Is it possible to ask the flowcode developers to compose an example for that case as the example for Modbus Slave ,and post it at the forum as help for all of us who want to play with that type of communication?
Thanks.
It's very strange that it's so difficult to display some characters to an LCD. I made many tests with code that I send below, everything works fine. except of one component macro : ModbusSlave1::SetHoldingRegister(0,pic_reg). For anything data that I send from master , LCD always shows 0x00 . I set several values to pic_reg ,to several forms, I'm playing with some calculations from a form to another , all of that was displayed fine at LCD. It seems that pic_reg holds no data. This var. is normally
a UINT 0-65535. The transfer of data (Rx + Tx ) to pic is OK . Maybe it's not transferred to pic_reg , because there is a problem to function SetHoldindRegister of component ModbusSlave. How can check this to solve this problem? Is it possible to ask the flowcode developers to compose an example for that case as the example for Modbus Slave ,and post it at the forum as help for all of us who want to play with that type of communication?
Thanks.
- Attachments
-
- ModBus_Slave_DEMO5.fcfx
- (15.14 KiB) Downloaded 324 times
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello,
In your program you are writing an uninitialised variable pic_reg to holding register 0. You then convert the variable to a string and print to the LCD. So at no point do you read the value passed in from the master.
Here is hopefully a better program that will take the values in the holding registers 0-19 and print to the first line of the LCD.
In your program you are writing an uninitialised variable pic_reg to holding register 0. You then convert the variable to a string and print to the LCD. So at no point do you read the value passed in from the master.
Here is hopefully a better program that will take the values in the holding registers 0-19 and print to the first line of the LCD.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello Benj,
Happy New Year to you and to Matrix stuff .
I would like to thank you for support. For us who we are new in programming, it's not easy to learn quickly the FLOWCODE neither to avoid mistakes , but we continue to try . The new code works better , I think it's a good start for some projects that I would like to make.
Happy New Year to you and to Matrix stuff .
I would like to thank you for support. For us who we are new in programming, it's not easy to learn quickly the FLOWCODE neither to avoid mistakes , but we continue to try . The new code works better , I think it's a good start for some projects that I would like to make.
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello Benj,
The code Modbus_Slave_LCD works fine for the first line (0). I was added the code for lines 1,2,3 but characters not displayed at these lines. Ι noticed with the help of a master emulator, that only 20 registers can read with a Read Holding Register command. If that's true , what is that must change at code for lines 1,2,3 ? Would you like to give me an example for 4 lines of LCD?
The code Modbus_Slave_LCD works fine for the first line (0). I was added the code for lines 1,2,3 but characters not displayed at these lines. Ι noticed with the help of a master emulator, that only 20 registers can read with a Read Holding Register command. If that's true , what is that must change at code for lines 1,2,3 ? Would you like to give me an example for 4 lines of LCD?
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello,
This should now allow all four LCD lines to be controlled.
This should now allow all four LCD lines to be controlled.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello Benj,
Thanks for reply . When compiling, I receive an error : 197:(1250) could not find space (160 bytes) for variable_FCV_04c01_ModbusSlave1_REGS.
Thanks for reply . When compiling, I receive an error : 197:(1250) could not find space (160 bytes) for variable_FCV_04c01_ModbusSlave1_REGS.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello,
Sounds like the 80 x 16-bit modbus registers cannot fit into your devices RAM, the eaisest option is to use a different microcontroller with more RAM available.
As the modbus registers are 16-bit you could get away with using 40 registers instead of 80 to store the 80-byte ASCII LCD data but it means packing the ASCII data on the Master side and unpacking at the slave side.
To pack the 8-bit data into 16-bit values at the master side you would do this.
byte0 = 'H'
byte1 = 'i'
packed16bitword = byte0 | ( byte1 << 8 )
To unpack the 16-bit values into 8-bit data at the slave side you would do this.
byte0 = packed16bitword & 0xFF
byte1 = packed16bitword >> 8
Sounds like the 80 x 16-bit modbus registers cannot fit into your devices RAM, the eaisest option is to use a different microcontroller with more RAM available.
As the modbus registers are 16-bit you could get away with using 40 registers instead of 80 to store the 80-byte ASCII LCD data but it means packing the ASCII data on the Master side and unpacking at the slave side.
To pack the 8-bit data into 16-bit values at the master side you would do this.
byte0 = 'H'
byte1 = 'i'
packed16bitword = byte0 | ( byte1 << 8 )
To unpack the 16-bit values into 8-bit data at the slave side you would do this.
byte0 = packed16bitword & 0xFF
byte1 = packed16bitword >> 8
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello Benj.
According to PIC16F887 data sheet : PROG. MEMORY : 8192 bytes
SRAM : 368 bytes , 4 Banks 170 bytes free.
EEPROM : 256 bytes
MCU is 8bit . I don't know what part of RAM uses the code , it seems that is not enough . I don't want to convert data at master, so I'll use another MCU . What in your opinion is the suitable RAM capacity for an 8 bit MCU? I don't prefer to use an 16bit MCU.
Many thanks.
According to PIC16F887 data sheet : PROG. MEMORY : 8192 bytes
SRAM : 368 bytes , 4 Banks 170 bytes free.
EEPROM : 256 bytes
MCU is 8bit . I don't know what part of RAM uses the code , it seems that is not enough . I don't want to convert data at master, so I'll use another MCU . What in your opinion is the suitable RAM capacity for an 8 bit MCU? I don't prefer to use an 16bit MCU.
Many thanks.
- Benj
- Matrix Staff
- Posts: 15312
- Joined: Mon Oct 16, 2006 10:48 am
- Location: Matrix TS Ltd
- Has thanked: 4803 times
- Been thanked: 4314 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello,
The following chips have the same pin out as the 887 chip but more RAM.
16F1937 - 512 Bytes
16F1939 - 1024 Bytes
Or this chip has a lot more memory but a different pin out.
16F18877 - 4096 Bytes
Before you buy any more chips change the chip in the project options and make sure it will actually compile.
The following chips have the same pin out as the 887 chip but more RAM.
16F1937 - 512 Bytes
16F1939 - 1024 Bytes
Or this chip has a lot more memory but a different pin out.
16F18877 - 4096 Bytes
Before you buy any more chips change the chip in the project options and make sure it will actually compile.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
- Steve001
- Valued Contributor
- Posts: 1189
- Joined: Wed Dec 31, 2008 3:37 pm
- Has thanked: 460 times
- Been thanked: 523 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello SILVESTROS9
you could have a look at this post to help find a device
viewtopic.php?f=54&t=16403&hilit=picpac+list#p69835
Please note this is for V6 so double check that the device is compatible with V7
you could have a look at this post to help find a device
viewtopic.php?f=54&t=16403&hilit=picpac+list#p69835
Please note this is for V6 so double check that the device is compatible with V7
Steveas ben says Before you buy any more chips change the chip in the project options and make sure it will actually compile.
Success always occurs in private and failure in full view.
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello Steve001,
Thanks for useful info. I change the chip in project option with PIC16F1937 , PIC16F1939 and both compiles . For now I have not any of these chips, so I made an order . Now I must confirm that the code works . Is there a Modbus Master simulator as injector to make a flowchart simulation ? Is there an example that will help me to make a simulation ?
Thanks for useful info. I change the chip in project option with PIC16F1937 , PIC16F1939 and both compiles . For now I have not any of these chips, so I made an order . Now I must confirm that the code works . Is there a Modbus Master simulator as injector to make a flowchart simulation ? Is there an example that will help me to make a simulation ?
- Steve001
- Valued Contributor
- Posts: 1189
- Joined: Wed Dec 31, 2008 3:37 pm
- Has thanked: 460 times
- Been thanked: 523 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello SILVESTROS9
there are a couple of ways around this
make a master device using flow code and the associated hardware
you could use modscan - this requires use of a RS232 - RS485 converter
Modscan
http://www.win-tech.com/html/modscan32.htm
I have never used it to write coils though
Steve
there are a couple of ways around this
make a master device using flow code and the associated hardware
you could use modscan - this requires use of a RS232 - RS485 converter
Modscan
http://www.win-tech.com/html/modscan32.htm
I have never used it to write coils though
Steve
Success always occurs in private and failure in full view.
-
- Posts: 115
- Joined: Wed Aug 03, 2016 10:45 pm
- Has thanked: 24 times
- Been thanked: 11 times
- Contact:
Re: PIC AS MODBUS SLAVE
Hello Steve001,
Thanks for info. I'll try to use some Modbus master emulators to test the code in simulation mode. When I receive PIC microcontrollers I'll make some tests with hardware.
Thanks for info. I'll try to use some Modbus master emulators to test the code in simulation mode. When I receive PIC microcontrollers I'll make some tests with hardware.