我现在已经比较清楚如何建立TCP连接,发送TCP数据了,但是现在我想,如果PA0由高电平变化到低电平,那么单片机就向电脑不停地发送十六进制1,这段代码怎么加在程序里面呢?下面贴出我原来的代码,这段代码原本是用于TELNET上面,但是我只要有TCP数据就可以了。
#include "ziji.h"
#include "stm3210c_eval_lcd.h"
#include "lwip/tcp.h"
#include <string.h>
#include <stdio.h>
#define GREETING "欢迎进入8001端口\n\r\n\r"
extern const uint8_t STM32F10x_STR[];
#define HELLO "回答:"
#define MAX_NAME_SIZE 32
extern const uint8_t STM32F10x_STR[];
{
int length;
char bytes[MAX_NAME_SIZE];
};
static err_t ziji_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err);
static err_t ziji_accept(void *arg, struct tcp_pcb *pcb, err_t err);
static void ziji_conn_err(void *arg, err_t err);
static err_t ziji_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
{
struct pbuf *q;
struct name *name = (struct name *)arg;
int done;
char *c;
int i;
if (p != NULL)
{
tcp_recved(pcb, p->tot_len);
if(!name)
{
pbuf_free(p);
return ERR_ARG;
}
done = 0;
for(q=p; q != NULL; q = q->next)
{
c = q->payload;
for(i=0; i<q->len && !done; i++)
{
done = ((c == '\r') || (c == '\n'));
if(name->length < MAX_NAME_SIZE)
{
name->bytes[name->length++] = c;
}
}
}
if(done)
{
if(name->bytes[name->length-2] != '\r' || name->bytes[name->length-1] != '\n')
{
if((name->bytes[name->length-1] == '\r' || name->bytes[name->length-1] == '\n') && (name->length+1 <= MAX_NAME_SIZE))
{
name->length += 1;
}
else if(name->length+2 <= MAX_NAME_SIZE)
{
name->length += 2;
}
else
{
name->length = MAX_NAME_SIZE;
}
name->bytes[name->length-2] = '\r';
name->bytes[name->length-1] = '\n';
}
tcp_write(pcb, HELLO, strlen(HELLO), 1);
tcp_write(pcb, name->bytes, name->length, TCP_WRITE_FLAG_COPY);
tcp_write(pcb, "\n\r", strlen("\n\r"), 1);
name->length = 0;
}
pbuf_free(p);
}
else if (err == ERR_OK)
{
mem_free(name);
return tcp_close(pcb);
}
return ERR_OK;
}
static err_t ziji_accept(void *arg, struct tcp_pcb *pcb, err_t err)
{
u32_t IPaddress;
u8_t iptxt[50];
volatile u8_t iptab[4];
IPaddress = pcb->remote_ip.addr;
iptab[0] = (u8_t)(IPaddress >> 24);
iptab[1] = (u8_t)(IPaddress >> 16);
iptab[2] = (u8_t)(IPaddress >> 8);
iptab[3] = (u8_t)(IPaddress);
LCD_DisplayStringLine(Line5, iptxt);
tcp_arg(pcb, mem_calloc(sizeof(struct name), 1));
tcp_err(pcb, ziji_conn_err);
tcp_recv(pcb, ziji_recv);
tcp_write(pcb, GREETING, strlen(GREETING), 1);
return ERR_OK;
}
void ziji_init(void)
{
struct tcp_pcb *pcb;
pcb = tcp_new();
tcp_bind(pcb, IP_ADDR_ANY, 8001);
pcb = tcp_listen(pcb);
tcp_accept(pcb, ziji_accept);
}
static void ziji_conn_err(void *arg, err_t err)
{
struct name *name;
name = (struct name *)arg;
mem_free(name);
}
请教大家,到底怎么修改呢? |