Fisrt_commit

This commit is contained in:
Obe Van Lierde
2023-11-06 23:48:03 +01:00
parent a912f45961
commit 83db4dcb5d
8 changed files with 7053 additions and 3 deletions

97
project/Core/Src/modbus.c Normal file
View File

@@ -0,0 +1,97 @@
#include "modbus.h"
#include <tcp.h>
#include <string.h>
char tcp_buffer[1024];
struct Data_Modbus {//registers to hold the data of the modbus
uint16_t reg_00;//
uint16_t reg_01;//
uint16_t reg_03;//
};
struct Data_Modbus datamodbus;
/*
static void echo_close (struct tcp_pcb *pcb )
{
tcp_arg(pcb, NULL);
tcp_sent(pcb, NULL);
tcp_recv(pcb, NULL);
tcp_close(pcb);
}
static err_t echo_recv( void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err )
{
int i;
int len;
char *pc;
if ( err == ERR_OK && p != NULL )
{
tcp_recved( pcb, p->tot_len );
pc = (char *)p->payload;
len =p->tot_len;
for( i=0; i<len; i++ )
{
tcp_buffer[i] = pc[i];
}
if( tcp_buffer[0] == 'X' )
echo_close( pcb );
pbuf_free( p );
if( len > tcp_sndbuf( pcb ) )
len= tcp_sndbuf( pcb );
tcp_write( pcb, tcp_buffer, len, 0 );
tcp_sent( pcb, NULL );
}
else
{
pbuf_free( p );
}
if( err == ERR_OK && p == NULL )
{
echo_close( pcb );
}
return ERR_OK;
}
*/
static void modbus_incomming_data(){
void *data;
u16_t len;
}
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( err );//Eliminates compiler warning about unused arguments
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_err( pcb, NULL );//sets the function thats called when an error ocours
tcp_poll( pcb, NULL, 4 );//the aplication is being polled every 4 seconds
tcp_write( pcb, "TEST\r\n", 8, 0 );//pcb , data , lenght in bytes , apiflags
printf("connected\n");
tcp_sent( pcb, NULL );//Specifies the callback function that should be called when data has successfully been received
return ERR_OK;
}
void modbus_init( void )
{
struct tcp_pcb *tcp_pcb;
tcp_pcb = tcp_new();
tcp_bind(tcp_pcb, IP_ADDR_ANY, MODBUSPOORT);
tcp_pcb = tcp_listen( tcp_pcb );
tcp_accept( tcp_pcb, modbus_accept );
}