eGospodarka.pl
eGospodarka.pl poleca

eGospodarka.plGrupypl.misc.elektronikaMultiplekser/sniffer/arbiter modbus › Re: Multiplekser/sniffer/arbiter modbus
  • Path: news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed.pionier.net.pl!2.eu.feeder.erj
    e.net!feeder.erje.net!weretis.net!feeder8.news.weretis.net!news.uzoreto.com!pee
    r03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!newsfeed.neostra
    da.pl!unt-exc-02.news.neostrada.pl!unt-spo-b-01.news.neostrada.pl!news.neostrad
    a.pl.POSTED!not-for-mail
    Date: Wed, 5 Apr 2023 23:02:43 +0200
    MIME-Version: 1.0
    User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
    Thunderbird/102.9.0
    Subject: Re: Multiplekser/sniffer/arbiter modbus
    Content-Language: en-US, pl
    Newsgroups: pl.misc.elektronika
    References: <u0jesg$3rbf4$1@dont-email.me>
    <4...@g...com>
    <u0kfa6$513$1@dont-email.me>
    <642dcc7b$0$19610$65785112@news.neostrada.pl>
    <u0kjt4$nps$1@dont-email.me>
    <642dd9da$0$19615$65785112@news.neostrada.pl>
    <u0kmjc$10e0$4@dont-email.me>
    From: Piotrek <p...@p...na.berdyczow.info>
    In-Reply-To: <u0kmjc$10e0$4@dont-email.me>
    Content-Type: text/plain; charset=UTF-8; format=flowed
    Content-Transfer-Encoding: 8bit
    Lines: 138
    Message-ID: <642de1f3$0$19604$65785112@news.neostrada.pl>
    Organization: Telekomunikacja Polska
    NNTP-Posting-Host: 5.173.168.10
    X-Trace: 1680728564 unt-rea-a-02.news.neostrada.pl 19604 5.173.168.10:6597
    X-Complaints-To: a...@n...neostrada.pl
    X-Received-Bytes: 5173
    Xref: news-archive.icm.edu.pl pl.misc.elektronika:779556
    [ ukryj 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: