├── src ├── Makefile ├── openDev.h ├── main.c └── openDev.c ├── README.md └── Makefile /src/Makefile: -------------------------------------------------------------------------------- 1 | all:sendat 2 | 3 | OBJS = main.o openDev.o 4 | 5 | sendat:$(OBJS) 6 | $(CC) $(CFLAGS) -o $@ $(OBJS) -lpthread 7 | 8 | clean: 9 | rm -f sendat *.o 10 | -------------------------------------------------------------------------------- /src/openDev.h: -------------------------------------------------------------------------------- 1 | #ifndef OPENDEV_H 2 | #define OPENDEV_H 3 | 4 | #include 5 | 6 | #define TRUE 1 7 | #define FALSE 0 8 | #define MAX_BUFF_SIZE (256) 9 | 10 | typedef struct _serial_parse 11 | { 12 | char buff[MAX_BUFF_SIZE]; 13 | int rxbuffsize; 14 | }serial_parse; 15 | 16 | int OpenDev(char *Dev); 17 | void set_speed(int fd, int speed); 18 | int set_Parity(int fd,int databits,int stopbits,int parity); 19 | 20 | #endif 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sendat 2 | AT 命令工具 3 | 1简化使用AT命令请求方式 4 | 使用命令说明 5 | sendat 2 'ATI' 2表示 /dev/ttyUSB2 'ATI' 为at命令 6 | 7 | 8 |

ATTool_APIServer 新版支持 http api 请求类库 返回 json 数据

