Reformat modbus_tcp.c/h and fix some typos

This commit is contained in:
L-diy
2023-11-26 22:35:21 +01:00
parent 2090b039d4
commit e4b50559c4
2 changed files with 74 additions and 86 deletions

View File

@@ -8,7 +8,8 @@
#ifndef INC_MODBUS_H_ #ifndef INC_MODBUS_H_
#define INC_MODBUS_H_ #define INC_MODBUS_H_
#define MODBUSPORT 502 //is the default
#define MODBUSPORT 502 // 502 is the default
#include <tcp.h> #include <tcp.h>
@@ -21,9 +22,8 @@
/** /**
* @fn void modbus_init * @fn void modbus_init
* @brief Initiallises the modbus tcp * @brief Initializes the modbus tcp
*/ */
void modbus_init(void); void modbus_init(void);
#endif /* INC_MODBUS_H_ */ #endif /* INC_MODBUS_H_ */

View File

@@ -30,17 +30,15 @@ char registers[MAX_REG];
// const char* TAG = "Modbus_TCP"; // Tag used in logs // const char* TAG = "Modbus_TCP"; // Tag used in logs
// Functions // Functions
static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err); static err_t modbus_incoming_data(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err);
static err_t modbus_accept(void* arg, struct tcp_pcb* pcb, err_t err); static err_t modbus_accept(void* arg, struct tcp_pcb* pcb, err_t err);
/** /**
* @fn static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) * @fn static err_t modbus_incoming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
* @brief Function thats called when theris a new request on port 502. * @brief Function that's called when there is a new request on port 502.
* It handles the incomming data from Qmodmaster * It handles the incoming data from Qmodmaster
*/ */
static err_t modbus_incoming_data(void* arg, struct tcp_pcb* pcb, struct pbuf* p, err_t err) {
static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err){
uint8_t counter; uint8_t counter;
char* pc; char* pc;
char text[TEXT_LENGHT]; char text[TEXT_LENGHT];
@@ -59,12 +57,12 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
if (p != NULL) { if (p != NULL) {
// LOG_INFO(TAG, "data not null\n"); // LOG_INFO(TAG, "data not null\n");
// here im going to procces the modbusdata // Procces the modbusdata
tcp_recved(pcb, p->tot_len); tcp_recved(pcb, p->tot_len);
pc = (char*)p->payload; pc = (char*)p->payload;
for(uint16_t i = 0; i < p->tot_len; i++) { // putting the bufer in the register array for (uint16_t i = 0; i < p->tot_len; i++) { // Putting the bufer in the register array
registers[i] = pc[i]; // getting the error "void value not ignored as it ought to be" on this line registers[i] = pc[i]; // Getting the error "void value not ignored as it ought to be" on this line
} }
if (registers[7] == MULTIPLE_REG) { // Check if it's a Modbus Write Multiple Registers request (0x10) if (registers[7] == MULTIPLE_REG) { // Check if it's a Modbus Write Multiple Registers request (0x10)
@@ -77,7 +75,8 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
text_color_blue = (uint8_t)(registers[REG_06]); // 06 text_color_blue = (uint8_t)(registers[REG_06]); // 06
nr_img = (uint8_t)(registers[REG_07]); // 07 nr_img = (uint8_t)(registers[REG_07]); // 07
//LOG_INFO(TAG, "%d %d %d %d %d %d %d ",background_red,background_green,background_blue,text_color_red,text_color_green,text_color_blue,nr_img); // LOG_INFO(TAG, "%d %d %d %d %d %d %d
// ",background_red,background_green,background_blue,text_color_red,text_color_green,text_color_blue,nr_img);
counter = 0; counter = 0;
for (int i = START_DATA; i < REG_LENGTH; i++) { for (int i = START_DATA; i < REG_LENGTH; i++) {
@@ -96,12 +95,12 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
result_txt |= ((uint32_t)text_color_red) << 16; result_txt |= ((uint32_t)text_color_red) << 16;
result_txt |= ((uint32_t)text_color_green) << 8; result_txt |= ((uint32_t)text_color_green) << 8;
result_txt |= text_color_blue; result_txt |= text_color_blue;
// proccesing the image index // Processing the image index
size_t number_of_files = llfs_file_count(); // How many files that there are size_t number_of_files = llfs_file_count(); // How many files that there are
llfs_file_t file_list[number_of_files]; // reserving memory for the list llfs_file_t file_list[number_of_files]; // Reserving memory for the list
number_of_files = llfs_file_list(file_list, number_of_files, NULL); // freeed memory filled with the list number_of_files = llfs_file_list(file_list, number_of_files, NULL); // Freed memory filled with the list
if (number_of_files < nr_img) { if (number_of_files < nr_img) {
lcd_clear_text(); lcd_clear_text();
@@ -109,8 +108,7 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
lcd_stop_all_gifs(); lcd_stop_all_gifs();
lcd_display_text(text, 10, 10, result_txt, result_background, LCD_FONT24); // When no image lcd_display_text(text, 10, 10, result_txt, result_background, LCD_FONT24); // When no image
lcd_display_text("FILE NOT IN FILESYSTEM", 10, 75, LCD_RED, LCD_BLACK, LCD_FONT24); lcd_display_text("FILE NOT IN FILESYSTEM", 10, 75, LCD_RED, LCD_BLACK, LCD_FONT24);
} } else {
else{
const char* ext = strrchr(file_list[nr_img - 1].name, '.'); const char* ext = strrchr(file_list[nr_img - 1].name, '.');
if (ext == NULL) { if (ext == NULL) {
// No valid extension found // No valid extension found
@@ -129,47 +127,37 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
lcd_draw_img_from_fs(file_list[nr_img - 1].name, 0, 75); // BMP on screen lcd_draw_img_from_fs(file_list[nr_img - 1].name, 0, 75); // BMP on screen
} }
} }
} } else {
else{
// LOG_INFO(TAG, "not in writing multiple register mode!!!\n"); // LOG_INFO(TAG, "not in writing multiple register mode!!!\n");
} }
} else if (err == ERR_OK) { } else if (err == ERR_OK) {
tcp_close(pcb); // when everithing was ok close the tcpconnection tcp_close(pcb); // When everything was ok close the TCP connection
} }
return ERR_OK; return ERR_OK;
} }
/** /**
* @fn static err_t modbus_accept(void *arg, struct tcp_pcb *pcb, err_t err) * @fn static err_t modbus_accept(void *arg, struct tcp_pcb *pcb, err_t err)
* @brief Sets the function thats being called when theirs incoming data * @brief Sets the function that's being called when theirs incoming data
*/ */
static err_t modbus_accept(void* arg, struct tcp_pcb* pcb, err_t err) {
static err_t modbus_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
LWIP_UNUSED_ARG(arg); // Eliminates compiler warning about unused arguments LWIP_UNUSED_ARG(arg); // Eliminates compiler warning about unused arguments
LWIP_UNUSED_ARG(err); // Eliminates compiler warning about unused arguments LWIP_UNUSED_ARG(err); // Eliminates compiler warning about unused arguments
tcp_setprio(pcb, TCP_PRIO_MIN); // Sets the priority of a connection. tcp_setprio(pcb, TCP_PRIO_MIN); // Sets the priority of a connection.
tcp_recv(pcb, modbus_incomming_data); // sets which function is being called when new data arives tcp_recv(pcb, modbus_incoming_data); // sets which function is being called when new data arrives
return ERR_OK; return ERR_OK;
} }
/** /**
* @fn void modbus_init * @fn void modbus_init
* @brief Initiallises the modbus tcp * @brief Initializes the modbus tcp
*/ */
void modbus_init(void) {
void modbus_init(void)
{
struct tcp_pcb* modbus_pcb; struct tcp_pcb* modbus_pcb;
modbus_pcb = tcp_new(); // creating a new tcp pcb modbus_pcb = tcp_new(); // Creating a new tcp pcb
tcp_bind(modbus_pcb, IP_ADDR_ANY, MODBUSPORT); // bind the modbus_pcb to port 502 tcp_bind(modbus_pcb, IP_ADDR_ANY, MODBUSPORT); // Bind the modbus_pcb to port 502
modbus_pcb = tcp_listen(modbus_pcb); // listen modbus_pcb = tcp_listen(modbus_pcb); // Listen
tcp_accept(modbus_pcb, modbus_accept); // set callback function tcp_accept(modbus_pcb, modbus_accept); // Set callback function
} }