Bonita Montero schreibt:
> Dann kopier den Code doch hier rein.
> Bei so einer minimalistischen Aufgabe kann das ja nicht viel sein.
Bitte sehr :-)
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
#include <pthread.h>
#include <stdlib.h>
#define SERIAL "/dev/ttyS0"
#define SERIALUSB "/dev/ttyUSB0"
pthread_t tid[2];
struct data {int src; int dst;};
/*
* 'open_port()' - Open serial port 1.
*
* Returns the file descriptor on success or -1 on error.
*/
int open_port(char * portname)
{
int fd; /* File descriptor for the port */
fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
perror(portname);
perror("open_port: Unable to open port");
}
else
{
fcntl(fd, F_SETFL, 0);
printf("open_port successfull on %s\n", portname);
}
return (fd);
}
int set_options(int fd)
{
struct termios options;
/*
* Get the current options for the port...
*/
tcgetattr(fd, &options);
/*
* Set the baud rates to 9600...
*/
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
/*
* Enable the receiver and set local mode...
*/
options.c_cflag |= (CLOCAL | CREAD);
/*
* Set the new options for the port...
*/
tcsetattr(fd, TCSANOW, &options);
/* data size */
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
/* parity */
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
/* disable hardware flow control */
options.c_cflag &= ~CRTSCTS;
/* also disable software flow control */
options.c_iflag &= ~(IXON | IXOFF | IXANY);
/* raw input (not line oriented) */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
/* choosing raw output */
options.c_oflag &= ~OPOST;
return 0; // show always success, never fail.
}
void *serial_channel(void *arg)
/* int serial_channel(struct data f) */
{
struct data *f = (struct data *)arg;
unsigned char buf[1000];
ssize_t s=0;
int wr_ret = 0;
printf("thread-ID: %ld\n", pthread_self());
printf("file handle src: %i\n", f->src);
printf("file handle dst: %i\n", f->dst);
while(1)
{
/* write(fd, &c,1); */
if((s=read(f->src, &buf, 1000)) != -1)
{
/* printf("0x %s ", buf); */
if( s > 0)
{
wr_ret = write(f->dst, buf, s);
if(wr_ret == -1)
{
printf("writing to %i failed with %i\n", f->dst, errno);
}
else
{
printf("%li bytes from \"%i\" ->", s, f->src);
for (int i = 0; i < s; i++)
{
printf("0x%x ", buf[i]);
}
printf("<- to \"%i\"\n", f->dst);
fflush(stdout);
}
}
}
else
{
perror("read");
}
}
/* return 0; */
}
int main(void)
{
/* unsigned char d; */
struct data *f;
struct data *h;
int fd_ser, fd_usb; /* File descriptor for the port */
if((fd_ser = open_port(SERIAL)) < 0)
{
return fd_ser;
}
if((fd_usb = open_port(SERIALUSB)) < 0)
{
return fd_usb;
}
set_options(fd_ser);
set_options(fd_usb);
/* Speicher fuer Daten anfordern u. m. Werten belegen*/
f = (struct data *)malloc(sizeof(struct data));
if(f == NULL) {
printf("Konnte keinen Speicher reservieren ...!\n");
exit(EXIT_FAILURE);
}
h = (struct data *)malloc(sizeof(struct data));
if(h == NULL) {
printf("Konnte keinen Speicher reservieren ...!\n");
exit(EXIT_FAILURE);
}
f->src = fd_ser;
f->dst = fd_usb;
h->src = fd_usb;
h->dst = fd_ser;
/* serial_channel(&f); */
int i = 0;
int err;
/* while(i < 1) */
/* { */
err = pthread_create(&(tid[i]), NULL, &serial_channel, h);
if (err != 0)
{
printf("\ncan't create thread :[%s]", strerror(err));
}
else
{
printf("\n Thread created successfully\n");
}
i++;
/* } */
err = pthread_create(&(tid[i]), NULL, &serial_channel, f);
if (err != 0)
{
printf("\ncan't create thread :[%s]", strerror(err));
}
else
{
printf("\n Thread created successfully\n");
}
char c;
/* forever loop to keep the treads running */
while((c=getchar()) != 'q')
{
}
close(fd_ser);
close(fd_usb);
return 0;