Recent

Author Topic: Modbus on Raspberry Pi  (Read 1888 times)

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
Re: Modbus on Raspberry Pi
« Reply #30 on: November 07, 2019, 12:56:44 pm »
Absolute will do indeed, since that is basically a pointer de-referenced with a specific type. I would not recommend it in most cases, but here it is a good solution.
also related to equus asinus.

cgd

  • New member
  • *
  • Posts: 6
Re: Modbus on Raspberry Pi
« Reply #31 on: November 07, 2019, 09:34:55 pm »
I made my own routines to Communicate Raspberry PI3B/3B+ with PLCs by Modbus ASCII (using TSdpoSerial component) and Modbus TCP (using TIdTCPClient component)... Just in case, let me know if you need any help.

avra

  • Hero Member
  • *****
  • Posts: 1717
    • Additional info
Re: Modbus on Raspberry Pi
« Reply #32 on: November 08, 2019, 09:07:08 am »
I don't use the absolute directive but instead a record with variant parts.
Then you will probably like BitHelpers from my signature  ;)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jdp

  • Jr. Member
  • **
  • Posts: 90
Re: Modbus on Raspberry Pi
« Reply #33 on: November 08, 2019, 09:09:46 am »
I got PascalScada modbus installed. fabio who developes it helped me to get it installed. Now I need to figure out how it works to read a normal regitser using it. Who knows how it works? I have it working in Python using minimalmodbus. I now want the same in Lazarus usinf PascalScada.


Code: Pascal  [Select]
  1. import minimalmodbus
  2.  
  3. vsd = minimalmodbus.Instrument('/dev/ttymeter', 1 , 'rtu')
  4. vsd.serial.parity = serial.PARITY_NONE
  5. vsd.serial.baudrate = 9600
  6. vsd.serial.timeout = 1    
  7. vsd.serial.flushInput()
  8.  
  9.  
  10. watts = vsd.read_float(0x000C  , functioncode=4 ,  number_of_registers=2)
  11.  
  12.  

jdp

  • Jr. Member
  • **
  • Posts: 90
Re: Modbus on Raspberry Pi
« Reply #34 on: November 08, 2019, 09:12:12 am »
I started with a simple test program but I am not sure what all the steps should be.
Code: Pascal  [Select]
  1. program mbustest;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes,
  10.   ModBusSerial,
  11.   SerialPort,
  12.   PLCTagNumber;
  13.  
  14.  
  15. var
  16.  
  17.   spd        : TSerialPortDriver;
  18.   mbd        : TModBusRTUDriver;
  19.   watts_tag  : TPLCTagNumber;
  20.  
  21.  
  22.  
  23.   watts  : Double;
  24.  
  25. begin
  26.  
  27.  
  28.  
  29.      //Set the comport properties
  30.      spd.COMPort := '/dev/ttymeter';
  31.      spd.BaudRate := br9600;
  32.      spd.Timeout := 1;
  33.      spd.Paridade := spNone;
  34.  
  35.  
  36.  
  37.      //Set the Modbus properties
  38.      mbd.CommunicationPort := spd;
  39.      //mbd.
  40.  
  41.  
  42.      //Set the tag properties
  43.      watts_tag.PLCStation := 1;
  44.  
  45.  
  46.  
  47.      //Read the regitsers
  48.  
  49.  
  50.  
  51.      //Print the values
  52.      write('Meter Watss : ');
  53.      writeln(watts);
  54.  
  55.  
  56. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
