0

我正在尝试将数据从 PC 发送到 crossbow motes。PC 从串口接收的数据格式如下:

7E 45 0 FF FF 0 0 12 0 89 0 5 0 0 27 B 0 0 27 AF 0 0 26 E7 0 0 27 BF 37 D4 7E

发送按钮的Qt fun实现如下:

void MainWindow::on_sendData_clicked()
{
    QVector<quint8> senddata;
    quint8 *alldata=new quint8[8];
    int ij;
    alldata[0]=0x7e;
    alldata[1]=0x44;
    alldata[2]=0x00;
    alldata[3]=0x00;
    alldata[4]=0xff;
    alldata[5]=0xff;
    alldata[6]=0x00;
    alldata[7]=0x00;
    for (ij=0;ij<8;ij++)
        senddata.append(alldata[ij]);
     //senddata.append("7E 44 0F 00 FF FF 00 00");
    //QSerialPort *serial=new QSerialPort(ui->portNameComboBox->currentText());
    QString string=ui->SendMseLineEdit->text();
    string=string.simplified();
    quint8 sendlen=(quint8)(string.count(QRegExp(" "))+1);

//    if (sendlen==10)
//        sendlen=0x0a;
    senddata.append(sendlen);//payload
    senddata.append(0x00);
    senddata.append(0x89);
    QString tmp;
    quint8 *payload=new quint8[sendlen];
    for (ij=0;ij<sendlen;ij++)
    {
        tmp.append(string[2*ij]);
        tmp.append(string[2*ij+1]);
        payload[ij]=(quint8)tmp.toInt(0,16);//QString-int
        senddata.append(payload[ij]);
    }
    quint8 *dest=senddata.data();
    //qDebug()<<dest[0];
    quint16 sendcrc=calc(dest,1,sendlen+10);
    quint8  sendhigh=(quint8)(sendcrc>>8);
    quint8  sendlow =(quint8)(sendcrc&0xFF);
    senddata.append(sendlow);
    senddata.append(sendhigh);// crc
    senddata.append(0x7e);
    qDebug()<<senddata;
    int totalsize=senddata.size();
   QString sendstring;
//   // sendarray.resize(totalsize);
    for (ij=0;ij<totalsize;ij++)
    {
        if (senddata.at(ij)<=0x0f)
            sendstring.append(tr("0"));//
        sendstring.append(QString::number(senddata[ij],16));//QVcetor-QString
//        sendstring.append(tr(" "));

   }
   // qDebug()<<sendstring;

    qint64 testlen=0;
    QByteArray sendarray=sendstring.toLocal8Bit();//QString-QByteArray
    qDebug()<<sendarray;

    char *chardata=new char[sendarray.size()];//QByteArray-char
    strcpy(chardata,sendarray.data());
    qDebug()<<sendarray.size();
    for (ij=0;ij<sendarray.size();ij++)
    {
        if (ij%2==0)
    testlen=testlen+serial->write(chardata+ij,1);

    }
     qDebug()<<testlen;

}
/****************CRC************************/
 quint16 MainWindow::calcByte(int crc,int b)
{
    crc=crc^(int) b<<8;
    for (int i=0;i<8;i++){
        if ((crc &0x8000)==0x8000)
            crc=crc<<1^0x1021;
        else
            crc=crc<<1;
    }
    return crc & 0xffff;
 }

 quint16 MainWindow::calc(quint8 *packet, int index, int count)
{
    quint16 crc=0;
    //int i;
    while(count>0){
        crc =calcByte(crc,packet[index++]);
        count--;
    }
    return crc;
}


void MainWindow::on_openserial_clicked()
{
    QPainterPath linepath;
    paintwiget=new paint(linepath,ui->scrollArea);
    ui->scrollArea->setWidget(paintwiget);
    paintwiget->show();
    if (serial->isOpen())
    {
        QMessageBox::about(this,tr("info"),tr("Serial port is already open"));
    return;
    }

问题是当按下发送按钮时,发送的内容是:

7e440000ffff000002008901009f1a7e

这是我想要的确切传输格式(如果我没有在正确的串行数据格式中出错)。但是mote没有收到数据(当数据到达时,led可能会切换)。我不知道在这里问与串口相关的问题是否可以。但如果问题得到回答,我真的很感激。提前感谢响应者。

4

0 回答 0