QextSerialPort 的字符编码问题 (Qt/C++)

发布于 2024-10-28 04:42:12 字数 1237 浏览 1 评论 0原文

我正在 QtCreator 中开发一个 Qt/C++ 程序,该程序使用 QextSerialPort 从串行端口读取和写入。我的程序向 Rhino Mark IV 控制器发送命令,并且必须读取这些命令的响应(以防万一它们产生任何响应)。我的开发和部署平台是Windows XP Professional。

当 Mark IV 发送命令响应并且我的程序从串行端口缓冲区读取该响应时,数据未正确编码;我的程序似乎没有获得纯 ASCII 数据。例如,当 Mark IV 发送 ASCII“0”(十进制 48)后跟回车符(十进制 13)时,我的缓冲区 (char *) 得到 -80 和 13。未正确编码,但回车确实如此。我尝试过使用read (char *data, qint64 maxSize)readAll()

我一直在使用两个监视器来监视串行端口流量,这些监视器解释 ASCII 数据并显示相应的字符,并且以两种方式发送的数据似乎都已正确编码(它们是实际显示正确)。鉴于 QByteArray 不解释任何字符编码,并且我尝试使用 read (char *data, qint64 maxSize) 和 readAll () ,我已经放弃了这个问题可能是Qt引起的。但是,我不确定问题是否是由 QextSerialPort 引起的,因为我的程序正确发送(写入)数据,但没有读取正确的字节。

我还尝试使用超级终端与 Mark IV 控制器进行手动通信,并且通信也正确进行。我使用具有以下参数的超级终端设置连接:

  • 波特率:9600
  • 数据位:8
  • 奇偶校验位:0
  • 停止位:1
  • 流控制:硬件

我的程序使用相同的参数设置串行端口。超级终端可以工作,但我的程序却不能。

我开始使用 qextserialport.sourceforge.net 中的 QextSerialPort 1.1,然后尝试使用 Google 代码上的 QextSerialPort,问题仍然存在。

是什么导致了错误的字符编码?

我需要做什么来解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤还要旧人安 2024-11-04 04:42:12

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

48 vs. -80 smells like a signed char vs. unsigned char mismatch to me. Try with explicit unsigned char* instead of char*.

相对绾红妆 2024-11-04 04:42:12

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

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

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

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

Finally, I have realized that I was not configuring the serial port correctly, as suggested by Judge Maygarden. I did not find that information in the device's manual, but in the manual of a software product developed for that device.

The correct way to set up the serial port for connecting to the Mark IV controller is to set

  • Baud rate: 9600
  • Data bits: 7
  • Parity: even
  • Stop bits: 2 bits
  • Flow control: Hardware

However, I am still wondering why did HyperTerminal show the characters properly even with the wrong configuration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文