c - 将字符回显到C中的串行端口

标签 c serial-port communication

我正在尝试使用 C 通过串行端口将字符从 pic32 处理器回显到终端仿真器。系统会提示用户输入一个字符串,所有发生的事情就是字符会在用户键入时出现在屏幕上。这只是为了设置一个初始程序,以后可以将其用于用户的实时菜单选择。示例如下:

main()
{
   // 1. init the console serial port
   initU2();

   // 2. text prompt
   clrscr();
   home();    
   fputs("Enter some text: ", stdout);
   puts(stdout);

   // 3. main loop
   while (1)
   {
      // 3.1 read a full line of text
      getsn(stdout, sizeof(stdout));
      // 3.2 send a string to the serial port
      puts(stdout);
   } // main loop
} // main

这是我在命令行中得到的结果(无论用户是否输入):

输入一些文本:ÿÿÿÿ

我可以简单地将字符回显到终端程序显示,但是如果我需要让用户输入字符,C 程序似乎没有响应。任何帮助将不胜感激!

最佳答案

你写道:

fputs("Enter some text: ", stdout);
puts(stdout);

我认为您对 puts 的调用无效。在普通的 C 库中,puts 期望传递一个指向字符串的指针,但您传递给它的不是字符串 stdout。因此,您会看到一些垃圾字符在串行端口上传输。

尝试删除 puts 行或将您的代码更改为:

puts("Enter some text: ");

关于c - 将字符回显到C中的串行端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8024928/

相关文章:

c - malloc.c :3074 + Valgrind output

java - 蓝牙 COM 端口的 RXTX 问题

Python 串行读取不工作

c - Arduino定时器中断采样

c++ - #if 0 && defined(_LP64) 如何为真?

c++ - 在运算符之间添加间距的目的是什么?

c - MPI_Gatherv : create and collect arrays of variable size (MPI+C)

delphi - Delphi 7上监听串口

communication - 优先考虑开发人员通信的即时消息

multithreading - 看门狗获胜服务,观看另一获胜服务