change modbus to modbus-tcp
This commit is contained in:
92
project/Core/Src/modbus-tcp.c
Normal file
92
project/Core/Src/modbus-tcp.c
Normal file
@@ -0,0 +1,92 @@
|
||||
#include "modbus-tcp.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 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 err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err){
|
||||
int i;
|
||||
int len;
|
||||
char *pc;
|
||||
printf("data incomming\n");
|
||||
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
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 *modbus_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
|
||||
|
||||
modbus_pcb = tcp_listen( modbus_pcb );//listen
|
||||
tcp_accept(modbus_pcb, modbus_accept);//set callback function
|
||||
}
|
||||
Reference in New Issue
Block a user