【问题标题】:Why is my serial communication not working?为什么我的串行通信不起作用?
【发布时间】:2013-10-02 03:23:01
【问题描述】:

我查看了 DE2 的 manual(第 177 页),据我所知,它应该可以进行串行通信,例如通过腻子和连接到板上的 USB 到串行电缆,所以我采取手册中的程序:

/* A simple program that recognizes the characters 't' and 'v' */
#include <stdio.h>
#include <string.h>
int main ()
{
char* msg = "Detected the character 't'.\n";
FILE* fp;
char prompt = 0;
fp = fopen ("/dev/uart1", "r+"); //Open file for reading and writing
if (fp)
{
while (prompt != 'v')
{ // Loop until we receive a 'v'.
prompt = getc(fp); // Get a character from the JTAG UART.
if (prompt == 't')
{ // Print a message if character is 't'.
fwrite (msg, strlen (msg), 1, fp);
}
if (ferror(fp))// Check if an error occurred with the file pointer 
clearerr(fp); // If so, clear it.
}
fprintf(fp, "Closing the JTAG UART file handle.\n");
fclose (fp);
}
return 0;
}

我尝试将它作为 Nios2 硬件运行,但是当我将 std i/o 配置为使用 uart 时收到此消息

nios2-terminal: can't open uart: No such file or directory

然后当我连接终端程序(腻子串行连接)时,它没有连接。我究竟做错了什么?我尝试在项目的特性中将 std i/o 更改为 uart,但这并没有帮助。你能帮帮我吗?

【问题讨论】:

  • 请先缩进你的代码。
  • 你正在迭代你正在修改的文件..
  • 第一次尝试:"/dev/uart1" 在 shell 上会显示文件名...也将你的代码写成 $sudo ./exename

标签: c serial-port fpga uart nios


【解决方案1】:

如何调试串行通信:

拿起 9 针串行电缆,将跳线针 2 和 3 放在一起。你可以使用回形针或任何你手边的东西。引脚 2 和 3 是 TX 和 RX。如果将它们连接在一起,则您从计算机发送的任何命令都将被计算机接收。您已创建串行环回!

打开 Putty,尝试连接到您的串行电缆。敲击键盘上的任意键。波特率和事情无关紧要,因为它是一个环回。

如果您在终端上看到您发送的字符,则说明您的串行电缆有效!如果没有,您的电缆或腻子有问题。我过去在串行通信方面遇到过腻子问题。如果您遇到 Putty 无法连接的问题,请尝试下载 Tera Term

或者为您的串行电缆寻找新的驱动程序!祝你好运。

【讨论】:

    【解决方案2】:

    在 Linux 中,我会执行以下操作。

    fp = fopen ("/dev/ttyS0", "r+"); //Open file for reading and writing
    

    寻找 ttyS1;如果正在使用 ttyS0。

    fp = fopen ("/dev/ttyS1", "r+"); //Open file for reading and writing
    

    执行 dmesg 以了解您连接的确切设备。

    $tail -f /var/log/messages
    

    对于USB-serial,它可能是

    /dev/ttyUSB0;可以使用 /var/log/messages 找到确切的数字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-29
      相关资源
      最近更新 更多