I have it set up so that when a client first connects to the TCP socket, the Netburner will send out the current value of each pin.
The TCP message takes the form of "SOCKET_NUM,VALUE\n".
Inside of the socket's TcpAccepted callback, I have this little piece of code. I am using a queue to transfer the messages to the main loop, where I am doing the actual TCP sending.
Code: Select all
for (int i = 7; i < NUM_PINS; i++) {
int currState = Pins[i].read();
int msg = (i << 8) | (currState & 0xFF);
if (OSQPost(&pinQueue, (void *) msg) == OS_Q_FULL) {
iprintf("pinQueue was full\r\n");
}
}
Code: Select all
void * pinQueueStorage[PIN_QUEUE_SIZE];
OSQInit(&pinQueue, pinQueueStorage, PIN_QUEUE_SIZE);
while (1) {
BYTE err;
void * pData = OSQPend(&pinQueue, 0, &err);
if (gpio_sock) {
if (pData) {
int message = (int) pData;
int pin = (message >> 8) & 0xFF;
int value = (message & 0xFF);
tcp_iprintf(gpio_sock, "%d,%d\r\n", pin, value);
} else {
iprintf("OSQPend failed with error code: %d\r\n", err);
}
}
}
For example, with the above code, if I connect to the socket using telnet, I will get a response of
Code: Select all
7,1
8,1
9,1
10,1
11,1
12,1
Code: Select all
13,1
14,1
15,1