下面这个写法经过测试后是可行的。。。
写法一:
#include "tcp_client_demo.h"
#include "lwip/opt.h"
#include "lwip_comm.h"
#include "lwip/lwip_sys.h"
#include "lwip/api.h"
#include "includes.h"
#include "key.h"
#include "ADC.h"
#include "led.h"
//////////////////////////////////////////////////////////////////////////////////
struct netconn *tcp_clientconn; //TCP CLIENTíøÂçᬽó½á11ìå
u8 tcp_client_recvbuf[TCP_CLIENT_RX_BUFSIZE]; //TCP¿í»§¶Ë½óêÕêy¾Y»o3åÇø
u8 *tcp_client_sendbuf="ENC28J60 NETCONN TCP Client send data
"; //TCP¿í»§¶Ë·¢Ëíêy¾Y»o3åÇø
u8 tcp_client_flag; //TCP¿í»§¶Ëêy¾Y·¢Ëí±ê־λ
#define K 8000
int value,value1,value2,value3;
char data[22];
//TCP¿í»§¶ËèÎÎñ
#define TCPCLIENT_PRIO 8
//èÎÎñ¶ÑÕ»′óD¡
#define TCPCLIENT_STK_SIZE 300
//èÎÎñ¶ÑÕ»
OS_STK TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE];
//tcp¿í»§¶ËèÎÎñoˉêy
static void tcp_client_thread(void *arg)
{
OS_CPU_SR cpu_sr;
u32 data_len = 0;
struct pbuf *q;
err_t err,recv_err;
static ip_addr_t server_ipaddr,loca_ipaddr;
static u16_t server_port,loca_port;
LWIP_UNUSED_ARG(arg);
server_port = REMOTE_PORT;
IP4_ADDR(&server_ipaddr, lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3]);
while (1)
{
tcp_clientconn = netconn_new(NETCONN_TCP); //′′½¨ò»¸öTCPá′½ó
err = netconn_connect(tcp_clientconn,&server_ipaddr,server_port);//ᬽó·tÎñÆ÷
if(err != ERR_OK)
{
netconn_delete(tcp_clientconn); //·μ»ØÖμ2»μèóúERR_OK,é¾3ytcp_clientconnᬽó
}
else if (err == ERR_OK) //′|àíDÂᬽóμÄêy¾Y
{
struct netbuf *recvbuf;
tcp_clientconn->recv_timeout = 10;
netconn_getaddr(tcp_clientconn,&loca_ipaddr,&loca_port,1); //»ñ衱¾μØIPÖ÷»úIPμØÖ·oí¶Ë¿úoÅ
printf("ᬽóéÏ·tÎñÆ÷%d.%d.%d.%d,±¾»ú¶Ë¿úoÅÎa:%d
",lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3],loca_port);
while(1)
{
if((tcp_client_flag & LWIP_SEND_DATA) == LWIP_SEND_DATA) //óDêy¾Yòa·¢Ëí
{
int i=0;
for(i=0;i<K;i++)
{
value=Get_Adc(ADC_Channel_10);
value1=Get_Adc(ADC_Channel_11);
sprintf((char*)data,"%5d%5d%5s%5d
",value,value1,"curv",i);
err = netconn_write(tcp_clientconn ,(char*)data,strlen((char*)data),NETCONN_COPY); //·¢Ëíêy¾Y
}
if(err != ERR_OK)
{
printf("·¢Ëí꧰ü
");
}
tcp_client_flag &= ~LWIP_SEND_DATA;
}
else
{
value=Get_Adc(ADC_Channel_10);
value1=Get_Adc(ADC_Channel_11);
sprintf((char*)data,"%5d%5d%5s%5s
",value,value1,"Dint","Dint");
err = netconn_write(tcp_clientconn ,(char*)data,strlen((char*)data),NETCONN_COPY); //·¢Ëíêy¾Y
}
if((recv_err = netconn_recv(tcp_clientconn,&recvbuf)) == ERR_OK) //½óêÕμ½êy¾Y
{
OS_ENTER_CRITICAL(); //1ØÖD¶Ï
memset(tcp_client_recvbuf,0,TCP_CLIENT_RX_BUFSIZE); //êy¾Y½óêÕ»o3åÇøÇåáã
for(q=recvbuf->p;q!=NULL;q=q->next) //±éàúíêÕû¸öpbufá′±í
{
//ÅD¶Ïòa¿½±′μ½TCP_CLIENT_RX_BUFSIZEÖDμÄêy¾YêÇ·ñ′óóúTCP_CLIENT_RX_BUFSIZEμÄê£óà¿Õ¼ä£¬èç1û′óóú
//μÄ»°¾íÖ»¿½±′TCP_CLIENT_RX_BUFSIZEÖDê£óà3¤¶èμÄêy¾Y£¬·ñÔòμÄ»°¾í¿½±′ËùóDμÄêy¾Y
if(q->len > (TCP_CLIENT_RX_BUFSIZE-data_len)) memcpy(tcp_client_recvbuf+data_len,q->payload,(TCP_CLIENT_RX_BUFSIZE-data_len));//¿½±′êy¾Y
else memcpy(tcp_client_recvbuf+data_len,q->payload,q->len);
data_len += q->len;
if(data_len > TCP_CLIENT_RX_BUFSIZE) break; //3¬3öTCP¿í»§¶Ë½óêÕêy×é,ìø3ö
}
OS_EXIT_CRITICAL(); //¿aÖD¶Ï
data_len=0; //¸′ÖÆíê3éoódata_lenòaÇåáã¡£
printf("%s
",tcp_client_recvbuf);
netbuf_delete(recvbuf);
}else if(recv_err == ERR_CLSD) //1رÕᬽó
{
netconn_close(tcp_clientconn);
netconn_delete(tcp_clientconn);
printf("·tÎñÆ÷%d.%d.%d.%d¶Ï¿aᬽó
",lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3]);
break;
}
}
}
}
}
//′′½¨TCP¿í»§¶ËÏß3ì
//·μ»ØÖμ:0 TCP¿í»§¶Ë′′½¨3é1|
// ÆäËû TCP¿í»§¶Ë′′½¨ê§°ü
INT8U tcp_client_init(void)
{
INT8U res;
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL(); //1ØÖD¶Ï
res = OSTaskCreate(tcp_client_thread,(void*)0,(OS_STK*)&TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE-1],TCPCLIENT_PRIO); //′′½¨TCP¿í»§¶ËÏß3ì
OS_EXIT_CRITICAL(); //¿aÖD¶Ï
return res;
}
写法二 :写法二就报0xfa, Illegal value.
#include "tcp_client_demo.h"
#include "lwip/opt.h"
#include "lwip_comm.h"
#include "lwip/lwip_sys.h"
#include "lwip/api.h"
#include "includes.h"
#include "key.h"
#include "ADC.h"
#include "led.h"
//////////////////////////////////////////////////////////////////////////////////
struct netconn *tcp_clientconn; //TCP CLIENTíøÂçᬽó½á11ìå
u8 tcp_client_recvbuf[TCP_CLIENT_RX_BUFSIZE]; //TCP¿í»§¶Ë½óêÕêy¾Y»o3åÇø
u8 tcp_client_flag; //TCP¿í»§¶Ëêy¾Y·¢Ëí±ê־λ
#define K 8000
int value,value1;
char data[22];
int i=0;
u32 data_len = 0;
struct pbuf *q;
err_t err,recv_err;
static ip_addr_t server_ipaddr,loca_ipaddr;
static u16_t server_port,loca_port;
//TCP¿í»§¶ËèÎÎñ
#define TCPCLIENT_PRIO 8
//èÎÎñ¶ÑÕ»′óD¡
#define TCPCLIENT_STK_SIZE 300
//èÎÎñ¶ÑÕ»
OS_STK TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE];
//tcp¿í»§¶ËèÎÎñoˉêy
static void tcp_client_thread(void *arg)
{
OS_CPU_SR cpu_sr;
LWIP_UNUSED_ARG(arg);
server_port = REMOTE_PORT;
IP4_ADDR(&server_ipaddr, lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3]);
while (1)
{
tcp_clientconn = netconn_new(NETCONN_TCP); //′′½¨ò»¸öTCPá′½ó
err = netconn_connect(tcp_clientconn,&server_ipaddr,server_port);//ᬽó·tÎñÆ÷
if(err != ERR_OK)
{
netconn_delete(tcp_clientconn); //·μ»ØÖμ2»μèóúERR_OK,é¾3ytcp_clientconnᬽó
}
else if (err == ERR_OK) //′|àíDÂᬽóμÄêy¾Y
{
struct netbuf *recvbuf;
tcp_clientconn->recv_timeout = 10;
netconn_getaddr(tcp_clientconn,&loca_ipaddr,&loca_port,1); //»ñ衱¾μØIPÖ÷»úIPμØÖ·oí¶Ë¿úoÅ
printf("ᬽóéÏ·tÎñÆ÷%d.%d.%d.%d,±¾»ú¶Ë¿úoÅÎa:%d
",lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3],loca_port);
while(1)
{
if((recv_err = netconn_recv(tcp_clientconn,&recvbuf)) == ERR_OK) //½óêÕμ½êy¾Y
{
OS_ENTER_CRITICAL(); //1ØÖD¶Ï
memset(tcp_client_recvbuf,0,TCP_CLIENT_RX_BUFSIZE); //êy¾Y½óêÕ»o3åÇøÇåáã
for(q=recvbuf->p;q!=NULL;q=q->next) //±éàúíêÕû¸öpbufá′±í
{
//ÅD¶Ïòa¿½±′μ½TCP_CLIENT_RX_BUFSIZEÖDμÄêy¾YêÇ·ñ′óóúTCP_CLIENT_RX_BUFSIZEμÄê£óà¿Õ¼ä£¬èç1û′óóú
//μÄ»°¾íÖ»¿½±′TCP_CLIENT_RX_BUFSIZEÖDê£óà3¤¶èμÄêy¾Y£¬·ñÔòμÄ»°¾í¿½±′ËùóDμÄêy¾Y
if(q->len > (TCP_CLIENT_RX_BUFSIZE-data_len)) memcpy(tcp_client_recvbuf+data_len,q->payload,(TCP_CLIENT_RX_BUFSIZE-data_len));//¿½±′êy¾Y
else memcpy(tcp_client_recvbuf+data_len,q->payload,q->len);
data_len += q->len;
if(data_len > TCP_CLIENT_RX_BUFSIZE) break; //3¬3öTCP¿í»§¶Ë½óêÕêy×é,ìø3ö
}
OS_EXIT_CRITICAL(); //¿aÖD¶Ï
data_len=0; //¸′ÖÆíê3éoódata_lenòaÇåáã¡£
printf("%s
",tcp_client_recvbuf);
netbuf_delete(recvbuf);
}else if(recv_err == ERR_CLSD) //1رÕᬽó
{
netconn_close(tcp_clientconn);
netconn_delete(tcp_clientconn);
printf("·tÎñÆ÷%d.%d.%d.%d¶Ï¿aᬽó
",lwipdev.remoteip[0],lwipdev.remoteip[1], lwipdev.remoteip[2],lwipdev.remoteip[3]);
break;
}
}
}
}
}
//′′½¨TCP¿í»§¶ËÏß3ì
//·μ»ØÖμ:0 TCP¿í»§¶Ë′′½¨3é1|
// ÆäËû TCP¿í»§¶Ë′′½¨ê§°ü
INT8U tcp_client_init(void)
{
INT8U res;
OS_CPU_SR cpu_sr;
OS_ENTER_CRITICAL(); //1ØÖD¶Ï
res = OSTaskCreate(tcp_client_thread,(void*)0,(OS_STK*)&TCPCLIENT_TASK_STK[TCPCLIENT_STK_SIZE-1],TCPCLIENT_PRIO); //′′½¨TCP¿í»§¶ËÏß3ì
OS_EXIT_CRITICAL(); //¿aÖD¶Ï
return res;
}
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê±Öóê1Äü
//¶¨ê±Æ÷TIM33õê¼»ˉ
TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIMÏòéϼÆêyÄ£ê½
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //ê1ÄüÖ¸¶¨μÄTIM3ÖD¶Ï,ÔêDí¸üDÂÖD¶Ï
//ÖD¶ÏóÅÏ輶NVICéèÖÃ
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //ÏèÕ¼óÅÏ輶1¼¶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //′óóÅÏ輶3¼¶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //3õê¼»ˉNVIC¼Ä′æÆ÷
TIM_Cmd(TIM3, ENABLE); //ê1ÄüTIMx
}
//¶¨ê±Æ÷3ÖD¶Ï·tÎñoˉêy
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //òç3öÖD¶Ï
{
if((tcp_client_flag & LWIP_SEND_DATA) == LWIP_SEND_DATA) //óDêy¾Yòa·¢Ëí
{
value=Get_Adc(ADC_Channel_10);
value1=Get_Adc(ADC_Channel_11);
sprintf((char*)data,"%5d%5d%5s%5d
",value,value1,"curv",i);
i++;
err = netconn_write(tcp_clientconn ,(char*)data,strlen((char*)data),NETCONN_COPY); //·¢Ëíêy¾Y
if(err != ERR_OK)
{
printf("·¢Ëí꧰ü
");
}
if(i>=7999)
{
tcp_client_flag &= ~LWIP_SEND_DATA;
i=0;
printf("8000ìõêy¾Yíê3é");
}
}
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //Çå3yÖD¶Ï±ê־λ
}
以上两个写法我自己认为没有大的改动。。。为什么会报警。。请大虾们帮我解决下。。
一周热门 更多>