User Tools

Site Tools

한국어

logicpython:cubloc_api:setmodbus

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
logicpython:cubloc_api:setmodbus [2026/04/14 04:51] – [Example] mfranklinlogicpython: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 ''channel''+Configure UART settings and start the LogicPython Modbus RTU slave process.
- +
-===== Syntax ===== +
- +
-<code python> +
-from cubloc import SetModbus +
- +
-SetModbus(channel: int, +
-    baudRate: int, +
-    protocol: int, +
-    recvSize: int, +
-    sendSize: int, +
-    slaveAddress: int, +
-    coils: object, +
-    discreteInputs: object, +
-    inputRegisters: object, +
-    holdingRegisters: object, +
-    memoryLock: object, +
-    returnInterval: int = 0) +
-</code> +
- +
-===== Parameters ===== +
- +
-  * **channel**: RS-232 ''channel'' number. ''OpenCom'' must already be called for this ''channel''+
-  * **slaveAddress**: Modbus unit ID (1 to 247). +
-  * **coils**: Writable bit-packed coil buffer (8 ''coils'' per byte). +
-  * **discreteInputs**: Writable bit-packed discrete-input buffer (8 inputs per byte). +
-  * **inputRegisters**: Writable input-register byte buffer (2 bytes per register). +
-  * **holdingRegisters**: Writable holding-register byte buffer (2 bytes per register). +
-  * **memoryLock**: lock object to synchronize Modbus memory access between the user program and the Modbus RTU slave process. +
-  * **returnInterval**: Non-blocking delay in microseconds before sending a response (default 0). +
- +
-===== Exceptions ===== +
- +
-  * [[https://docs.micropython.org/en/latest/library/builtins.html#TypeError|TypeError]]: An argument has an invalid type. +
-  * [[https://docs.micropython.org/en/latest/library/builtins.html#ValueError|ValueError]]: ''slaveAddress'' is outside the supported range. +
-  * [[https://docs.micropython.org/en/latest/library/builtins.html#RuntimeError|RuntimeError]]: ''OpenCom'' has not been called for this ''channel'' or Modbus startup failed. +
- +
-====== SetModbus ====== +
- +
-Configure UART settings and start the LogicPython Modbus RTU slave worker.+
  
 ===== Syntax ===== ===== Syntax =====
Line 66: Line 26:
     * **channel**: Serial ''channel'' number. ''Channel'' 0 maps to UART0 (GP0/GP1) and ''channel'' 1 maps to UART1 (GP4/GP5).     * **channel**: Serial ''channel'' number. ''Channel'' 0 maps to UART0 (GP0/GP1) and ''channel'' 1 maps to UART1 (GP4/GP5).
     * **baudRate**: Serial baud rate (2400 to 921600).     * **baudRate**: Serial baud rate (2400 to 921600).
-    * **protocol**: Encoded bit field for data bits, parity, and stop bits. +    * **protocol**: Encoded bit field for data bits, parity, and stop bits.  See the tables below
-    * **recvSize**: Receive buffer size in bytes (1 to 1024). +    * **recvSize**: Receive buffer size in bytes (1 to 4096). 
-    * **sendSize**: Send buffer size in bytes (1 to 1024).+    * **sendSize**: Send buffer size in bytes (1 to 4096).
     * **slaveAddress**: Modbus unit ID (1 to 247).     * **slaveAddress**: Modbus unit ID (1 to 247).
     * **coils**: Writable bit-packed coil buffer (8 ''coils'' per byte).     * **coils**: Writable bit-packed coil buffer (8 ''coils'' per byte).
Line 75: Line 35:
     * **holdingRegisters**: Writable holding-register byte buffer (2 bytes per register).     * **holdingRegisters**: Writable holding-register byte buffer (2 bytes per register).
     * **memoryLock**: lock object to synchronize Modbus memory access between the user program and the Modbus RTU slave process.     * **memoryLock**: lock object to synchronize Modbus memory access between the user program and the Modbus RTU slave process.
-    * **returnInterval**: Non-blocking delay in microseconds before sending response (default 0).+    * **returnInterval**: Delay in microseconds before replying to Modbus query from the Modbus master (default 0). 
 + 
 +''protocol'' bit layout: 
 + 
 +^ 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 ''ValueError'') | 
 + 
 +Common ''protocol'' values: 
 + 
 +^ protocol ^ Frame format ^ 
 +| 3 | 8N1 | 
 +| 11 | 8E1 | 
 +| 19 | 8O1 | 
 +| 7 | 8N2 |
  
 ===== Exceptions ===== ===== Exceptions =====
Line 94: Line 69:
 INPUT_PINS  = (6,  8,  10) INPUT_PINS  = (6,  8,  10)
 ADC_CHANS   = (0,  1,  2) ADC_CHANS   = (0,  1,  2)
 +
 +for pin in OUTPUT_PINS:
 +    Output(pin)
 +    Low(pin)
 +for pin in INPUT_PINS:
 +    Input(pin)
 +
 +# Configure and start the Modbus RTU slave process
 +UART_CHANNEL = 0
 +BAUD_RATE = 115200
 +PROTOCOL = 3        # 8N1
 +BUFFER_SIZE = 64
 +SLAVE_ADDRESS = 1
  
 coils           = bytearray(1) coils           = bytearray(1)
Line 101: Line 89:
 mem_lock        = _thread.allocate_lock() mem_lock        = _thread.allocate_lock()
  
-for pin in OUTPUT_PINS: +SetModbus(UART_CHANNEL, 
-    Output(pin) +    BAUD_RATE, 
-    Low(pin) +    PROTOCOL, 
-for pin in INPUT_PINS: +    BUFFER_SIZE, 
-    Input(pin) +    BUFFER_SIZE, 
- +    SLAVE_ADDRESS 
-SetModbus(0, 9600, 3, 64, 64, 1, coils, discrete_inputs, input_regs, holding_regs, mem_lock)+    coils, discrete_inputs, input_regs, holding_regs, mem_lock)
  
 while True: while True:
logicpython/cubloc_api/setmodbus.1776142305.txt.gz · Last modified: by mfranklin