Код:
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
int hport;
void togglerts(int on) {
int status;
ioctl(hport,TIOCMGET,&status);
status=on?(status|TIOCM_RTS):(status&(~TIOCM_RTS));
ioctl(hport,TIOCMSET,&status);
}
void doexchange() {
togglerts(0);
unsigned char data1[]={0x01,0x01,0x03,250};
write(hport,data1,4);
togglerts(-1);
sleep(10);
togglerts(0);
unsigned char data2[]={0x01,0x01,0x04,249};
write(hport,data2,4);
togglerts(-1);
}
int main() {
if((hport=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY))==-1) {
perror("Open /dev/ttyS0");
return EXIT_FAILURE;
}
fcntl(hport,F_SETFL,0);
struct termios options;
tcgetattr(hport,&options);
// Baud rates to 9600
cfsetspeed(&options,B9600);
// cfsetispeed(&options,B9600);
// cfsetospeed(&options,B9600);
// Enable receiver and set local mode
options.c_cflag|=(CLOCAL|CREAD);
// 8 data bits, no 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;
// choose raw input
options.c_cflag&=~(ICANON|ECHO|ECHOE|ISIG);
// disable software flow control
options.c_iflag&=~(IXON|IXOFF|IXANY);
// choose raw output
options.c_oflag&=~OPOST;
// 1s timeout
options.c_cc[VMIN]=0;
options.c_cc[VTIME]=10;
tcsetattr(hport,TCSANOW,&options);
doexchange();
return 0;
}
Правка: извиняюсь, запостил не в тот раздел