https://github.com/interkosmos/fortran-modbus
Fortran 2018 interface bindings to libmodbus
https://github.com/interkosmos/fortran-modbus
automation fortran fortran-2018 fortran-package-manager iiot iot libmodbus modbus modbus-rtu modbus-tcp
Last synced: 11 months ago
JSON representation
Fortran 2018 interface bindings to libmodbus
- Host: GitHub
- URL: https://github.com/interkosmos/fortran-modbus
- Owner: interkosmos
- License: isc
- Created: 2024-04-23T20:07:42.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-11-18T22:23:10.000Z (about 1 year ago)
- Last Synced: 2024-12-30T13:57:09.445Z (about 1 year ago)
- Topics: automation, fortran, fortran-2018, fortran-package-manager, iiot, iot, libmodbus, modbus, modbus-rtu, modbus-tcp
- Language: Fortran
- Homepage:
- Size: 25.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# fortran-modbus
A collection of Fortran 2018 interface bindings to
[libmodbus](https://libmodbus.org/), for Modbus RTU/TCP communication.
## Build Instructions
The package _libmodbus_ has to be installed with development headers. On
FreeBSD, run:
```
# pkg install comms/libmodbus
```
On Linux, instead:
```
# apt-get install libmodbus5 libmodbus-dev
```
Build and install the Fortran library using the provided Makefile:
```
$ make
$ make install PREFIX=/opt
```
Link your programs against `/opt/lib/libfortran-modbus.a -lmodbus`. Optionally,
overwrite the default compiler and the compiler flags:
```
$ make FC=ifx FFLAGS="-O3"
```
Or, use the [Fortran Package Manager](https://github.com/fortran-lang/fpm):
```
$ fpm build --profile release
```
Build and run the test program:
```
$ make test
$ ./test_modbus
```
## Example
The following example program connects to a device via Modbus RTU and outputs
two registers as a real number:
```fortran
! example.f90
program main
use, intrinsic :: iso_c_binding
use :: modbus
use :: modbus_rtu
implicit none (type, external)
integer, parameter :: ADDRESS = 50
integer, parameter :: SLAVE = 10
integer :: stat
integer(kind=c_uint16_t) :: regs(2)
real :: f
type(c_ptr) :: ctx
ctx = c_null_ptr
stat = -1
regs = 0
mb_block: block
! Create Modbux RTU context.
ctx = modbus_new_rtu('/dev/ttyUSB0', 19200, 'E', 8, 1)
if (.not. c_associated(ctx)) exit mb_block
! Connect to device.
stat = modbus_connect(ctx)
if (stat == -1) exit mb_block
! Set slave number.
stat = modbus_set_slave(ctx, SLAVE)
if (stat == -1) exit mb_block
! Read registers.
stat = modbus_read_registers(ctx, ADDRESS, size(regs), regs)
if (stat == -1) exit mb_block
! Convert to real.
f = modbus_get_float_abcd(regs)
print '(f12.8)', f
end block mb_block
call modbus_close(ctx)
call modbus_free(ctx)
if (stat == -1) print '(a)', 'Error: operation failed'
end program main
```
If the Fortran library is installed to `/opt/lib`, run:
```
$ gfortran -o example example.f90 /opt/lib/libfortran-modbus.a -lmodbus
```
## Fortran Package Manager
You can add *fortran-modbus* as an FPM dependency:
```toml
[dependencies]
fortran-modbus = { git = "https://github.com/interkosmos/fortran-modbus.git" }
```
## Licence
ISC