Hallo, habe folgendes Problem:
In meinem Programm möchte ich die serielle Schnittsttelle öffnen und zu Testzwecken zuerst ein "w" gefolgt von einem CR an den UART eines AVR senden. Mit einem Terminalprogramm, z.b. putty funktioniert alles einwandfrei. Das Programm selbst auf den ersten Blick aber nicht. Jetzt kommt das komische:
Habe ich aber putty im Hintergrund mit geöffneter Verbindung laufen funktioniert es, die beiden Zeichen werden vom Programm gesendet (der AVR reagiert wie erwartet). Kann sich das einer erklären? Meine Vermutung ist, dass ich die Schnittstelle im Programm selbst nicht richtig öffne.
cmdline.txt und inittab sind angepasst, der User pi ist Mitglied der Gruppe dialout.
Hat jemand eine Idee?
ZitatAlles anzeigen#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>void writeBytes(int descriptor, int count);
void readBytes(int descriptor, int count);#define CR 13
char serialBuffer[16]; // Serial buffer to store data for I/O
int main(int argc, char **argv)
{
int fd; // File descriptor of port we will talk to
char *portName = "/dev/ttyAMA0"; // Name of the UART port on the Raspberry pi
struct termios options; // Port optionsfd = open(portName, O_RDWR | O_NOCTTY | O_NONBLOCK); // Open port for read and write not making it a controlling terminal
//fd = open(portName, O_RDWR);
if (fd == -1)
{
perror("openPort: Unable to open port "); // If open() returns an error
}
tcgetattr(fd, &options);
cfsetispeed(&options, B38400); // Set baud rate
cfsetospeed(&options, B38400);
cfmakeraw(&options);
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
usleep(400000); // Sleep for UART to power up and set options
//send data
serialBuffer[0] = 119; // 119=w
serialBuffer[1] = CR;
writeBytes(fd, 2);
close(fd); // Close port
return 0;
}void writeBytes(int descriptor, int count) {
if ((write(descriptor, serialBuffer, count)) == -1) { // Send data out
perror("Error writing");
close(descriptor); // Close port if there is an error
exit(1);
}
}void readBytes(int descriptor, int count) {
if (read(descriptor, serialBuffer, count) == -1) { // Read back data into buf[]
perror("Error reading ");
close(descriptor); // Close port if there is an error
exit(1);
}
}