6、读写串口
- //读取传感器中的数据
- void MainWindow::process_read_weight_fun()
- {
- bool ok;
- const unsigned char s[8]={0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B};
- serial.write ((char*)s,8);//以ASCII码的形式通过串口发送出去。
- serial.waitForBytesWritten (300);
- QByteArray temp=serial.read(10);//在缓冲区中读一个byte
- serial.waitForReadyRead (100);
- float weight=temp.toHex ().left (14).right (8).toInt (&ok,16)/1000.;
- para_weight_show->setText (QString::number(weight, 'f',2)+" Kg");
- }
|