resolved changes
Disabled logger, it gave me errors on 'TAG'
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
/*
|
/**
|
||||||
* modbus.h
|
* @file modbus_tcp.h
|
||||||
*
|
*
|
||||||
* Created on: 6 nov. 2023
|
* @brief TCP Modbus handler
|
||||||
|
* Created on: Nov 6, 2023
|
||||||
* Author: Obe
|
* Author: Obe
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef INC_MODBUS_H_
|
#ifndef INC_MODBUS_H_
|
||||||
#define INC_MODBUS_H_
|
#define INC_MODBUS_H_
|
||||||
#define MODBUSPOORT 502 //is the default
|
#define MODBUSPORT 502 //is the default
|
||||||
|
|
||||||
|
|
||||||
#include <tcp.h>
|
#include <tcp.h>
|
||||||
@@ -18,6 +19,12 @@
|
|||||||
#include "llfs.h"
|
#include "llfs.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn void modbus_init
|
||||||
|
* @brief Initiallises the modbus tcp
|
||||||
|
*/
|
||||||
|
|
||||||
void modbus_init(void);
|
void modbus_init(void);
|
||||||
|
|
||||||
#endif /* INC_MODBUS_H_ */
|
#endif /* INC_MODBUS_H_ */
|
||||||
|
|||||||
@@ -1,13 +1,36 @@
|
|||||||
#include <modbus_tcp.h>
|
/**
|
||||||
|
* @file modbus_tcp.c
|
||||||
|
*
|
||||||
|
* @brief TCP Modbus handler
|
||||||
|
* Created on: Nov 6, 2023
|
||||||
|
* Author: Obe
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Includes
|
||||||
|
#include <modbus_tcp.h>
|
||||||
|
#include "log.h"
|
||||||
|
|
||||||
|
// Defines
|
||||||
#define MAX_REG 250
|
#define MAX_REG 250
|
||||||
#define EXTENSION_LENGHT 4
|
#define EXTENSION_LENGHT 4
|
||||||
#define TEXT_LENGHT 200
|
#define TEXT_LENGHT 200
|
||||||
#define MULTIPLE_REG 0x10
|
#define MULTIPLE_REG 0x10
|
||||||
#define ASCII_UNDERSCORE 95
|
|
||||||
|
|
||||||
|
|
||||||
|
// Global variables
|
||||||
char registers[MAX_REG];
|
char registers[MAX_REG];
|
||||||
|
//const char* TAG = "Modbus_TCP"; // Tag used in logs
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
static err_t modbus_incomming_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);
|
||||||
|
void modbus_init(void);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn static err_t modbus_incomming_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.
|
||||||
|
* It handles the incomming data from Qmodmaster
|
||||||
|
*/
|
||||||
|
|
||||||
static err_t modbus_incomming_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){
|
||||||
int j;
|
int j;
|
||||||
@@ -15,7 +38,6 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
|
|||||||
int lenght;
|
int lenght;
|
||||||
char extention[EXTENSION_LENGHT];
|
char extention[EXTENSION_LENGHT];
|
||||||
char *pc;
|
char *pc;
|
||||||
char *textstring;
|
|
||||||
char text[TEXT_LENGHT];
|
char text[TEXT_LENGHT];
|
||||||
uint8_t bg_red = 0;
|
uint8_t bg_red = 0;
|
||||||
uint8_t bg_green = 0;
|
uint8_t bg_green = 0;
|
||||||
@@ -27,27 +49,23 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
|
|||||||
uint32_t result_bg = 0;
|
uint32_t result_bg = 0;
|
||||||
uint32_t result_txt = 0;
|
uint32_t result_txt = 0;
|
||||||
|
|
||||||
for(i = 0; i < 100; i++){
|
memcpy(text,'_',TEXT_LENGHT); // Putting underscores in the whole array
|
||||||
text[i] = ASCII_UNDERSCORE; // Putting underscores in the whole array
|
text[TEXT_LENGHT-1] = '\0';
|
||||||
}
|
|
||||||
text[199] = '\0';
|
|
||||||
|
|
||||||
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
|
// here im going to procces the modbusdata
|
||||||
tcp_recved( pcb, p->tot_len );
|
tcp_recved( pcb, p->tot_len );
|
||||||
pc = (char*)p->payload;
|
pc = (char*)p->payload;
|
||||||
len = p->tot_len;
|
len = p->tot_len;
|
||||||
|
|
||||||
// putting the bufer in the register array
|
|
||||||
for(int i = 0; i < len; i++) {
|
for(int i = 0; i < 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)
|
||||||
LOG_INFO(TAG, "in writing multiple register mode\n");
|
//LOG_INFO(TAG, "in writing multiple register mode\n");
|
||||||
}
|
|
||||||
// putting the values from the array in their variable
|
|
||||||
bg_red = (uint8_t)(registers[14]); // 01
|
bg_red = (uint8_t)(registers[14]); // 01
|
||||||
bg_green = (uint8_t)(registers[16]); // 02
|
bg_green = (uint8_t)(registers[16]); // 02
|
||||||
bg_blue = (uint8_t)(registers[18]); // 03
|
bg_blue = (uint8_t)(registers[18]); // 03
|
||||||
@@ -56,17 +74,15 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
|
|||||||
txt_blue = (uint8_t)(registers[24]); // 06
|
txt_blue = (uint8_t)(registers[24]); // 06
|
||||||
nr_img = (uint8_t)(registers[26]); // 07
|
nr_img = (uint8_t)(registers[26]); // 07
|
||||||
|
|
||||||
LOG_INFO(TAG, "%d %d %d %d %d %d %d ",bg_red,bg_green,bg_blue,txt_red,txt_green,txt_blue,nr_img);
|
//LOG_INFO(TAG, "%d %d %d %d %d %d %d ",bg_red,bg_green,bg_blue,txt_red,txt_green,txt_blue,nr_img);
|
||||||
|
|
||||||
j = 0;
|
j = 0;
|
||||||
for(i = 28; i < 428; i++){
|
for(int i = 28; i < 428; i++){
|
||||||
if(i % 2 == 0){
|
if(i % 2 == 0){
|
||||||
text[j] = registers[i];
|
text[j] = registers[i];
|
||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// processing the text data to screen
|
|
||||||
textstring = text;
|
|
||||||
|
|
||||||
result_bg = 0xff000000;
|
result_bg = 0xff000000;
|
||||||
result_bg |= ((uint32_t)bg_red) << 16;
|
result_bg |= ((uint32_t)bg_red) << 16;
|
||||||
@@ -78,16 +94,18 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
|
|||||||
result_txt |= ((uint32_t)txt_green) << 8;
|
result_txt |= ((uint32_t)txt_green) << 8;
|
||||||
result_txt |= txt_blue;
|
result_txt |= txt_blue;
|
||||||
// proccesing the image index
|
// proccesing the image index
|
||||||
size_t number_of_files = llfs_file_count(); // hoeveel files er zijn
|
size_t number_of_files = llfs_file_count(); // How many files that there are
|
||||||
|
|
||||||
llfs_file_t file_list[number_of_files]; // geheugen vrijmaken voor files
|
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); // vrijgemaakt geheugen invullen met de lijst
|
number_of_files = llfs_file_list(file_list, number_of_files, NULL); // freeed memory filled with the list
|
||||||
|
|
||||||
if(number_of_files < nr_img){
|
if(number_of_files < nr_img){
|
||||||
lcd_clear_txt(void);
|
lcd_clear_text();
|
||||||
lcd_clear_images(void);
|
lcd_clear_images();
|
||||||
lcd_display_text("FILE NOT IN FILESYSTEM", 10, 10, LCD_RED, LCD_BLACK, LCD_FONT24);
|
lcd_stop_all_gifs();
|
||||||
|
lcd_display_text(text, 10, 10, result_txt, result_bg, LCD_FONT24); //When no image
|
||||||
|
lcd_display_text("FILE NOT IN FILESYSTEM", 10, 75, LCD_RED, LCD_BLACK, LCD_FONT24);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
lenght = strlen(file_list[nr_img - 1].name); //lenght from filename
|
lenght = strlen(file_list[nr_img - 1].name); //lenght from filename
|
||||||
@@ -96,24 +114,36 @@ static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *
|
|||||||
extention[3] = '\0'; //adding the \0
|
extention[3] = '\0'; //adding the \0
|
||||||
|
|
||||||
if(strcmp(extention,"gif") == 0){
|
if(strcmp(extention,"gif") == 0){
|
||||||
lcd_clear_txt(void);
|
lcd_clear_text();
|
||||||
lcd_clear_images(void);
|
lcd_clear_images();
|
||||||
lcd_display_text(textstring, 10, 10, result_txt, result_bg, LCD_FONT24);
|
lcd_stop_all_gifs();
|
||||||
|
lcd_display_text(text, 10, 10, result_txt, result_bg, LCD_FONT24);
|
||||||
lcd_gif_t* gif = lcd_draw_gif_from_fs(file_list[nr_img - 1].name, 0, 75); //GIF on screen
|
lcd_gif_t* gif = lcd_draw_gif_from_fs(file_list[nr_img - 1].name, 0, 75); //GIF on screen
|
||||||
}
|
}
|
||||||
if(strcmp(extention,"bmp") == 0){
|
if(strcmp(extention,"bmp") == 0){
|
||||||
lcd_clear_txt(void);
|
lcd_clear_text();
|
||||||
lcd_clear_images(void);
|
lcd_clear_images();
|
||||||
lcd_display_text(textstring, 10, 10, result_txt, result_bg, LCD_FONT24);
|
lcd_stop_all_gifs();
|
||||||
|
lcd_display_text(text, 10, 10, result_txt, result_bg, LCD_FONT24);
|
||||||
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{
|
||||||
|
//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 everithing was ok close the tcpconnection
|
||||||
}
|
}
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
|
|
||||||
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
|
||||||
@@ -124,11 +154,17 @@ static err_t modbus_accept(void *arg, struct tcp_pcb *pcb, err_t err)
|
|||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @fn void modbus_init
|
||||||
|
* @brief Initiallises 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, MODBUSPOORT); // 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
|
||||||
|
|||||||
Reference in New Issue
Block a user