Код: Выделить всё
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
int open_port(char *str){
int fd0;
fd0 = open (str, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd0==-1) perror("open_port: Unable to open port /dev/ttyS0 - ");
else fcntl(fd0,F_SETFL, 0);
return fd0;
}
int main () {
int fd0,fd1,n,i,k;
struct termios options;
char com1;
char com2;
char str1[4096];
//Open port /dev/ttyS0 //
fd0=open_port("/dev/ttyS0");
tcgetattr(fd0, &options);
//Input 1 sek time out//
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
//Changing options----------------
tcsetattr(fd0, TCSANOW, &options);
//--------------------------------
//Open port /dev/ttyS1 //
fd1=open_port("/dev/ttyS1");
tcgetattr(fd1, &options);
//Input 1 sek time out//
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 10;
//Changing options
tcsetattr(fd1, TCSANOW, &options);
//--------------------------------
for(i=0;i<11;i++){// for(i=0;i<2;i++)
if(n = write(fd0,"1234567890",4096)==-1)
perror("write fd0 \" 1234567890\" \n");
if(k = read(fd1, str1,4096)== -1)
perror("reading Failed\n");
printf("%s\n", str1);
}
close(fd0);
close(fd1);
return 0;
}
в общем вот что получилось. Вроде все пишет и читает. Я вот только не понял какой там размер буфера максимальный, а то читает он немного странно строчка уплывает все время..
И вот сразу еще вопрос:
Нужно будет потом эту програмульку запускать процессом, чтоб он слушала TCP порт получала комманды и посылала соответственно. Как бы ето сделать, есть идеи?
Мне кажется, что проще сделать типа сервиса, он слушает порт, берет комманду и передает ее этой програмке... или можно еще как то??