61 lines
1.9 KiB
C
61 lines
1.9 KiB
C
#include "modbus-tcp.h"
|
|
|
|
#include <tcp.h>
|
|
|
|
char tcp_buffer[1024];
|
|
|
|
#define MAX_REG 100
|
|
|
|
char registers[MAX_REG];
|
|
|
|
static err_t modbus_incomming_data(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err){
|
|
int i;
|
|
int len;
|
|
char *pc;
|
|
|
|
if (p != NULL){
|
|
printf("data not null\n");
|
|
//here im going to procces the modbusdata
|
|
tcp_recved( pcb, p->tot_len );
|
|
pc = (char*)p->payload;
|
|
len =p->tot_len;
|
|
|
|
//putting the bufer in the register array
|
|
for( i=0; i<len; i++ ) {
|
|
registers[i] = pc[i];//getting the error "void value not ignored as it ought to be" on this line
|
|
}
|
|
|
|
if(registers[7] == 0x10){// Check if it's a Modbus Write Multiple Registers request (0x10)
|
|
printf("in writing multiple register mode\n");
|
|
}
|
|
} else if (err == ERR_OK){
|
|
tcp_close(pcb);//when everithing was ok close the tcpconnection
|
|
}
|
|
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
|
|
}
|