Skip to content

Commit bd290d7

Browse files
committed
example updates
1 parent e2de7b5 commit bd290d7

File tree

8 files changed

+49
-42
lines changed

8 files changed

+49
-42
lines changed

.gitmodules

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
[submodule "examples/libmodbus_tests/-f"]
2-
path = examples/libmodbus_tests/-f
3-
url = git@github.com:stephane/libmodbus.git
41
[submodule "examples/libmodbus_tests/libmodbus"]
52
path = examples/libmodbus_tests/libmodbus
63
url = git@github.com:stephane/libmodbus.git
@@ -16,3 +13,6 @@
1613
[submodule "include/flatbuffers"]
1714
path = include/flatbuffers
1815
url = git@github.com:snhobbs/flatbuffers.git
16+
[submodule "include/BareMetalHardwareAbstraction"]
17+
path = include/HardwareInterfaces
18+
url = git@github.com:snhobbs/BareMetalHardwareAbstraction

examples/LinuxSlave/source/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ int main(int argc, char* argv[]){
2424
while(true) {
2525
loops++;
2626
slave.Run();
27-
sleep(0.05);
27+
//sleep(0.5);
2828
#if 0
2929
if (loops%(1<<10) == 0) {
3030
printf("[");

examples/libmodbus_tests/libmodbus_client/libmodbus_slave/libmodbus_slave.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ int main(int argc, char* argv[]) {
4646
loops++;
4747

4848
len = modbus_receive(ctx, req);
49-
if (len == -1) break;
49+
if (len == -1) continue;
5050

5151
len = modbus_reply(ctx, req, len, mapping);
5252
for (std::size_t i=0; i<len; i++){
5353
std::cout << static_cast<int>(req[i]) << ", ";
5454
}
5555
std::cout << std::endl;
56-
if (len == -1) break;
56+
if (len == -1) continue;
5757

5858
#if 0
5959
if (loops%16 == 0) {

examples/posix/PosixSerial.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int SetupSerial(const char *device, const speed_t baudrate = B9600) {
6464
return connection;
6565
}
6666

67-
class UartController : public IODevice {
67+
class UartController: public IODevice<RingBuffer<uint8_t, 1024>> {
6868
const char *const device_name_;
6969
const speed_t baudrate_;
7070
int connection_ = 0;

examples/pymodbus_tests/asynchronous_server.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
of nodes which can be helpful for testing monitoring software.
99
"""
1010
# --------------------------------------------------------------------------- #
11+
import sys
1112
# import the various server implementations
1213
# --------------------------------------------------------------------------- #
13-
from pymodbus.server.asynchronous import StartTcpServer
14-
from pymodbus.server.asynchronous import StartUdpServer
15-
from pymodbus.server.asynchronous import StartSerialServer
14+
#from pymodbus.server.asynchronous import StartTcpServer
15+
#from pymodbus.server.asynchronous import StartUdpServer
16+
from pymodbus.server import StartSerialServer
1617

1718
from pymodbus.device import ModbusDeviceIdentification
1819
from pymodbus.datastore import ModbusSequentialDataBlock
@@ -33,7 +34,7 @@
3334
log.setLevel(logging.DEBUG)
3435

3536

36-
def run_async_server():
37+
def run_async_server(port="/dev/ttyUSB0"):
3738
# ----------------------------------------------------------------------- #
3839
# initialize your data store
3940
# ----------------------------------------------------------------------- #
@@ -146,8 +147,8 @@ def run_async_server():
146147
# StartUdpServer(context, identity=identity, address=("127.0.0.1", 5020))
147148

148149
# RTU Server
149-
StartSerialServer(context, identity=identity,
150-
port='/dev/ttyUSB0', framer=ModbusRtuFramer, baud=9600)
150+
StartSerialServer(context=context, identity=identity,
151+
port=port, framer=ModbusRtuFramer, baud=9600)
151152

152153
# ASCII Server
153154
# StartSerialServer(context, identity=identity,
@@ -159,5 +160,4 @@ def run_async_server():
159160

160161

161162
if __name__ == "__main__":
162-
run_async_server()
163-
163+
run_async_server(sys.argv[1] if len(sys.argv)>1 else "/dev/ttyUSB0")

examples/pymodbus_tests/custom_message.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@
1818
# import the various server implementations
1919
# --------------------------------------------------------------------------- #
2020
from pymodbus.pdu import ModbusRequest, ModbusResponse, ModbusExceptions
21-
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
2221
from pymodbus.bit_read_message import ReadCoilsRequest
23-
from pymodbus.compat import int2byte, byte2int
2422
# --------------------------------------------------------------------------- #
2523
# configure the client logging
2624
# --------------------------------------------------------------------------- #
@@ -34,7 +32,7 @@
3432
# --------------------------------------------------------------------------- #
3533
# The following is simply a read coil request that always reads 16 coils.
3634
# Since the function code is already registered with the decoder factory,
37-
# this will be decoded as a read coil response. If you implement a new
35+
# this will be decoded as a read coil response. If you implement a new
3836
# method that is not currently implemented, you must register the request
3937
# and response with a ClientDecoder factory.
4038
# --------------------------------------------------------------------------- #
@@ -47,13 +45,13 @@ class CustomModbusResponse(ModbusResponse):
4745
def __init__(self, values=None, **kwargs):
4846
ModbusResponse.__init__(self, **kwargs)
4947
self.values = values or []
50-
48+
5149
def encode(self):
5250
""" Encodes response pdu
5351
5452
:returns: The encoded packet message
5553
"""
56-
result = int2byte(len(self.values) * 2)
54+
result = bytes([len(self.values) * 2])
5755
for register in self.values:
5856
result += struct.pack('>H', register)
5957
return result
@@ -117,6 +115,7 @@ def __init__(self, address, **kwargs):
117115

118116

119117
if __name__ == "__main__":
118+
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
120119
with ModbusClient(host='localhost', port=5020) as client:
121120
client.register(CustomModbusResponse)
122121
request = CustomModbusRequest(1, unit=1)

examples/pymodbus_tests/test_device_identifier.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,35 @@
33
from serial import Serial
44
from time import sleep
55
import sys
6+
import click
67

7-
device = "/tmp/ptyp0"
8-
if(len(sys.argv) > 1) :
9-
device = sys.argv[1]
8+
@click.command()
9+
@click.option("--device", "-d", default="/tmp/ptyp0")
10+
@click.option("--address", "-a", type=int, default=3)
11+
@click.option("--repeat", "-r", type=int, default=0)
12+
@click.option("--period", "-p", type=float, default=1)
13+
@click.option("--baud", "-d", type=int, default=9600)
14+
def run(device, address, repeat, period, baud):
15+
s = Serial(device, baud)
16+
crc = Crc(width = 16, poly = 0x8005,reflect_in = True, xor_in = 0xffff, reflect_out = True, xor_out = 0x0000)
1017

11-
slave_address = 0x03
12-
if(len(sys.argv) > 2) :
13-
slave_address = int(sys.argv[2])
18+
count = 0
19+
while True:
20+
if count > repeat + 1 and repeat > 0:
21+
break
22+
else:
23+
count -= 1
24+
function = 0x2b
25+
frame = [address, function, 0x0f, 0x01, 0x00]
26+
crc_calc = crc.bit_by_bit(frame)
27+
frame.append(crc_calc & 0xff)
28+
frame.append((crc_calc >> 8) & 0xff)
29+
s.write(bytes(frame))
30+
print([hex(d) for d in frame])
31+
data = (s.read_all())
32+
print(data, len(data))
33+
print([chr(d) for d in data])
34+
sleep(period)
1435

15-
s = Serial(device, 9600)
16-
#s = Serial("/dev/ttyUSB0", 9600)
17-
crc = Crc(width = 16, poly = 0x8005,reflect_in = True, xor_in = 0xffff, reflect_out = True, xor_out = 0x0000)
18-
19-
function = 0x2b
20-
frame = [slave_address, function, 0x0f, 0x01, 0x00]
21-
22-
crc_calc = crc.bit_by_bit(frame)
23-
frame.append(crc_calc & 0xff)
24-
frame.append((crc_calc >> 8) & 0xff)
25-
s.write(bytes(frame))
26-
print([hex(d) for d in frame])
27-
sleep(2)
28-
data = (s.read_all())
29-
print(data, len(data))
30-
print([chr(d) for d in data])
36+
if __name__ == "__main__":
37+
run()

include/HardwareInterfaces

Submodule HardwareInterfaces added at f96b1d7

0 commit comments

Comments
 (0)