我在 Ubuntu Gnome 14.04 LTS 上使用 Qt 5.4 从串行端口读取字符串行。一切正常,但当我重新安装 Ubuntu Gnome 14.04 和 Qt 5.4 时,我的串口代码无法正常工作。当 Arduino 发送“0”时,Qt 的代码将其读取为“�”,而通过串行发送的其他数字则 Qt 将其读取为字母和符号。我认为我的 Qt 的 unicode 有问题。我的 ubuntu 的 unicode 是 en.US.UTF-8,QT unicode 设置为“系统”。请帮我 :( 这是我从串口读取数据的代码:
QByteArray input;
if (serial->canReadLine()) //chect if data line is ready
input.clear();
input = serial->readLine(); //read data line from sreial port
ui->label->setText(input);
qDebug ()<<input<<endl;
Arduino 的这段代码在 CuteCom 和 Arduino 串口监视器上运行良好
const int analogInPin = A0;
unsigned int sensorValue = 0; // value read from the pot
void setup() {
Serial.begin(19200);
}
void loop() {
Serial.print("\n");
Serial.print("#");
for (int i=0; i < 5; i++) {
// read the analog in value:
sensorValue = analogRead(analogInPin);
Serial.print(sensorValue);
Serial.print("#");
};
sensorValue = analogRead(analogInPin);
Serial.print(sensorValue);
Serial.print("# \n");
}
对不起我的英语
最佳答案
如果奇偶校验或数据/停止位参数不同,您仍然可以写入和读取,但您会得到类似于上面显示的“有趣”输出,这不是 unicode 设置的问题(尤其不是带有 '0',这是 ASCII 集的一个字符)。
尝试在开始通信之前在两端显式设置相同的端口参数。
关于c++ - Qt串口读取字符串不好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35315841/