Linux 获取 4G 信号强度

找了好多资料还是没找到 获取4G信号强度的应用代码

最后没办法了, 只能自己写了

首先要知道AT指令是什么 这个可以参考下面的连接:

https://www.cnblogs.com/yuweifeng/p/5587473.html


如果4G设备正常运行 在终端输入下面命令会得到设备当前的信号

命令:cat /dev/ttyUSB2&

echo -e "AT+CSQ\r\n" > /dev/ttyUSB2


得到:

AT+CSQ
+CSQ: 28,99
OK
AAT+CSQ
+CSQ: 28,99

28 只是 rssi 转换成信号格 参考文章 http://velep.com/archives/1147.html

大概了解一下就行了 下面是代码 是在qt写的,共同探讨 ,欢迎留言


void MainWindow::GetCurSignalStrenght(void)
{
	printf("============Geting CurSignalStrenght============\n");
	int SentATcount = 100;
	
    if(ec20check==1)
    {
		system("cat /dev/ttyUSB2 | grep \'CSQ\' > signalstrength&");
		
		while(SentATcount--)
		{
			printf("============Launch AT command============\n");
			system("echo -e \"AT+CSQ\\r\\n\" > /dev/ttyUSB2");
			printf("echo -e \"AT+CSQ\\r\\n\" > /dev/ttyUSB2\n");
			usleep(1000);
			
			QFile tempFile("signalstrength");
			
			if (!tempFile.open(QIODevice::ReadOnly | QIODevice::Text))
			{
				qDebug()<<"open signalstrength fail";
			}
			else
			{
				QTextStream tempLineFile(&tempFile);
				QString templine ;
				templine = tempLineFile.readLine().simplified();
				tempFile.close();
				if(!templine.isEmpty())
				{
					printf("============Get data from temp file, say oh ho!!!!============\n");
					break;
				}
				else
				{
					printf("============Fuck , There no useful data yet============\n");
					//continue wile do nothine
				}
			}
			
		}
		
		system("grep \'+CSQ:\' signalstrength > tempfilesignal");

		QFile signalFile("tempfilesignal");

		if (!signalFile.open(QIODevice::ReadOnly | QIODevice::Text))
		{
			signalStrenght = 0;
 			qDebug()<<"open tempfilesignal fail";
		}
		else
		{
			QTextStream infile(&signalFile);
			QString line ;
			line = infile.readLine().simplified();
			signalFile.close();
	
			if(!line.isEmpty())
			{
				//--------------------------
				QString linedate ;
				QString rssi;
				bool ok;  
				rssi = line.mid(6,2);
				qDebug() << " signal number is -tmpchr>="<%d\n",rssi.toInt(&ok,10));
				int signalTrans =-113 + (2 * rssi.toInt(&ok,10));
				
				printf(" signalTrans is ->%d\n",signalTrans);

				if(signalTrans >-91)
				{
					signalStrenght = 5;
				}
				else if(signalTrans <= -91 && signalTrans>-101)
				{
					signalStrenght = 4;
				}
				else if(signalTrans <= -101 && signalTrans>-103)
				{
					signalStrenght = 3;
				}
				else if(signalTrans <= -103 && signalTrans>-107)
				{
					signalStrenght = 2;
				}
				else if(signalTrans <= -107 && signalTrans>-113)
				{
					signalStrenght = 1;
				}
				else
				{ 	
					//no signal
					signalStrenght = 0;
				}
				printf("=======Get signalStrenght success.= signalStrenght is %d=======\n",signalStrenght);
			}
			else
			{
				printf("=======Get signalStrenght failed...==set signalStrenght zero=======\n");
				signalStrenght = 0;
			}
		}
	}
	
	system("rm tempfilesignal -rf");
}






你可能感兴趣的:(4G网卡Linux相关)