Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <string.h>
- #include <errno.h>
- #include <termios.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <unistd.h>
- #include <termios.h>
- #include <signal.h>
- __sighandler_t sighandle(int signum, __sighandler_t h) {
- //fprintf(stderr, "BREAK DETECTED\n");
- printf("break com received\n");
- signal(SIGINT, (__sighandler_t) sighandle);
- return SIG_IGN;
- }
- int
- set_interface_attribs (int fd, int speed, int parity)
- {
- struct termios tty;
- memset (&tty, 0, sizeof tty);
- if (tcgetattr (fd, &tty) != 0)
- {
- printf ("error %d from tcgetattr", errno);
- return -1;
- }
- cfsetospeed (&tty, speed);
- cfsetispeed (&tty, speed);
- tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8| BRKINT; // 8-bit chars
- // disable IGNBRK for mismatched speed tests; otherwise receive break
- // as \000 chars
- tty.c_iflag &= ~IGNBRK; // ignore break signal
- 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 ("error %d from tcsetattr", errno);
- 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 ("error %d from tggetattr", errno);
- 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 ("error %d setting term attributes", errno);
- }
- void main(){
- char portname[] = "/dev/ttyO1";
- signal(SIGINT, (__sighandler_t) sighandle);//abilitazione ricezione break
- int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
- int value=0;
- if (fd < 0)
- {
- printf ("error %d opening %s: %s", errno, portname, strerror (errno));
- return;
- }else{
- printf("init port\n");
- }
- value=set_interface_attribs (fd, B19200, 0); // set speed to 115,200 bps, 8n1 (no parity)
- if(value==0) printf("port opened ok\n");
- set_blocking (fd, 0); // set no blocking
- printf("port no blocking ok\n");
- write (fd, "hello!\n", 7); // send 7 character greeting
- char buf [100];
- int n = 0;
- int esci=0;
- int i=0;
- while(esci==0){
- n=read (fd, buf, sizeof buf); // read up to 100 characters if ready to read
- for(i=0;i<n;i++){
- printf("%c",buf[i]);
- if(buf[i]=='*')
- esci=1;
- }
- }
- printf("\n");
- close(fd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement