lwIP TCP client

This forum is about you. Feel free to discuss anything is related to embedded and electronics, your awesome projects, your ideas, your announcements, not necessarily related to ChibiOS but to embedded in general.
nojwa
Posts: 8
Joined: Sat Oct 13, 2018 9:05 pm
Has thanked: 1 time

lwIP TCP client

Postby nojwa » Sun Nov 18, 2018 8:35 pm

Exists somewhere example for TCP client over lwip (ST's family)?

My test program end with err_t == -4.

Have you some experiences? Some lwipopt for tcp client ... anything?

Error output:
[018.434] [./src/ethcomm.c:ethCommThread] start client thread
[018.442] [./src/ethcomm.c:ethCommThread] binded: 0, state: 0
[018.447] [./src/ethcomm.c:ethCommThread] connected: -4, state: 0

code:

static void ethMacAddress(struct lwipthread_opts *ipSettings)
{
uint8_t mac[6];

mac[0] = 0x06;
mac[1] = 0x*;
mac[2] = 0x*;
mac[3] = 0x02;
mac[4] = 0x03;
mac[5] = 0x04;

dhexprint(mac, 6);

ipSettings->macaddress = mac;
}

static void ethIpSettings(struct lwipthread_opts *ipSettings)
{
ip_addr_t ip, netmask;

IP4_ADDR(&ip, *, *, 6, 102);

ipSettings->address = ip.addr;
IP4_ADDR(&netmask, 255, 255, 255, 0);
ipSettings->netmask = netmask.addr;
ipSettings->addrMode = NET_ADDRESS_STATIC;
}

static THD_WORKING_AREA(ethThread_wa, 2048);
static thread_t *ethThread = NULL;

static THD_FUNCTION(ethCommThread, arg)
{
(void) arg;

dprint("start client thread\n");
chRegSetThreadName("tcp_client");

struct netconn *conn;
err_t err;

/* Create a new TCP connection handle */
conn = netconn_new(NETCONN_TCP);
LWIP_ERROR("tcp client: invalid conn", (conn != NULL), chThdExit(MSG_RESET););

/* Bind to port 2000 with default IP address */
err = netconn_bind(conn, IP_ADDR_ANY, 2000);
dprint("binded: %d, state: %d\n", err, conn->state);

ip_addr_t serverIp;
IP4_ADDR(&serverIp, *, *, 6, 110);

err = netconn_connect(conn, &serverIp, 6000);
dprint("connected: %d, state: %d\n", err, conn->state);
}

uint8_t ethernetInit()
{
struct lwipthread_opts ipSettings;
ethIpSettings(&ipSettings);

ethMacAddress(&ipSettings);
lwipInit(&ipSettings);

ethThread = chThdCreateStatic(ethThread_wa, sizeof(ethThread_wa), NORMALPRIO, ethCommThread, NULL);
return 0;
}

mikeprotts
Posts: 9
Joined: Wed Jan 09, 2019 12:37 pm
Been thanked: 2 times

Re: lwIP TCP client

Postby mikeprotts » Fri Jan 11, 2019 12:29 pm

From ChibiOS_18.2.1/ext/lwip/src/include/lwip/err.h, -4 would be a routing problem.

I assume the * markers for IP addess are within the same subnet (otherwise you'll need the gateway address set).

I'd suggest moving most variables to global and declaring as static. Also for the client port you know the address, and shouldn't care about port number, so try changing the bind to:
netconn_bind(conn, &ip, 0 );

Ideally also print out the ip addresses from the thread code, so you check they are as expected.
Mike


Return to “User Projects”

Who is online

Users browsing this forum: No registered users and 1 guest