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
logicpython:cubloc_api:setmodbus [2026/04/14 05:10] – [Example] mfranklinlogicpython:cubloc_api:setmodbus [2026/05/19 06:34] (current) – [Example] mfranklin
Line 61: Line 61:
  
 <code python> <code python>
-import struct 
-import time 
 import _thread import _thread
 from cubloc import * from cubloc import *
 +from machine import *
 +import os
 +from time import sleep
  
-OUTPUT_PINS = (25, 16, 18, 20+machine os.uname().machine 
-INPUT_PINS  = (6,  8,  10) +print(machine)
-ADC_CHANS   (0,  1,  2)+
  
 +if "LP126T" in machine:
 +    OUTPUT_PINS = (3, 16, 17, 18, 19)
 +    INPUT_PINS  = (32, 33, 34, 35, 36, 37, 38, 39)
 +    ADC_CHANS   = (40, 41, 42, 43, 44, 45)
 +else:
 +    OUTPUT_PINS = (25, 16, 17, 18)
 +    INPUT_PINS  = (19, 20, 21)
 +    ADC_CHANS   = (26, 27, 28)
 +
 +OUTPUT_COUNT = len(OUTPUT_PINS)
 +INPUT_COUNT  = len(INPUT_PINS)
 +ADC_COUNT    = len(ADC_CHANS)
 +ADC_BYTES    = ADC_COUNT << 1
 +
 +# Initialize the physical IO
 for pin in OUTPUT_PINS: for pin in OUTPUT_PINS:
     Output(pin)     Output(pin)
     Low(pin)     Low(pin)
 +
 for pin in INPUT_PINS: for pin in INPUT_PINS:
-    Input(pin)+    Input(pin, 1)
  
 # Configure and start the Modbus RTU slave process # Configure and start the Modbus RTU slave process
 UART_CHANNEL = 0 UART_CHANNEL = 0
-BAUD_RATE = 115200+BAUD_RATE = 460800
 PROTOCOL = 3        # 8N1 PROTOCOL = 3        # 8N1
 BUFFER_SIZE = 64 BUFFER_SIZE = 64
 SLAVE_ADDRESS = 1 SLAVE_ADDRESS = 1
  
 +# Configure the Modbus memory
 coils           = bytearray(1) coils           = bytearray(1)
-discrete_inputs = bytearray(1+discrete_inputs = bytearray(2
-input_regs      = bytearray(6+input_regs      = bytearray(16
-holding_regs    = bytearray(2)+holding_regs    = bytearray(16)
 mem_lock        = _thread.allocate_lock() mem_lock        = _thread.allocate_lock()
 +adc_shadow      = bytearray(ADC_BYTES)
 +setpoint        = 0
  
-SetModbus(UART_CHANNEL,+# Start the Modbus slave 
 +print("Starting Modbus"
 +SetModbus( 
 +    UART_CHANNEL,
     BAUD_RATE,     BAUD_RATE,
     PROTOCOL,     PROTOCOL,
     BUFFER_SIZE,     BUFFER_SIZE,
     BUFFER_SIZE,     BUFFER_SIZE,
-    SLAVE_ADDRESS,  +    SLAVE_ADDRESS, 
-    coils, discrete_inputs, input_regs, holding_regs, mem_lock)+    coils, 
 +    discrete_inputs, 
 +    input_regs, 
 +    holding_regs, 
 +    mem_lock 
 +)
  
 +# Run in an infinite loop synchronizing Modbus memory with the physical IO
 +print("Starting the main loop")
 while True: while True:
-    # Read coils snapshot under lock+    # Read the coil memory
     mem_lock.acquire()     mem_lock.acquire()
-    try: +    coils_byte = coils[0] 
-        coils_byte = coils[0] +    mem_lock.release()
-    finally: +
-        mem_lock.release()+
  
-    # Drive outputs from that snapshot +    # Drive digital outputs from the coil memory 
-    for i, pin in enumerate(OUTPUT_PINS)+    i = 0 
-        if coils_byte & (<< i): +    while i < OUTPUT_COUNT
-            High(pin+        Out(OUTPUT_PINS[i], (coils_byte >> i) & 1) 
-        else: +        i += 1
-            Low(pin)+
  
-    # Sample physical inputs outside the lock+    # Read digital inputs
     din_byte = 0     din_byte = 0
-    for i, pin in enumerate(INPUT_PINS)+    i = 0 
-        if In(pin): +    while i < INPUT_COUNT
-            din_byte |= (1 << i)+        if In(INPUT_PINS[i]): 
 +            din_byte |= 1 << i 
 +        i += 1
  
-    adc_vals = [ADIn(ch) for ch in ADC_CHANS] +    # Transfer digital inputs to discrete input memory
- +
-    Publish a coherent snapshot and read setpoint atomically+
     mem_lock.acquire()     mem_lock.acquire()
-    try: +    discrete_inputs[0] = din_byte 
-        discrete_inputs[0] = din_byte+    mem_lock.release()
  
-        for i, val in enumerate(adc_vals)+    # Read analog inputs 
-            struct.pack_into('>H', input_regs, * 2, val)+    = 0 
 +    j = 0 
 +    while i < ADC_COUNT
 +        val = ADIn(ADC_CHANS[i])
  
-        setpoint struct.unpack_from('>H', holding_regs, 0)[0] +        adc_shadow[j]     = (val >> 8& 0xFF 
-    finally: +        adc_shadow[j + 1] = val & 0xFF
-        mem_lock.release()+
  
-    time.sleep(0.010)+        i += 1 
 +        j += 2 
 + 
 +    # Transfer analog inputs to input register memory 
 +    mem_lock.acquire(
 +    i = 0 
 +    while i < ADC_BYTES: 
 +        input_regs[i] = adc_shadow[i] 
 +        i += 1 
 +    mem_lock.release() 
 + 
 +    # Transfer holding register memory to the setpoint variable 
 +    mem_lock.acquire() 
 +    setpoint = (holding_regs[0] << 8) | holding_regs[1] 
 +    mem_lock.release()
 </code> </code>
  
  
logicpython/cubloc_api/setmodbus.1776143418.txt.gz · Last modified: by mfranklin