1

I am developing a Qt/C++ programme in QtCreator that reads and writes from/to the serial port using QextSerialPort. My programme sends commands to a Rhino Mark IV controller and must read the response of those commands (just in case they produce any response). My development and deployment platform is Windows XP Professional.

When the Mark IV sends a response to a command and my programme reads that response from the serial port buffer, the data are not properly encoded; my programme does not seem to get plain ASCII data. For example, when the Mark IV sends an ASCII "0" (decimal 48) followed by a carriage return (decimal 13), my buffer (char *) gets -80 and 13. Characters are not properly encoded, but carriage returns are indeed. I have tried using both read (char *data, qint64 maxSize) and readAll ().

I have been monitoring the serial port traffic using two monitors that interpret ASCII data and display the corresponding characters, and the data sent in both ways seem to be correctly encoded (they are actually displayed correctly). Given that QByteArray does not interpret any character encoding and that I have tried using both read (char *data, qint64 maxSize) and readAll (), I have discarded that the problem may be caused by Qt. However, I am not sure if the problem is caused by QextSerialPort, because my programme send (writes) data properly, but does not read the correct bytes.

I have also tried talking to the Mark IV controller by hand using HyperTerminal, and the communication takes place correctly, too. I set up the connection using HyperTerminal with the following parammeters:

  • Baud rate: 9600
  • Data bits: 8
  • Parity bits: 0
  • Stop bits: 1
  • Flow control: Hardware

My programme sets up the serial port using the same parammeters. HyperTerminal works, my programme does not.

I started using QextSerialPort 1.1 from qextserialport.sourceforge.net and then tried with the latest source code from QextSerialPort on Google Code, and the problem remains.

What is causing the wrong character encoding?

What do I have to do to solve this issue?

4

2 回答 2

1

48 与 -80 对我来说闻起来像是有符号字符与无符号字符不匹配。尝试使用显式 unsigned char* 而不是 char*。

于 2011-03-30T11:39:53.583 回答
0

最后,我意识到我没有按照 Maygarden 法官的建议正确配置串行端口。我没有在设备手册中找到该信息,而是在为该设备开发的软件产品的手册中找到。

设置用于连接 Mark IV 控制器的串行端口的正确方法是设置

  • 波特率:9600
  • 数据位:7
  • 奇偶性:偶数
  • 停止位:2位
  • 流量控制:硬件

但是,我仍然想知道为什么即使配置错误,超级终端也能正确显示字符。

于 2011-04-13T12:18:56.727 回答