问题
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:
/* 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-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. 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. Can you help me?
回答1:
HOW TO DEBUG SERIAL COMMUNICATION:
Take your 9 pin serial cable and put a jumper pins 2 and 3 together. You can use a paperclip or whatever you have handy. Pins 2 and 3 are TX and 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. 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. Try downloading Tera Term if you have a problem with Putty not connecting.
Or find new drivers for your serial cable! Good luck.
回答2:
In Linux, I would do the following.
fp = fopen ("/dev/ttyS0", "r+"); //Open file for reading and writing
or
look for ttyS1; if ttyS0 is being used.
fp = fopen ("/dev/ttyS1", "r+"); //Open file for reading and writing
Do dmesg to know which exact device are you have attached.
$tail -f /var/log/messages
For USB-serial, it might be
/dev/ttyUSB0; the exact number can be find out using /var/log/messages
来源:https://stackoverflow.com/questions/19023485/why-is-my-serial-communication-not-working