9 | (项目地址:(https://github.com/ouyangzq/ATTool_APIServer)) 10 | 11 | 12 |

ATTool_WebSocketServer 新版支持 WebSocket AT 命令工具 主动推送数据 项目优化中 等待开源...

13 | (项目地址:https://github.com/ouyangzq/ATTool_WebSocketServer) 14 | 15 | 16 |

luci-app-cpe_new 新版使用ATTool_WebSocketServer 处理打包整体lui—app-cpe工具包全依赖 支持相同拨号 信号查看 短信读取 等, 项目等待开源...

17 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | include $(TOPDIR)/rules.mk#将openwrt顶层目录下的rules.mk文件中的内容导入进来 2 | 3 | PKG_NAME:=sendat#软件包名 4 | PKG_VERSION:=6.8.2#软件包版本 5 | PKG_BUILD_DIR:= $(BUILD_DIR)/$(PKG_NAME)#真正编译当前软件包的目录 6 | 7 | 8 | include $(INCLUDE_DIR)/package.mk#将$(TOPDIR)/include目录下的package.mk文件中的内容导入进来 9 | 10 | define Package/sendat 11 | SECTION:=wrtnode#软件包类型 12 | CATEGORY:=Daocaoren#menuconfig中软件包所属的一级目录 13 | SUBMENU :=CPE#menuconfig中软件包所属的二级目录 14 | TITLE:=Sendat AT#软件包标题 15 | DEPENDS:=+libpthread#运行本软件依赖的其他包 16 | endef 17 | 18 | define Package/sendat/description #软件包描述 19 | A sample for sendat test 20 | endef 21 | 22 | define Build/Prepare #编译之前的准备动作 23 | mkdir -p $(PKG_BUILD_DIR) 24 | $(CP) ./src/* $(PKG_BUILD_DIR)/ 25 | endef 26 | 27 | 28 | define Package/sendat/install #软件包的安装方法,主要就是将一系列编译好的文件、启动脚本、UCI配置文件等拷贝到指定位置 29 | $(INSTALL_DIR) $(1)/bin 30 | $(INSTALL_BIN) $(PKG_BUILD_DIR)/sendat $(1)/bin/ 31 | endef 32 | 33 | $(eval $(call BuildPackage,sendat)) -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include "openDev.h" 22 | #define MAX_PORTS 4 23 | /*this array hold information about each port we have opened */ 24 | struct PortInfo { 25 | char name[16]; 26 | int port_fd; 27 | } 28 | ; 29 | /* 30 | ** File: uart.c 31 | ** 32 | ** Description: 33 | ** Provides an RS-232 interface that is very similar to the CVI provided 34 | ** interface library 35 | */ 36 | /*this array hold information about each port we have opened */ 37 | struct PortInfo ports[13] = { { 38 | "/dev/ttyUSB0", 0 39 | } 40 | , { 41 | "/dev/ttyUSB1", 0 42 | } 43 | , { 44 | "/dev/ttyUSB2", 0 45 | } 46 | , { 47 | "/dev/ttyUSB3", 0 48 | } 49 | , { 50 | "/dev/ttyUSB4", 0 51 | } 52 | , { 53 | "/dev/ttyUSB5", 0 54 | } 55 | , { 56 | "/dev/ttyUSB6", 0 57 | } 58 | , { 59 | "/dev/ttyUSB7", 0 60 | } 61 | , { 62 | "/dev/ttyUSB8", 0 63 | } 64 | , { 65 | "/dev/ttyUSB9", 0 66 | } 67 | , { 68 | "/dev/ttyUSB10", 0 69 | } 70 | , { 71 | "/dev/ttyUSB11", 0 72 | } 73 | , { 74 | "/dev/ttyUSB12", 0 75 | } 76 | , 77 | }; 78 | /*判断是否存在*/ 79 | int FileExist(const char* filename) { 80 | if (filename && access(filename, F_OK) == 0) { 81 | return 1; 82 | } 83 | return 0; 84 | } 85 | int main(int argc, char **argv) { 86 | if(argc<3) { 87 | printf("ERROR demo: sendat 2 'ATI'\n"); 88 | exit(1); 89 | return 0; 90 | } 91 | int debug = 1; 92 | int port= 0; 93 | sscanf(argv[1], "%d", &port); 94 | if(FileExist(ports[port].name)==0) { 95 | printf("AT ERROR absent.\n"); 96 | return 0; 97 | } 98 | char *message= argv[2]; 99 | char *nty= "\r\n"; 100 | int fd = OpenDev(ports[port].name); 101 | //打开串口 102 | if(fd>0) { 103 | set_speed(fd,19200); 104 | //设置波特率 105 | } else { 106 | printf("Can't Open Serial Port!\n"); 107 | exit(1); 108 | return 0; 109 | } 110 | //设置校验位 111 | if(set_Parity(fd,8,1,'N')==FALSE) { 112 | printf("Set Parity Error\n"); 113 | exit(1); 114 | } 115 | int nread; 116 | char buffer[MAX_BUFF_SIZE]; 117 | serial_parse phandle; 118 | phandle.rxbuffsize = 0; 119 | char *ATcStr = strcat(message, nty); 120 | int ATlen = strlen(ATcStr); 121 | char sendAT[ATlen + 1]; 122 | strcpy(sendAT, ATcStr); 123 | write(fd, sendAT, strlen(sendAT)); 124 | usleep(10000); 125 | nread = read(fd , phandle.buff + phandle.rxbuffsize, MAX_BUFF_SIZE - phandle.rxbuffsize); 126 | phandle.rxbuffsize += nread; 127 | phandle.buff[phandle.rxbuffsize] = '\0'; 128 | printf("%s",phandle.buff); 129 | exit(1); 130 | return 0; 131 | } 132 | -------------------------------------------------------------------------------- /src/openDev.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include "openDev.h" 12 | 13 | #define TRUE 1 14 | #define FALSE 0 15 | 16 | int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, }; 17 | int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, }; 18 | 19 | int OpenDev(char *Dev) 20 | { 21 | // , O_RDWR|O_NOCTTY 22 | int fd = open(Dev,O_RDWR | O_NOCTTY | O_NONBLOCK); 23 | if(-1 == fd) 24 | { 25 | perror("Can't Open Serial PPPPort"); 26 | return -1; 27 | } 28 | else 29 | { 30 | return fd; 31 | } 32 | } 33 | 34 | void set_speed(int fd, int speed) 35 | { 36 | int i; 37 | int status; 38 | struct termios Opt; 39 | tcgetattr(fd, &Opt); 40 | for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) 41 | { 42 | if (speed == name_arr[i]) 43 | { 44 | tcflush(fd, TCIOFLUSH); 45 | cfsetispeed(&Opt, speed_arr[i]); 46 | cfsetospeed(&Opt, speed_arr[i]); 47 | status = tcsetattr(fd, TCSANOW, &Opt); 48 | if (status != 0) perror("tcsetattr fd1"); 49 | return; 50 | } 51 | tcflush(fd,TCIOFLUSH); 52 | } 53 | } 54 | 55 | int set_Parity(int fd,int databits,int stopbits,int parity) 56 | { 57 | struct termios options; 58 | if ( tcgetattr( fd,&options) != 0) 59 | { 60 | perror("SetupSerial 1"); 61 | return(FALSE); 62 | } 63 | bzero(&options,sizeof(options)); 64 | options.c_cflag |= CLOCAL | CREAD; 65 | options.c_cflag &= ~CSIZE; 66 | switch (databits) 67 | { 68 | case 7: 69 | options.c_cflag |= CS7; 70 | break; 71 | case 8: 72 | options.c_cflag |= CS8; 73 | break; 74 | default: fprintf(stderr,"Unsupported data size\n"); 75 | return (FALSE); 76 | } 77 | switch (parity) 78 | { 79 | case 'n': 80 | case 'N': 81 | options.c_cflag &= ~PARENB; 82 | options.c_iflag &= ~INPCK; 83 | break; 84 | case 'o': 85 | case 'O': 86 | options.c_cflag |= (PARODD | PARENB); 87 | options.c_iflag |= (INPCK | ISTRIP); 88 | break; 89 | case 'e': 90 | case 'E': 91 | options.c_cflag |= PARENB; 92 | options.c_cflag &= ~PARODD; 93 | options.c_iflag |= (INPCK | ISTRIP); 94 | break; 95 | case 'S': 96 | case 's': 97 | options.c_cflag &= ~PARENB; 98 | options.c_cflag &= ~CSTOPB; 99 | break; 100 | default: fprintf(stderr,"Unsupported parity\n"); 101 | return (FALSE); 102 | } 103 | switch (stopbits) 104 | { 105 | case 1: 106 | options.c_cflag &= ~CSTOPB; 107 | break; 108 | case 2: 109 | options.c_cflag |= CSTOPB; 110 | break; 111 | default: fprintf(stderr,"Unsupported stop bits\n"); 112 | return (FALSE); 113 | } 114 | if (parity != 'n') 115 | options.c_iflag |= INPCK; 116 | options.c_cc[VTIME] = 0; 117 | options.c_cc[VMIN] = 0; 118 | tcflush(fd,TCIFLUSH); 119 | if (tcsetattr(fd,TCSANOW,&options) != 0) 120 | { 121 | perror("SetupSerial 3"); 122 | return (FALSE); 123 | } 124 | return (TRUE); 125 | } 126 | --------------------------------------------------------------------------------