在 virtualbox 中读取 rs232 时出现乱码文本

问题描述 投票:0回答:1

我正在尝试从 Ubuntu 来宾读取串行端口数据(主机是 Windows,我正在使用 VirtualBox)。 当我在 Windows 主机上使用 Termite 时,我可以看到正确的消息,但是当我在 Linux 客户机中使用调试工具时,我只看到这样的乱码文本。

图片:在 Windows 主机上使用 Termites 工作得很好

图片:运行演示代码时出现乱码文本

我也尝试过使用不同的波特率来测试它,但没有成功。另外,它不应该和我在Windows主机中使用的波特率相同吗?它可以让我在 Termite 中看到正确的消息。知道我应该看哪里吗?

作为参考,这里是我在 Linux 中使用的演示代码,来自 https://www.teuniz.net/RS-232/

图片:还从这个调试工具中获得了乱码文本,https://www.teuniz.net/serial-com-tester/index.html

/**************************************************

file: demo_rx.c purpose: simple demo that receives characters from the serial port and print them on the screen, exit the program by pressing Ctrl-C

compile with the command: gcc demo_rx.c rs232.c -Wall -Wextra -o2 -o test_rx

**************************************************/

#include <stdlib.h>
#include <stdio.h>

#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif

#include "rs232.h"



int main() { int i, n, cport_nr=2,        /* /dev/ttyS2 (COM3 on windows) */ bdrate=921600;       /* 9600 baud */

unsigned char buf[4096];

char mode[]={'8','N','1',0};


if(RS232_OpenComport(cport_nr, bdrate, mode, 0))   { printf("Can not open comport\n");

return(0);   }else{ printf("Open COM %d on Windows\n", cport_nr+1);   }

while(1)   { printf("RS232_PollComport...\n"); n = RS232_PollComport(cport_nr, buf, 4095);

if(n > 0)
    { buf[n] = 0;   /* always put a "null" at the end of a string! */

for(i=0; i < n; i++)
      { if(buf[i] < 32)  /* replace unreadable control-codes by dots */
        { buf[i] = '.';
        }
      }

printf("received %i bytes: %s\n", n, (char *)buf);
    }

#ifdef _WIN32 Sleep(100);
#else usleep(100000);  /* sleep for 100 milliSeconds */
#endif   }

return(0); } 
c serial-port virtualbox
1个回答
0
投票

经过几次调试。我们发现这可能是由高速串行通信引起的,而 VirualBox 不支持高速串行通信。我们使用的是 921600 波特率,总是出现乱码,但是将其更改为 115200 后,它就可以了!我们想做进一步的测试,看看不使用虚拟机是否能让我们回到我们需要的高速波特率。

© www.soinside.com 2019 - 2024. All rights reserved.