eGospodarka.pl
eGospodarka.pl poleca

eGospodarka.plGrupypl.misc.elektronikaMultiplekser/sniffer/arbiter modbusRe: Multiplekser/sniffer/arbiter modbus
  • Data: 2023-04-05 23:02:43
    Temat: Re: Multiplekser/sniffer/arbiter modbus
    Od: Piotrek <p...@p...na.berdyczow.info> szukaj wiadomości tego autora
    [ pokaż wszystkie nagłówki ]

    On 05.04.2023 22:44, heby wrote:
    > On 05/04/2023 22:28, Piotrek wrote:
    >> GPT twierdzi, że nie ma ;-)
    >
    > GPT twierdzi, że to, jak jest najszybsza furmanka na świecie, zależy od
    > pojemności silnika. Nie dodał, czy jelit.
    >

    A kto by tam się przykładał do roboty za darmo? ;-)

    Ale wracając do naszych baranów ... będąc ciekawym czego można oczekiwać
    od GPT, odświeżyłem sobie swój projekt sprzed lat.

    Let's go back to the freemodbus library.

    I need a client that waits for incoming write coil commands and performs
    a specific action depending on the coil number.

    Could you give me a code sample to implement such requirements.

    "
    To create a Modbus RTU Slave (client) using the FreeModbus library,
    which waits for incoming Write Coil commands and takes specific actions
    based on the coil number, you can follow the example below:

    #include <stdio.h>
    #include "FreeRTOS.h"
    #include "task.h"
    #include "queue.h"
    #include "semphr.h"
    #include "mb.h"
    #include "mbport.h"

    #define MB_USART_PORT_NUM (1) // Change this to the correct USART port
    number
    #define MB_SLAVE_ADDRESS (1) // Change this to the correct slave address
    #define MB_COIL_START_ADDRESS (0) // Starting address of the coil
    #define MB_COIL_COUNT (10) // Number of coils

    static UCHAR ucCoilBuf[MB_COIL_COUNT / 8 + 1]; // Coil buffer

    eMBErrorCode eMBRegCoilsCB(UCHAR *pucRegBuffer, USHORT usAddress, USHORT
    usNCoils, eMBRegisterMode eMode) {
    eMBErrorCode eStatus = MB_ENOERR;
    int iNCoils = (int)usNCoils;
    USHORT usCoilStart = usAddress - MB_COIL_START_ADDRESS;

    if ((usCoilStart + usNCoils) <= MB_COIL_COUNT) {
    if (eMode == MB_REG_WRITE) {
    while (iNCoils > 0) {
    USHORT usByteOffset = usCoilStart / 8;
    UCHAR ucBitOffset = 1 << (usCoilStart % 8);
    UCHAR ucByte = pucRegBuffer[usByteOffset];

    if (ucByte & ucBitOffset) {
    ucCoilBuf[usByteOffset] |= ucBitOffset;
    } else {
    ucCoilBuf[usByteOffset] &= ~ucBitOffset;
    }

    // Implement specific actions depending on the coil number
    switch (usCoilStart) {
    case 0:
    // Perform action for coil 0
    break;

    case 1:
    // Perform action for coil 1
    break;

    // Add more cases for other coils as needed

    default:
    break;
    }

    iNCoils--;
    usCoilStart++;
    }
    } else {
    eStatus = MB_ENOREG;
    }
    } else {
    eStatus = MB_ENOREG;
    }

    return eStatus;
    }

    void ModbusSlaveTask(void *pvParameters) {
    eMBErrorCode eStatus;

    // Initialize Modbus stack
    eStatus = eMBInit(MB_RTU, MB_SLAVE_ADDRESS, MB_USART_PORT_NUM,
    9600, MB_PAR_NONE);
    if (eStatus != MB_ENOERR) {
    printf("Modbus initialization failed.\n");
    vTaskDelete(NULL);
    }

    // Enable Modbus stack
    eStatus = eMBEnable();
    if (eStatus != MB_ENOERR) {
    printf("Failed to enable Modbus stack.\n");
    vTaskDelete(NULL);
    }

    while (1) {
    // Poll the Modbus stack
    eMBPoll();

    // Add any necessary application logic here
    }
    }

    int main(void) {
    // Register the coil callback function
    eMBRegisterCB(MB_FUNC_WRITE_SINGLE_COIL, eMBRegCoilsCB);
    eMBRegisterCB(MB_FUNC_WRITE_MULTIPLE_COILS, eMBRegCoilsCB);

    // Create Modbus Slave Task
    xTaskCreate(ModbusSlaveTask, "ModbusSlaveTask",
    configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY + 1, NULL);

    // Start the FreeRTOS scheduler
    vTaskStartScheduler();

    for (;;) {
    }

    return 0;
    }

    "

    Ciekawe, ciekawe ...

    P.

Podziel się

Poleć ten post znajomemu poleć

Wydrukuj ten post drukuj


Następne wpisy z tego wątku

Najnowsze wątki z tej grupy


Najnowsze wątki

Szukaj w grupach

Eksperci egospodarka.pl

1 1 1

Wpisz nazwę miasta, dla którego chcesz znaleźć jednostkę ZUS.

Wzory dokumentów

Bezpłatne wzory dokumentów i formularzy.
Wyszukaj i pobierz za darmo: