简体   繁体   English

为什么我的串口通讯不起作用?

[英]Why is my serial communication not working?

I looked in the manual (page 177) for the DE2 and as far as I understand it should be possible to do serial communication for instance via putty and a usb-to-serial cable to the board, so I take the program from the manual: 我查看了DE2的手册 (第177页),据我所知,应该可以通过putty和usb-to-serial电缆进行串行通信,因此我从手册中获取程序:

/* 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;
}

And I try to run it as Nios2 hardware but then I get this message when I have configured std i/o to use uart 我尝试将其作为Nios2硬件运行但是当我配置std i / o使用uart时我收到此消息

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

And then when I connect with a terminal program (putty serial connection) it doesn't connect. 然后当我连接终端程序(putty串行连接)时,它没有连接。 What am I doing wrong? 我究竟做错了什么? I tried in the propeties of the projet to change the std i/o to uart but that didn't help. 我试图将projet i / o改为uart,但这并没有帮助。 Can you help me? 你能帮助我吗?

HOW TO DEBUG SERIAL COMMUNICATION: 如何调试串行通信:

Take your 9 pin serial cable and put a jumper pins 2 and 3 together. 使用9针串行电缆并将跳线针脚2和3放在一起。 You can use a paperclip or whatever you have handy. 您可以使用回形针或任何方便的东西。 Pins 2 and 3 are TX and RX. 引脚2和3是TX和RX。 If you connect them together, any command you send from the computer will be received by the computer. 如果将它们连接在一起,计算机将接收从计算机发送的任何命令。 You have created a serial loopback! 你创建了一个串行环回!

Open Putty, try to connect to your serial cable. 打开Putty,尝试连接到您的串行电缆。 Hit any key on your keyboard. 点击键盘上的任意键。 Baud rate and things don't matter because it's a loopback. 波特率和事情并不重要,因为它是一个环回。

If you see the character you sent out received on the terminal, your serial cable works! 如果您在终端上看到收到的字符,则串行电缆可以正常工作! If not, you have a problem with your cable or with putty. 如果没有,您的电缆或腻子有问题。 I have had issues with putty in the past with serial communication. 我在过去通过串行通信遇到过putty的问题。 Try downloading Tera Term if you have a problem with Putty not connecting. 如果您有Putty未连接的问题,请尝试下载Tera Term

Or find new drivers for your serial cable! 或者找到串口线的新驱动程序! Good luck. 祝好运。

In Linux, I would do the following. 在Linux中,我会做以下事情。

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

or 要么

look for ttyS1; 寻找ttyS1; if ttyS0 is being used. 如果正在使用ttyS0。

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

Do dmesg to know which exact device are you have attached. dmesg知道你附加了哪个确切的设备。

$tail -f /var/log/messages

For USB-serial, it might be 对于USB-serial,它可能是

/dev/ttyUSB0; 的/ dev / ttyUSB0; the exact number can be find out using /var/log/messages 可以使用/ var / log / messages查找确切的数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM