-
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.
Następne wpisy z tego wątku
- 06.04.23 08:47 Dawid Rutkowski
- 06.04.23 09:28 heby
- 06.04.23 11:17 Dawid Rutkowski
- 06.04.23 11:23 heby
- 06.04.23 11:56 Dawid Rutkowski
- 06.04.23 12:12 heby
- 06.04.23 13:45 Dawid Rutkowski
- 06.04.23 14:33 heby
- 06.04.23 14:47 Dawid Rutkowski
- 06.04.23 15:47 heby
- 06.04.23 16:09 Dawid Rutkowski
- 06.04.23 16:19 heby
- 06.04.23 16:36 Dawid Rutkowski
- 06.04.23 16:53 heby
- 06.04.23 17:24 Dawid Rutkowski
Najnowsze wątki z tej grupy
- Elektryczna łapka i szerszeń
- moduł dtmf
- 0.5V
- Odbiór SDR na mikrokontrolerach
- Taki uziom....
- Nałożnica?
- ładowanie akku
- Mikrofala
- organy
- Zasilacz, regulacja prądu, oscylacje
- Tester akumulatorów ACT612
- HD44780 i urządzenia 3,3V
- Odbiornik Ania R-612 - wyniesienie anteny ferrytowej
- Ładny rewersik
- Polski satelita i rozdzielczość
Najnowsze wątki
- 2024-09-02 Re: RP3 kasuje wizy studentom/"studentom" po wylądowaniu na lotnisku
- 2024-09-02 Wzór
- 2024-09-01 Polska-Chorwacja 650zł elektrykiem
- 2024-09-01 odrdzewiacz do amelinium
- 2024-09-01 Elektryczna łapka i szerszeń
- 2024-09-01 Jakie podatki za wynajem?
- 2024-09-02 Warszawa => Key Account Manager <=
- 2024-09-02 Warszawa => Projektant/Programista React Native <=
- 2024-09-02 Gdańsk => Head of International Freight Forwarding Department <=
- 2024-09-02 Warszawa => Sales Development Representative (in German) <=
- 2024-09-01 1. wrzesnia po niemiecku
- 2024-09-01 Od setki do setki...
- 2024-08-31 wpadłem w siodło
- 2024-08-25 cenka opada
- 2024-08-30 Elektryki takie bezpieczne cd.