Ich habe ein HC-05-Bluetooth-Modul an microZed Board angeschlossen und versucht, Daten über uart in Linux zu senden und zu empfangen, jetzt funktioniert der Sendecode, wenn ich Daten von meinem Board sende zu der App ist es gegeben funktioniert perfekt unter meinem Codeserielle Schnittstelle stecken bleiben bei lesen() Daten
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h> /* for threads */
#include <termios.h> /* uart */
#include <fcntl.h> /* uart */
#include <unistd.h> /* uart */
#define MODEMDEVICE "/dev/ttyPS1"
int main()
{
printf("Opening %s\n", MODEMDEVICE);
int portfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (portfd < 0) {
printf("ERROR coultn't open %s\n", MODEMDEVICE);
return -1;
}
/* set terminal settings */
struct termios tty;
tcgetattr(portfd, &tty);
cfsetospeed(&tty, (speed_t)B9600);
cfsetispeed(&tty, (speed_t)B9600);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag = IGNBRK;
tty.c_lflag = ICANON;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= PARENB;
tty.c_cflag &= ~CSTOPB;
tcsetattr(portfd, TCSANOW, &tty);
/* sleep a bit */
usleep(200000);
/* flush possible characters in the input buffer */
tcflush(portfd, TCIOFLUSH);
char buf;
int i;
while(1) {
buf++;
write(portfd, &buf, 1);
write(portfd, "\r\n", 2);
usleep(200000);
}
return 0;
}
Nun ist das Problem entsteht, wenn ich Daten aus der App an das Modul Bluetooth senden versuchen, manchmal das Programm stoppt und sagt, „random nonblocking Pool initialisiert“ für das senden oder es bleibt bei i = read(portfd, buf, 20);
in dem unten angegebenen Code stecken
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <pthread.h> /* for threads */
#include <termios.h> /* uart */
#include <fcntl.h> /* uart */
#include <unistd.h> /* uart */
#define MODEMDEVICE "/dev/ttyPS1"
int main()
{
printf("Opening %s\n", MODEMDEVICE);
int portfd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (portfd < 0) {
printf("ERROR coultn't open %s\n", MODEMDEVICE);
return -1;
}
printf("hello1\n\r");
/* set terminal settings */
struct termios tty;
tcgetattr(portfd, &tty);
cfsetospeed(&tty, (speed_t)B9600);
cfsetispeed(&tty, (speed_t)B9600);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
tty.c_iflag = IGNBRK;
tty.c_lflag = ICANON;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
tty.c_cc[VTIME] = 0;
tty.c_cc[VMIN] = 1;
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
tty.c_cflag &= ~(PARENB | PARODD);
tty.c_cflag |= PARENB;
tty.c_cflag &= ~CSTOPB;
tcsetattr(portfd, TCSANOW, &tty);
/* sleep a bit */
printf("hello2\n\r");
usleep(200000);
/* flush possible characters in the input buffer */
tcflush(portfd, TCIOFLUSH);
char buf[20];
printf("hello3\n\r");
int i;
while(1) {
i = read(portfd, buf, 20);
printf("hello\n\r");
buf[i] = 0;
printf("%s", buf);
printf("\n\r");
}
return 0;
}
Irgendwelche Vorschläge, wie kann ich das beheben?
Erwarten Sie, dass das 2. Programm die Antwort liest, was das 1. Programm gesendet hat? – alk
Der sendende Code sendet auch den nicht initialisierten Inhalt von 'buf'. – alk
Sie müssen die Rückkehrcodes aller Systemaufrufe besser überprüfen. Ihre termios-Einstellungen sind nicht rational. Zuerst deaktivierst du PARENB, dann stellst du es wieder her. Und PARENB ohne ISTRIP ist ungewöhnlich. Siehe [Terminalmodi richtig einstellen] (http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_12.html#SEC237) und [Serielle Programmieranleitung für POSIX-Betriebssysteme] (http: //www.cmrr.umn.edu/~strupp/serial.html). Sind Sie sicher, dass die erhaltenen Daten kanonisch sind? – sawdust