Witam,
Problem jest następujący:
Napisałem apke w C na raspberrypi pod linuxa , zadanie apki jest dość proste pobiera dane z jednego portu i przesyła na drugi ( w założeniu ma modyfikować te dane ) coś w rodzaju monitora/sniff`era apka działa w jedną stronę super ( pobranie i przesłanie dalej ) ale kiedy dochodzi do komunikacji 2 stronnej wszystko się sypie.
Domyślam się że problem jest w sterowaniu transmisja ewentualnie w buforze.
Wiem że wina leży gdzieś po mojej stronie , za pomoc z góry dziękuje.
Poniżej kod apki
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
int
set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
//error_message ("error %d from tcgetattr", errno);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
// disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf("attribut settings failed\n");
return -1;
}
return 0;
}
void
set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf("attr settings failed - set blocking\n");
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
printf("atribute settings failed-set blocking\n");
}
int main(int argc, char** argv) {
char *portname = "/dev/ttyUSB0";
char *portoutn = "/dev/ttyUSB1";
int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
printf("opening in port failed \n");
return;
}
int fdo = open (portoutn, O_RDWR | O_NOCTTY | O_SYNC);
if (fdo < 0)
{
printf("opening out port failed \n");
return;
}
set_interface_attribs (fd, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity)
set_blocking (fd, 0); // set no blocking
set_interface_attribs (fdo, B115200, 0); // set speed to 115,200 bps, 8n1 (no parity)
set_blocking (fdo, 0); // set no blocking
char buf [2];
do{
int n = read (fd, buf, sizeof buf); // read up to 100 characters if ready to read
// processing colected data from one end to beeing resend , allsow loging data
printf("%s",buf);
write (fdo, buf, sizeof(buf));
// second way
n = read (fdo, buf, sizeof buf);
printf("%s",buf);
write (fd, buf, sizeof(buf));
// maybe we need usleep here , but i am guessing that colecting data to the buffer take the same time as sending so we should not have any problem
}while(1);
return 0;
}