要点:
串口转int:
printf("%d\n",atoi(argv[2]));
获取信号强度:
int fill_ntfd_common_info() { FILE * fp; char link_str[8]; int i, link_len; int link_percent; fp = popen("iwinfo | grep \"Quality\" | awk '{print $6}'", "r"); fgets(link_str, sizeof(link_str), fp); pclose(fp); link_len = strlen(link_str);// include '\0' ;link_str is unknown/70 or link_str is 48/70 i = 0; while (i < link_len) { //if (link_str[i] == '\\') { if (link_str[i] == 'u') { //if the quality is "unknown/70" link_str[i] = 0; break; } i++; } //link_percent = atoi(link_str)*10/7; //get the molecular link_percent = atoi(link_str)*51/14; return link_percent; }
socket断线重连:
//if( send(sockfd, sendline, strlen(sendline), 0) < 0) if( send(sockfd, sendline, 29, 0) < 0) //array { printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); //exit(0); //reconnect close(sockfd); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket Error!!! Error # is %d. Exit!\n", errno); exit(-1); } while(-1 == connect(sockfd,(struct sockaddr *)(&servaddr), sizeof(struct sockaddr))) { printf("Reconnect to server fail !\n"); sleep(10); } printf("send msg to server: \n"); }
1、src文件夹下源码
//客户端 #include<stdio.h> #include<stdlib.h> #include<string.h> #include<errno.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #define MAXLINE 4096 int fill_ntfd_common_info() { FILE * fp; char link_str[8]; int i, link_len; int link_percent; fp = popen("iwinfo | grep \"Quality\" | awk '{print $6}'", "r"); fgets(link_str, sizeof(link_str), fp); pclose(fp); link_len = strlen(link_str);// include '\0' ;link_str is unknown/70 or link_str is 48/70 i = 0; while (i < link_len) { //if (link_str[i] == '\\') { if (link_str[i] == 'u') { //if the quality is "unknown/70" link_str[i] = 0; break; } i++; } //link_percent = atoi(link_str)*10/7; //get the molecular link_percent = atoi(link_str)*51/14; return link_percent; } int main(int argc, char** argv) { int sockfd, n; char recvline[4096];//, sendline[4096]; char sendline[29] = {0x0D,0x00,0xD2,0x19,//头部 0x2C,0x08,0x04,//源,第三个为类型,00无效;01主机;02基站;03为CS;04节点 0x00,0x00,0x01,//目标,第三个为类型, 0x00,0x07,0x94,0x11,0xAB,0x4C,0x2B,0xE6,0x00,//固定 0x02,0x04,0x02,0x01, //4通道,4个字节,格式2,第一通道 0x00,0x00,//数据 0x2E,0x20,0x0D,0x0A};//结束 struct sockaddr_in servaddr; int sret; if( argc != 3){ printf("usage: tcpclient <ipaddress> <seconds>\n"); exit(0); } if( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0){ printf("create socket error: %s(errno: %d)\n", strerror(errno),errno); //exit(0); return -1; } memset(&servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_port = htons(10001); if( inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0){ printf("inet_pton error for %s\n",argv[1]); //exit(0); return -1; } if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){ printf("connect error: %s(errno: %d)\n",strerror(errno),errno); //exit(0); sleep(10); } printf("send msg to server: \n"); //fgets(sendline, 4096, stdin); while(1) { sendline[14] = fill_ntfd_common_info(); //if( send(sockfd, sendline, strlen(sendline), 0) < 0) if( send(sockfd, sendline, 29, 0) < 0) //array { printf("send msg error: %s(errno: %d)\n", strerror(errno), errno); //exit(0); //reconnect close(sockfd); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket Error!!! Error # is %d. Exit!\n", errno); exit(-1); } while(-1 == connect(sockfd,(struct sockaddr *)(&servaddr), sizeof(struct sockaddr))) { printf("Reconnect to server fail !\n"); sleep(10); } printf("send msg to server: \n"); } sleep(atoi(argv[2])); } close(sockfd); exit(0); }
2、src下Makefile
all: tcpclient tcpclient: tcpclient.c $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< clean: rm -f tcpclient
3、tcpclient下Makefile
# # Copyright (C) 2010-2012 OpenWrt.org # # This is free software, licensed under the GNU General Public License v2. # See /LICENSE for more information. # # Quality; # reconnect; # return -1; # include $(TOPDIR)/rules.mk PKG_NAME:=tcpclient PKG_VERSION:=0.1.3 PKG_RELEASE:=1 include $(INCLUDE_DIR)/package.mk define Package/tcpclient SECTION:=utils CATEGORY:=Utilities TITLE:=tcpclient MAINTAINER:=tingpan PKGARCH:=all endef define Package/tcpclient/description this is a tcp client endef define Build/Prepare mkdir -p $(PKG_BUILD_DIR) $(CP) ./src/* $(PKG_BUILD_DIR)/ endef define Build/Configure endef define Build/Compile $(MAKE) -C $(PKG_BUILD_DIR) \ CC="$(TARGET_CC)" \ CFLAGS="$(TARGET_CFLAGS) -Wall" \ LDFLAGS="$(TARGET_LDFLAGS)" endef define Package/tcpclient/install $(INSTALL_DIR) $(1)/usr/sbin $(INSTALL_BIN) $(PKG_BUILD_DIR)/tcpclient $(1)/usr/sbin/ endef $(eval $(call BuildPackage,tcpclient))