Hallo zusammen,
ich habe ein Zybo Z7 Board und möchte Daten, die ich per UART an den PC
verschicke auf der Konsole (in einem ersten Schritt) und irgendwann
später in eine Datei speichern, auslesen.
Leider funktioniert mein Code nicht.
Hat hier jemand Erfahrungen warum nicht?
Dieses open allein funktioniert schon nicht, wenn ich aber fd als FILE
definiere und dann ein fopen verwende funktioniert es.
1
#include <stdio.h>
2
#include <fcntl.h> /* File Control Definitions */
3
#include <termios.h> /* POSIX Terminal Control Definitions */
4
#include <unistd.h> /* UNIX Standard Definitions */
5
#include <errno.h> /* ERROR Number Definitions */
6
7
void main(void)
8
{
9
int fd;/*File Descriptor*/
10
fd = open ("/dev/ttyUSB1", 'r'); //fd = open("/dev/ttyUSB1",O_RDWR | O_NOCTTY);
11
if(fd == -1) /* Error Checking */
12
printf("\n Error! in Opening ttyUSB1 ");
13
else
14
printf("\n ttyUSB1 Opened Successfully ");
15
16
struct termios SerialPortSettings; /* Create the structure */
17
tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */
18
19
/* Setting the Baud rate */
20
cfsetispeed(&SerialPortSettings,B115200); /* Set Read Speed as 115200 */
21
cfsetospeed(&SerialPortSettings,B115200); /* Set Write Speed as 115200 */
22
23
/* 8N1 Mode */
24
SerialPortSettings.c_cflag &= ~PARENB; /* Disables the Parity Enable bit(PARENB),So No Parity */
25
SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
26
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */
27
SerialPortSettings.c_cflag |= CS8; /* Set the data bits = 8 */
28
29
SerialPortSettings.c_cflag &= ~CRTSCTS; /* No Hardware flow Control */
Dominik schrieb:> fd = open ("/dev/ttyUSB1", 'r'); //fd = open("/dev/ttyUSB1",O_RDWR |> O_NOCTTY);
Was da nach dem "//" (auskommentiert) steht, sieht für mein Gefühl
besser aus, als das davor.
Der Filemodus "rw" ist für "fopen", also eine ganz andere Baustelle.
Wie ist denn das Verhalten?
Keine Fehlermeldungen, nur Warten und keine Ausgaben?
Dominik schrieb:> /* Setting Time outs */> SerialPortSettings.c_cc[VMIN] = 10; /* Read at least 10 characters> */> SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */
bedeutet, das mindestens 10 Zeichen reinkommen müssen, bevor "read"
zurückkommt.
Dominik schrieb:> if(fd == -1) /* Error Checking */> printf("\n Error! in Opening ttyUSB1 ");
Wieso ist das newline am Anfang? Ausserdem ist die Fehlermeldung
normalerweise noch aufschlussreich. perror oder strerror sind dazu gut
geeignet.
Empfangen bestehende Tools, wie z.B. minicom denn etwas?
Übrigens, der Variablenname SerialPortSettings nervt. Das ist eine
temporäre Variable, die man kurzzeitig braucht, also warum der Roman?
> /* Setting the Baud rate */> cfsetispeed(&SerialPortSettings,B115200); /* Set Read Speed as> 115200 */> cfsetospeed(&SerialPortSettings,B115200); /* Set Write Speed as> 115200 */>> /* 8N1 Mode */> SerialPortSettings.c_cflag &= ~PARENB; /* Disables the Parity> Enable bit(PARENB),So No Parity */> SerialPortSettings.c_cflag &= ~CSTOPB; /* CSTOPB = 2 Stop> bits,here it is cleared so 1 Stop bit */> SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the mask for> setting the data size */> SerialPortSettings.c_cflag |= CS8; /* Set the data bits = 8> */>> SerialPortSettings.c_cflag &= ~CRTSCTS; /* No Hardware flow> Control */> SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable> receiver,Ignore Modem Control lines */>>> SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /*> Disable XON/XOFF flow control both i/p and o/p */> SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*> Non Cannonical mode */>> SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/>> /* Setting Time outs */> SerialPortSettings.c_cc[VMIN] = 10; /* Read at least 10 characters> */> SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinetly */
Alles viel zu kompliziert, da du mit tcgetattr arbeitest. Wenn du die
Struktur ausgenullt hast, dann:
Jack schrieb:> Übrigens, der Variablenname SerialPortSettings nervt. Das ist eine> temporäre Variable, die man kurzzeitig braucht, also warum der Roman?
Es ist dein Variablenname aus deinem Programm. Wenn er dir nicht mehr
gefällt, dann einfach suchen/ersetzen.
Man kann nach einenm Fehlermit errno den Grund abfragen.
Und mit strerror den Code noch als String ausgeben.
Wäre doch mal praktisch nachzuschauen, warum es nicht geht.
Jack schrieb:> So, jetzt wird es ernst:fd = open("/dev/ttyUSB1", O_RDWR | O_NONBLOCK);
Warum O_NONBLOCK? Das führt dazu, dass read() sofort zurückkehrt, auch
wenn es gar nix zu lesen gibt.