450 lines
9.5 KiB
C++
450 lines
9.5 KiB
C++
#include "DlinkMonitor.h"
|
|
#include "ui_DlinkMonitor.h"
|
|
|
|
int distance_zero=0;
|
|
int distance_t[10];
|
|
int bandrate=115200;
|
|
int input_select=0;
|
|
int data_fifo[128];
|
|
int fifo_num=0;
|
|
float distance_xishu=2.409;
|
|
int en_ch=0;
|
|
int led_count=0;
|
|
int led_state=0;
|
|
int air_soft_ver=13;
|
|
int ground_soft_ver=13;
|
|
int have_data=1;
|
|
|
|
|
|
DlinkMonitor::DlinkMonitor(QWidget *parent) :
|
|
ToolsWidget(parent),
|
|
ui(new Ui::DlinkMonitor)
|
|
{
|
|
ui->setupUi(this);
|
|
}
|
|
|
|
DlinkMonitor::~DlinkMonitor()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
|
|
int DlinkMonitor::SendMessageTo(quint8 ch, quint8 *msg, quint16 len)
|
|
{
|
|
foreach(Node node,clientSockets)
|
|
{
|
|
qint64 flag = node.sock->writeDatagram((const char *)msg,len,node.addr, node.port);
|
|
if(flag != -1)
|
|
{
|
|
//qDebug() << "Client Send Msg";
|
|
}
|
|
}
|
|
|
|
|
|
if (serialPort)
|
|
{
|
|
qint64 flag = serialPort->write((const char *)msg,len);
|
|
if(flag != -1)
|
|
{
|
|
//qDebug() << "serialPort Send Msg";
|
|
}
|
|
}
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
//这个函数就在本线程内运行
|
|
bool DlinkMonitor::setupPort(const QString port, qint32 baudrate, QSerialPort::Parity parity)
|
|
{
|
|
bool isSuccess = false;
|
|
|
|
if (serialPort)//多串口接入
|
|
{
|
|
serialPort->close();
|
|
delete serialPort;
|
|
}
|
|
serialPort = new QSerialPort(this);
|
|
|
|
serialPort->setPortName(port);
|
|
|
|
if (serialPort->open(QIODevice::ReadWrite))
|
|
{
|
|
serialPort->setBaudRate(baudrate);
|
|
serialPort->setParity(parity);
|
|
serialPort->setDataBits(QSerialPort::Data8);
|
|
serialPort->setStopBits(QSerialPort::OneStop);
|
|
|
|
connect(serialPort, SIGNAL(readyRead()), this, SLOT(readPendingDatagramsSerialPort()));//本线程内调用
|
|
isSuccess = true;
|
|
}
|
|
else
|
|
{
|
|
isSuccess = false;
|
|
delete serialPort;
|
|
serialPort = nullptr;
|
|
}
|
|
|
|
return isSuccess;
|
|
}
|
|
|
|
bool DlinkMonitor::statesPort()
|
|
{
|
|
if(serialPort)
|
|
{
|
|
return (serialPort != nullptr)?(true):(false);
|
|
}
|
|
}
|
|
|
|
void DlinkMonitor::stopPort()
|
|
{
|
|
if(serialPort)
|
|
{
|
|
if(serialPort->isOpen())
|
|
{
|
|
serialPort->close();
|
|
}
|
|
delete serialPort;
|
|
serialPort = nullptr;
|
|
}
|
|
}
|
|
|
|
void DlinkMonitor::readPendingDatagramsSerialPort(void)
|
|
{
|
|
|
|
|
|
QObject *obj = sender();
|
|
|
|
QSerialPort *serialport = qobject_cast<QSerialPort *>(obj);
|
|
if(serialport)
|
|
{
|
|
QByteArray datagram = serialport->readAll();
|
|
|
|
for (QByteArray::const_iterator i = datagram.cbegin(); i != datagram.cend(); ++i)
|
|
{
|
|
parse(*i);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
int DlinkMonitor::parse(uint8_t c)
|
|
{
|
|
|
|
static uint8_t stage = 0;
|
|
static uint8_t RawData[280];
|
|
static uint16_t idx = 0;
|
|
static uint8_t LEN = 0;
|
|
static uint8_t CID = 0;
|
|
static uint8_t SYN = 0;
|
|
static uint8_t SUM = 0;
|
|
|
|
switch (stage)
|
|
{
|
|
case 0:
|
|
if (c == 0xEB)
|
|
{
|
|
RawData[0] = c;
|
|
SUM = 0;
|
|
++stage;
|
|
}
|
|
break;
|
|
case 1:
|
|
if (c == 0x90)
|
|
{
|
|
RawData[1] = c;
|
|
++stage;
|
|
}
|
|
else
|
|
{
|
|
stage = 0;
|
|
}
|
|
break;
|
|
case 2:
|
|
CID = c;
|
|
RawData[2] = c;
|
|
++stage;
|
|
break;
|
|
case 3:
|
|
LEN = c;
|
|
RawData[3] = c;
|
|
idx = 0;
|
|
++stage;
|
|
break;
|
|
case 4:
|
|
if (idx < (LEN - 5))
|
|
{
|
|
RawData[4+idx] = c;
|
|
++idx;
|
|
}
|
|
else
|
|
{
|
|
RawData[4+idx] = c;
|
|
|
|
QByteArray raw((char *)RawData);
|
|
|
|
showinfo(raw);
|
|
|
|
stage = 0;
|
|
|
|
return 1;
|
|
}
|
|
break;
|
|
default:
|
|
stage = 0;
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
void DlinkMonitor::showinfo(QByteArray data)
|
|
{
|
|
int up_agc=0;
|
|
int up_agc_spread=0;
|
|
int down_agc=0;
|
|
int down_agc_spread=0;
|
|
int dimian_channel=0;
|
|
int jizai_channel=0;
|
|
int power=0;
|
|
int datalink_lock=0;
|
|
int up_vb=0;
|
|
int down_vb=0;
|
|
int tianxian=0;
|
|
int up_work=16;
|
|
int down_work=16;
|
|
QString power_char=0;
|
|
int distance=0;
|
|
|
|
if(data.at(0)==0xeb && data.at(1)== 0x90 && data.size()>31)
|
|
{
|
|
|
|
air_soft_ver=data[15];
|
|
ground_soft_ver=data[30];
|
|
|
|
up_agc=data[10];
|
|
ui->Label_up_agc->setText(QString::number(up_agc, 10));
|
|
|
|
up_agc_spread=data[7];
|
|
ui->Label_up_agc_spread->setText(QString::number(up_agc_spread, 10));
|
|
|
|
down_agc=data[20];
|
|
ui->Label_down_agc->setText(QString::number(down_agc,10));
|
|
|
|
down_agc_spread=data[17];
|
|
ui->Label_down_agc_spread->setText(QString::number(down_agc_spread,10));
|
|
|
|
|
|
|
|
dimian_channel=data[18];
|
|
ui->Label_dimian_channel->setText(QString::number(dimian_channel,10));
|
|
|
|
jizai_channel=data[8];
|
|
ui->Label_jizai_channel->setText(QString::number(jizai_channel,10));
|
|
|
|
power=data[16]&12&0xff;
|
|
if(power==4)
|
|
{ if(en_ch==0)
|
|
power_char="小功率";
|
|
else
|
|
power_char="Low";
|
|
}
|
|
if(power==8)
|
|
{
|
|
if(en_ch==0)
|
|
power_char="中功率";
|
|
else
|
|
power_char="Middle";
|
|
}
|
|
if(power==12)
|
|
{
|
|
if(en_ch==0)
|
|
power_char="大功率";
|
|
else
|
|
power_char="High";
|
|
}
|
|
ui->Label_up_power->setText(power_char);
|
|
|
|
|
|
|
|
tianxian=data[16]&128&0xff;
|
|
|
|
if(tianxian==0)
|
|
{
|
|
if(en_ch==0)
|
|
ui->Label_tianxian->setText("全向");
|
|
else
|
|
ui->Label_tianxian->setText("Omni");
|
|
}
|
|
if(tianxian==128)
|
|
{
|
|
if(en_ch==0)
|
|
ui->Label_tianxian->setText("定向");
|
|
else
|
|
ui->Label_tianxian->setText("Directional");
|
|
}
|
|
|
|
up_work=data[16]&16&0xff;
|
|
|
|
if(up_work==0)
|
|
{
|
|
if(en_ch==0)
|
|
ui->Label_up_work->setText("静默");
|
|
else
|
|
ui->Label_up_work->setText("close");
|
|
}
|
|
if(up_work==16)
|
|
{
|
|
if(en_ch==0)
|
|
ui->Label_up_work->setText("工作");
|
|
else
|
|
ui->Label_up_work->setText("open");
|
|
}
|
|
|
|
|
|
down_work=data[6]&16&0xff;
|
|
|
|
if(down_work==0)
|
|
{
|
|
if(en_ch==0)
|
|
ui->Label_down_work->setText("静默");
|
|
else
|
|
ui->Label_down_work->setText("close");
|
|
}
|
|
if(down_work==16)
|
|
{
|
|
if(en_ch==0)
|
|
ui->Label_down_work->setText("工作");
|
|
else
|
|
ui->Label_down_work->setText("open");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
datalink_lock=data[16]&1&0xff;
|
|
if(datalink_lock==0)
|
|
{
|
|
if(en_ch==0)
|
|
ui->Label_datalink_lock->setText("失锁");
|
|
else
|
|
ui->Label_datalink_lock->setText("Unlock");
|
|
QPalette pa;
|
|
pa.setColor(QPalette::WindowText,Qt::red);
|
|
ui->Label_datalink_lock->setPalette(pa);
|
|
|
|
}
|
|
if(datalink_lock==1)
|
|
{
|
|
if(en_ch==0)
|
|
ui->Label_datalink_lock->setText("锁定");
|
|
else
|
|
ui->Label_datalink_lock->setText("lock");
|
|
QPalette pa;
|
|
pa.setColor(QPalette::WindowText,Qt::green);
|
|
ui->Label_datalink_lock->setPalette(pa);
|
|
}
|
|
|
|
|
|
power=data[6]&12&0xff;
|
|
if(power==4)
|
|
{ if(en_ch==0)
|
|
power_char="小功率";
|
|
else
|
|
power_char="Low";
|
|
}
|
|
if(power==8)
|
|
{
|
|
if(en_ch==0)
|
|
power_char="中功率";
|
|
else
|
|
power_char="Middle";
|
|
}
|
|
if(power==12)
|
|
{
|
|
if(en_ch==0)
|
|
power_char="大功率";
|
|
else
|
|
power_char="High";
|
|
}
|
|
ui->Label_down_power->setText(power_char);
|
|
|
|
|
|
up_vb=(data[12])+(data[13])*256;
|
|
ui->Label_up_vb->setText(QString::number(up_vb, 10));
|
|
|
|
down_vb=(data[25])+((data[26])*256)+((data[27])*65536);
|
|
ui->Label_down_vb->setText(QString::number(down_vb, 10));
|
|
|
|
distance=((data[22]&0xff)+(data[23]&0xff)*256+(data[24]&0xff)*65536)*distance_xishu-distance_zero;
|
|
|
|
for(int i=0;i<9;i++)
|
|
distance_t[i]=distance_t[i+1];
|
|
distance_t[9]=distance;
|
|
|
|
distance=(distance_t[0]+distance_t[1]+distance_t[2]+distance_t[3]+distance_t[4]+distance_t[5]+distance_t[6]+distance_t[7]+distance_t[8]+distance_t[9])/10;
|
|
ui->Label_juli->setText(QString::number(distance, 10));
|
|
|
|
if(fifo_num>32 ){
|
|
for(int i=0;i<96;i++)
|
|
{
|
|
data[i]=data[i+32];
|
|
|
|
}
|
|
|
|
for(int i=96;i<128;i++)
|
|
{
|
|
data[i]=0;
|
|
|
|
}
|
|
|
|
fifo_num=fifo_num-32;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
void DlinkMonitor::on_pushButton_clicked()
|
|
{
|
|
if(statesPort())
|
|
{
|
|
disconnectdialog dlg(this);
|
|
|
|
dlg.setWindowTitle(tr("SerialPort"));
|
|
dlg.setWindowIcon(QIcon("qrc:/img/icons/Fet.ico"));
|
|
|
|
int ret = dlg.exec();
|
|
if (QDialog::Accepted == ret)
|
|
{
|
|
stopPort();
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
ConnectDialog dlg(this);
|
|
|
|
dlg.setWindowTitle(tr("SerialPort"));
|
|
dlg.setWindowIcon(QIcon("qrc:/img/icons/Fet.ico"));
|
|
|
|
dlg.baudrate = 115200;
|
|
dlg.parity = QSerialPort::NoParity;
|
|
int ret = dlg.exec();
|
|
if (QDialog::Accepted == ret)
|
|
{
|
|
if(dlg.port.size())//如果没选择串口,那么将不打开
|
|
{
|
|
setupPort(dlg.port, dlg.baudrate,dlg.parity);
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|