将string 转换为int再转换为角度
arcGraph[2]->setangleLength(ir.toUInt()*360/1023);
对传感器的数据处理:从一连串数据中取出最新的数据;
QString Ap3216c::readIrData()
{//省略对文件出错的处理;
char buf[10];
char const *filename = "/sys/class/misc/ap3216c/ir";
int fd = open(filename,O_RDONLY);
int err = read(fd,buf,sizeof (buf));
close(fd);
//将数据转化为字符串
QString irValue = buf;
//利用QStringList将字符串分开
QStringList list = irValue.split("\n");
//以string的格式返回最新的数据
return list[0];
}
QString::number,将数字转换为QString用于iu显示并只保留两位小数;
int fd;
char const *filename = "/dev/icm20608";
signed int databuf[7];
fd = open(filename,O_RDWR);
//使用int数组来装数据;
ret = read(fd,databuf,sizeof (databuf));
if(ret == 0)
{
//接收数据
signed int gyro_x_adc = databuf[0];
转换数据为float
float gyro_x_act = (float)(gyro_x_adc)/16.4;
将数字转为QString,只保留两位小数;
QString gxdata = QString::number(gyro_x_act,'f',2);
}
close(fd);
串口的数据转换
//设置串口;
serialPort->setPortName(ui->comboBox->currentText());
//设置波特率;将string转换为int
serialPort->setBaudRate(ui->comboBox_2->currentText().toUInt());
//设置停止位;先将string文本转换为数字,再将数字转换为StopBits下的枚举;
serialPort->setStopBits(QSerialPort::StopBits(ui->comboBox_3->currentText().toUInt()));
//设置数据位;先将string文本转换为数字,再将数字转换为DataBits下的枚举;
serialPort->setDataBits(QSerialPort::DataBits(ui->comboBox_4->currentText().toUInt()));
//设置校验位;通过匹配下标一一匹配;
switch(ui->comboBox_5->currentIndex())
{
case 0:
serialPort->setParity(QSerialPort::NoParity);
break;
case 1:
serialPort->setParity(QSerialPort::EvenParity);
break;
case 2:
serialPort->setParity(QSerialPort::OddParity);
break;
case 3:
serialPort->setParity(QSerialPort::SpaceParity);
break;
case 4:
serialPort->setParity(QSerialPort::MarkParity);
break;
default:
break;
}