Re: Modbus on Raspberry Pi
« Reply #35 on: November 08, 2019, 09:36:13 am »
I don't use the absolute directive but instead a record with variant parts.
Then you will probably like BitHelpers from my signature  ;)
@Avra
Aside: Maybe we should integrate each others code for my suggested patch (there is a willingness to accept it for sysutils helpers from devel's)
Maybe we can work together and  use some of your code for the helpers? My code is more basic, bare essentials,  and your code has more features.
also related to equus asinus.

avra

  • Hero Member
  • *****
  • Posts: 1717
    • Additional info
Re: Modbus on Raspberry Pi
« Reply #36 on: November 08, 2019, 11:05:45 am »
I started with a simple test program but I am not sure what all the steps should be.
Do you really need console application? There are several GUI modbus demos in Examples directory. It would be much easier if you examine them and try to adapt them to your needs. You can even use some of the mentioned MODBUS slave simulators for a quick startup convenience. From your code and guessed expectations I think that one of the 2 mentioned libraries would be more appropriate.

Maybe we should integrate each others code for my suggested patch (there is a willingness to accept it for sysutils helpers from devel's)
Maybe we can work together and use some of your code for the helpers? My code is more basic, bare essentials, and your code has more features.
Feel free to use whatever you want from my code. If discussion is needed, then PM me or open a new thread. On the other hand I use almost every features from BitHelpers so if merge is not feature rich (especially custom format data printing) then I can simply continue using original BitHelpers. Not a problem, as long as there are no conflicts.

If you remember, we discussed adding BitHelpers to sysutils, and at first I was convinced that it was the way to go, but because this issue could not be fixed:
Code: Pascal  [Select]
  1. MyWord.Byte[1].Bit[7] := true; // %0000000100000010 MyWord equals 258 (Beware!!! This DOES NOT set a bit in MyWord !!!)
I felt that it did not belong there - having something that potentially could lead to bugs in user code. I know that documenting such behavior should be enough, but many people do not read every bit of docs and besides compiler lets them use it in such a way so they might expect different from what can be. I would satisfy even with a compiler warning or error in such case, but could not find a way to do it. That's why I wanted to simply donate BitHelpers unit to FPC (and documenting it including mentioned behavior), but I was late for 3.2 so I was initially hoping to do it for 3.4. Anyway, as already said if you come up with something else I am quite fine with that, too. Merge is fine, but leaving our code separate is also fine.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jdp

  • Jr. Member
  • **
  • Posts: 90
Re: Modbus on Raspberry Pi
« Reply #37 on: November 08, 2019, 11:58:06 am »
Yes I need cli as the pi wont have a gui desktop. It runs the light Os without any gui stuff on it.
I am looking at the gui samples to try and figure out how this works but if sombody knows off hand then please help out a fellow developer lol.

MarkMLl

  • Full Member
  • ***
  • Posts: 224
Re: Modbus on Raspberry Pi
« Reply #38 on: November 08, 2019, 12:08:48 pm »
Yes I need cli as the pi wont have a gui desktop. It runs the light Os without any gui stuff on it.

Remember that you can always tunnel X11 programs over SSH, or install the non-lite variant of the OS and get a desktop using VNC.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
Re: Modbus on Raspberry Pi
« Reply #39 on: November 08, 2019, 12:12:30 pm »
Code: Pascal  [Select]
  1. MyWord.Byte[1].Bit[7] := true; // %0000000100000010 MyWord equals 258 (Beware!!! This DOES NOT set a bit in MyWord !!!)
That has already been addressed in the patch(es), but the original request is somewhat obfuscated by unnecessary comments.
The real issue is that some ordinal formats are treated differently - and wrong! - by the compiler: it is a sign issue, e.g. 128 etc instead of -128. (actually that one works)
Fixing that without {$rangechecks off} takes quite some thought, unless the compiler behaves as it should. Luckily most cases do not pose a problem, but 2 cases do.
And I *must* do so, because otherwise it will break existing things. The actual code is straight forward and rather basic. The compiler behavior is not!
The current status is that all tests pass with range checks off (even the initial patch) but there happens to be a needless expansion in those two cases and only for Intel.
My walking bit demo is  the proof. It is exhaustive. The latest patch is because I was wrong footed. The first patch needs just an extra type cast (not a removal as was suggested)
« Last Edit: November 08, 2019, 12:20:43 pm by Thaddy »
also related to equus asinus.

jdp

  • Jr. Member
  • **
  • Posts: 90
Re: Modbus on Raspberry Pi
« Reply #40 on: November 08, 2019, 12:51:50 pm »
Nope. I do not want to use any form of GUI. It adds a lot of overhead and I have other reasons. I need it to work on CLI. It works in Python with a couple of lines of code. Not sure why it is this difficult in Lazarus to do the same. I just want to read some registers using modbus. I am not a Lazarus guru that can build his own libs and downt have the time to do it also. I also asked Fabio if he can give me a sample ap. I hope he will as this should not be too complicated to do. I just don't know what to do as there is no documentation I can follow other than the GUI apps.

MarkMLl

  • Full Member
  • ***
  • Posts: 224
Re: Modbus on Raspberry Pi
« Reply #41 on: November 08, 2019, 12:58:54 pm »
That's fine. But you're putting yourself in a position where you're having to modify working code using a development tool with which you're not entirely familiar, and you're also asking everybody else to move away from a combination with which they're familiar.

You'd be in a stronger position if you first got the well-tested program working so that you know how things behave, and then investigated ripping out the GUI stuff etc. that you don't need.

Or at least that's the approach I've found most useful over the last 30 years or so.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.

jdp

  • Jr. Member
  • **
  • Posts: 90
Re: Modbus on Raspberry Pi
« Reply #42 on: November 08, 2019, 02:01:12 pm »
Mark I am not asking anybody to move away from anything. I am asking for help with a CLI app only. If there is no Modbus Lib for CLI only then there is a gap for one as a lot of IOT edge devices like the Pi will run headless in command line mode only. Modbus is a protocol that a lot of meters use and I see the need for such a lib in Lazarus. But again that is my opinion and need at this stage.

I guess I can build it all in C if I have to. The reason I don't use Python is the code is open and people can mess with it as the code is open for all to read.

avra

  • Hero Member
  • *****
  • Posts: 1717
    • Additional info
Re: Modbus on Raspberry Pi
« Reply #43 on: November 08, 2019, 02:36:30 pm »
If there is no Modbus Lib for CLI only then there is a gap for one as a lot of IOT edge devices
Did you read my previous message at all about 2 other MODBUS libraries and one even comming with CLI demo?
https://forum.lazarus.freepascal.org/index.php/topic,47289.msg338661.html#msg338661
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Thaddy

  • Hero Member
  • *****
  • Posts: 9183
Re: Modbus on Raspberry Pi
« Reply #44 on: November 08, 2019, 04:47:59 pm »
@Avra
Michael has accepted the patch, with a few tweaks, so trunk has basic bit manipulation for all integer types now... :D O:-)
If anyone wants more features, I will refer to your implementation.
And thx Bart for extensive testing!
- setbit()
- clearbit()
- togglebit()
- TestBit()
- ToBinString

All ranges supported up to Qword.
« Last Edit: November 09, 2019, 11:20:19 am by Thaddy »
also related to equus asinus.