一、简单说明
利用之前的功能,将数据通路打通,即开发板连接上wifi后,尝试连接服务器,连接成功服务器后可以进行数据通信。将读到的nfc卡信息和按键信息发送给服务器。
二、遇到的问题
在向服务器发送数据的时候,例程里面是用的write函数,
write(sock, recv_data, recv_data_len);
连接上服务器后,能收到服务器发送过来的数据,但是使用write接口始终发送不了给服务器。一度怀疑是服务器的问题。找了很久才知道要用send函数。这点很坑!!!!
send(sock, recv_data, strlen(recv_data), 0);
三、大体测试代码
测试代码,能实现大体功能,健壮性就暂时不考虑了、、、
static void user_app(void)
{
unsigned char nfcFlag = 0;
unsigned char nfc_Buf[4] = {0};
unsigned char key_value = 0;
int sock = -1;
struct sockaddr_in client_addr;
char recv_data[512] = {0};
int recv_data_len;
led_init();
WifiConnect(WIFI_NAME, WIFI_PWD);
printf("start wifi_test test\r\n");
adc_init();
NfcExtExample();
while (1)
{
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("Socket error\n");
osDelay(OD_DELAY_100);
continue;
}
memset_s(&(client_addr), sizeof(client_addr), 0, sizeof(client_addr));
client_addr.sin_family = AF_INET;
client_addr.sin_port = htons(SERVER_PORT);
client_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
printf("try connect to server "SERVER_IP" :%d \n", SERVER_PORT);
if (connect(sock, (struct sockaddr *)&client_addr, sizeof(struct sockaddr)) == -1) {
closesocket(sock);
osDelay(OD_DELAY_1000);
continue;
}
printf("Connect to tcp server successful!\n");
while (1) {
key_value = key_process();
if (key_value == 4)
{
memset(recv_data, 0, 100);
sprintf(recv_data, "key up press\r\n");
}
else if (key_value == 5)
{
memset(recv_data, 0, 100);
sprintf(recv_data, "key down press\r\n");
}
if(key_value)
{
led_blue_on();
send(sock, recv_data, strlen(recv_data), 0);
}
nfcFlag = get_NFC_ID(nfc_Buf);
if (nfcFlag)
{
led_blue_on();
memset(recv_data, 0, 100);
sprintf(recv_data, "NFC ID: %02X %02X %02X %02X\r\n", nfc_Buf[0], nfc_Buf[1], nfc_Buf[2], nfc_Buf[3]);
send(sock, recv_data, strlen(recv_data), 0);
}
}
}
}