对于/dev/ttyUSB*设备节点不存在修改下内核menuconfig
AT+CSQ : 获取信号强度
测试结果:AT+CSQ<CR>
+CSQ: 31, 99
命令解释:获取4G网络信号强度
命令格式:AT+CSQ<CR>
命令返回:+CSQ: **, ##
- **应在 0 到 31 之间(99表示无信号),数值越大表明信号质量越好;
- ##为误码率,值在 0 到 99 之间。否则应检查天线或 SIM 卡是否正确安装
- 串口下命令行获取方式
c代码实现:
通过串口获取到数据字符串后转换后再计算信号强度计算公式:信号强度 = -113dBm + (rssi * 2)
int getcsq(void *) {
int ret = 0;
unsigned char csq[32] = {0};
int str_fd_ttyUSB2 = openport("/dev/ttyUSB2"); //打开串口
if(str_fd_ttyUSB2>0)
{
ret=setport(str_fd_ttyUSB2); //设置串口,波特率,数据位,停止位,校验
}
write(str_fd_ttyUSB2,"AT+CSQ\r\n",12);//开启定位
sleep(2);
unsigned char rbuf[64];
int len = read(str_fd_ttyUSB2, rbuf, sizeof(rbuf)); // 在串口读入字符串
sleep(1);
if(len < 0)
{
printf("Can't get /dev/ttyUSB2 Serial Port data!\n");
}
const char *str2 = "+CSQ: ";
char *pdata = strstr(rbuf, str2);
if(pdata)
strncpy(csq, pdata+6, 6);
else {
return -1;
}
printf("SIM-CSQ: %s\n", csq);
int level;
char *str = NULL;
if (strstr(csq, "99,") || strstr(csq, "199,") ){
printf("No signal ..\n");
level = 0;
}
else {
char rssi_str[4];
int rssi;
memset(rssi_str, 0, sizeof(rssi_str));
if (*(csq+2) == ',') {
rssi_str[0] = *(csq+6+0);
rssi_str[1] = *(csq+6+1);
}
else if (isdigit(*(csq+2)) && isdigit(*(csq+3) == ',')) {
rssi_str[0] = *(csq+6+0);
rssi_str[1] = *(csq+6+1);
rssi_str[2] = *(csq+6+2);
}
rssi = atoi(rssi_str);
int signal = -113 + (2 * rssi);
if(signal > -91)
level = 5;
else if (signal <= -91 && signal > -101)
level = 4;
else if(signal <= -101 && signal > -103)
level = 3;
else if(signal <= -103 && signal > -107)
level = 2;
else if(signal <= -107 && signal > -113)
level = 1;
else
level = 0;
printf("level = %d, signal: %d\n", rssi, signal);
}
return ;
}
int main(int argc, char** argv) {
getcsq();
return 0;
}
AT+QCCID: 获取SIM卡CID
测试结果:AT+QCCID<CR>
+QCCID: 898604A0235431253112
命令解释:获取SIM序列号
命令格式:AT+QCCID<CR>
命令返回:+QCCID: *********
- ** 20位;
c代码实现:
unsigned char* get_Qccid( ) {
int ret = 0;
unsigned char *ccid = NULL;
int str_fd_ttyUSB2 = openport("/dev/ttyUSB2"); //打开串口
if(str_fd_ttyUSB2>0)
{
ret=setport(str_fd_ttyUSB2); //设置串口,波特率,数据位,停止位,校验
if(ret<0)
{
printf("Can't Set /dev/ttyUSB2!/n");
}
}
else
{
printf("Can't Open /dev/ttyUSB2!\n");
}
write(str_fd_ttyUSB2,"AT+QCCID\r\n",12);
usleep(200*1000);
unsigned char rbuf[128];
int len = read(str_fd_ttyUSB2, rbuf, sizeof(rbuf)); // 在串口读入字符串
close(str_fd_ttyUSB2);
if(len < 0)
{
printf("Can't get /dev/ttyUSB2 Serial Port data!\n");
}
printf("buf = %s \n", rbuf);
printf("bufff: %d\n", strlen(rbuf));
const char *str2 = "+QCCID: ";
char *pdata = strstr(rbuf, str2);
if(pdata)
strncpy(ccid, pdata+8, 20);
else {
return -1;
}
printf("SIM-CID: %s\n", ccid);
if (strlen(data) == 20)
{
int fd;
if ((fd= open("/tmp/cid.txt", O_RDWR | O_CREAT)) == -1) {
return -1;
}
if (write(fd, data, 20) == -1) {
return -1;
}
close(fd);
}
return 0;
}
int main(int argc, char** argv) {
get_qccid();
return 0;
}
设置串口属性代码:
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <time.h>
#include <termios.h>
static int openport(char *Dev)
{
int fd = open( Dev, O_RDWR|O_NOCTTY|O_NDELAY );
if (-1 == fd)
{
perror("Can''t Open Serial Port");
return -1;
}
else
return fd;
}
static int setport(int fd)
{
int baudrate;
struct termios newtio;
baudrate=B115200;//波特率
tcgetattr(fd,&newtio);
bzero(&newtio,sizeof(newtio));
//setting c_cflag
newtio.c_cflag &=~CSIZE;
newtio.c_cflag |= CS8; //8位数据位
//校验
newtio.c_cflag &= ~PARENB; // Clear parity enable
newtio.c_iflag &= ~INPCK; // Enable parity checking
//设置停止位
newtio.c_cflag &= ~CSTOPB; //1
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
newtio.c_cflag |= (CLOCAL|CREAD);
newtio.c_oflag|=OPOST;
newtio.c_iflag &=~(IXON|IXOFF|IXANY);
cfsetispeed(&newtio,baudrate);
cfsetospeed(&newtio,baudrate);
tcflush(fd, TCIFLUSH);
if (tcsetattr(fd,TCSANOW,&newtio) != 0)
{
perror("SetupSerial 3");
return -1;
}
return 0;
}