This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| logicpython:cubloc_api:setmodbus [2026/04/10 07:19] – [Example] mfranklin | logicpython:cubloc_api:setmodbus [2026/04/14 05:10] (current) – [Example] mfranklin | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== SetModbus ====== | ====== SetModbus ====== | ||
| - | Start the LogicPython Modbus RTU slave worker using a pre-opened UART '' | + | Configure UART settings and start the LogicPython Modbus RTU slave process. |
| ===== Syntax ===== | ===== Syntax ===== | ||
| Line 8: | Line 8: | ||
| from cubloc import SetModbus | from cubloc import SetModbus | ||
| - | SetModbus(channel: | + | SetModbus(channel: int, |
| + | baudRate: int, | ||
| + | protocol: int, | ||
| + | recvSize: int, | ||
| + | sendSize: int, | ||
| slaveAddress: | slaveAddress: | ||
| coils: object, | coils: object, | ||
| Line 20: | Line 24: | ||
| ===== Parameters ===== | ===== Parameters ===== | ||
| - | | + | |
| - | * **slaveAddress**: | + | * **baudRate**: |
| - | * **coils**: Writable bit-packed coil buffer (8 '' | + | * **protocol**: |
| - | * **discreteInputs**: | + | * **recvSize**: |
| - | * **inputRegisters**: | + | * **sendSize**: |
| - | * **holdingRegisters**: | + | |
| - | * **memoryLock**: | + | * **coils**: Writable bit-packed coil buffer (8 '' |
| - | * **returnInterval**: | + | * **discreteInputs**: |
| + | * **inputRegisters**: | ||
| + | * **holdingRegisters**: | ||
| + | * **memoryLock**: | ||
| + | * **returnInterval**: | ||
| + | |||
| + | '' | ||
| + | |||
| + | ^ Field ^ Bits ^ Meaning ^ | ||
| + | | Data bits | 1..0 | 00=5 bits, 01=6 bits, 10=7 bits, 11=8 bits | | ||
| + | | Stop bits | 2 | 0=1 stop bit, 1=2 stop bits | | ||
| + | | Parity | 4..3 | 00=None, 10=Even, 11=Odd (01 is reserved and raises '' | ||
| + | |||
| + | Common '' | ||
| + | |||
| + | ^ protocol ^ Frame format ^ | ||
| + | | 3 | 8N1 | | ||
| + | | 11 | 8E1 | | ||
| + | | 19 | 8O1 | | ||
| + | | 7 | 8N2 | | ||
| ===== Exceptions ===== | ===== Exceptions ===== | ||
| - | | + | |
| - | * [[https:// | + | * [[https:// |
| - | * [[https:// | + | * [[https:// |
| ===== Example ===== | ===== Example ===== | ||
| Line 46: | Line 69: | ||
| INPUT_PINS | INPUT_PINS | ||
| ADC_CHANS | ADC_CHANS | ||
| - | |||
| - | coils = bytearray(1) | ||
| - | discrete_inputs = bytearray(1) | ||
| - | input_regs | ||
| - | holding_regs | ||
| - | mem_lock | ||
| for pin in OUTPUT_PINS: | for pin in OUTPUT_PINS: | ||
| Line 59: | Line 76: | ||
| Input(pin) | Input(pin) | ||
| - | OpenCom(0, 460800, | + | # Configure and start the Modbus RTU slave process |
| - | SetModbus(0, 1, coils, discrete_inputs, | + | UART_CHANNEL = 0 |
| + | BAUD_RATE = 115200 | ||
| + | PROTOCOL = 3 # 8N1 | ||
| + | BUFFER_SIZE = 64 | ||
| + | SLAVE_ADDRESS = 1 | ||
| + | |||
| + | coils = bytearray(1) | ||
| + | discrete_inputs = bytearray(1) | ||
| + | input_regs | ||
| + | holding_regs | ||
| + | mem_lock | ||
| + | |||
| + | SetModbus(UART_CHANNEL, | ||
| + | BAUD_RATE, | ||
| + | PROTOCOL, | ||
| + | BUFFER_SIZE, | ||
| + | BUFFER_SIZE, | ||
| + | SLAVE_ADDRESS, | ||
| + | | ||
| while True: | while True: | ||
| Line 99: | Line 134: | ||
| time.sleep(0.010) | time.sleep(0.010) | ||
| </ | </ | ||
| - | |||
| - | ===== Related APIs ===== | ||
| - | |||
| - | * [[logicpython: | ||