Difference between revisions of "Упражнение 4. Сериен порт (2)"

From Ilianko
Line 16: Line 16:
  
 
== приемане ==
 
== приемане ==
 +
<code><pre>
 +
#include <termios.h>
 +
#include <stdlib.h>
 +
#include <stdio.h>
 +
#include <unistd.h>
 +
#include <fcntl.h>
 +
     
 +
#define BAUDRATE B9600
 +
#define MODEMDEVICE "/dev/ttyS0"
 +
     
 +
int  main()
 +
{
 +
 +
        int fd,c, res, stop = 0;
 +
        struct termios oldtio,newtio;
 +
        char buf[255];
 +
       
 +
        /* open the device to be non-blocking (read will return immediatly) */
 +
        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
 +
        if (fd < 0) {perror(MODEMDEVICE); exit(-1);}
 +
       
 +
        fcntl(fd, F_SETFL, 0);
 +
    fcntl(fd, F_SETFL, FNDELAY);       
 +
       
 +
       
 +
             
 +
        tcgetattr(fd,&oldtio); /* save current port settings */
 +
        /* set new port settings for canonical input processing */
 +
        newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
 +
        newtio.c_iflag = IGNPAR | ICRNL;
 +
        newtio.c_oflag = 0;
 +
        newtio.c_lflag = ICANON;
 +
       
 +
        tcflush(fd, TCIFLUSH);
 +
       
 +
            newtio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
 +
            newtio.c_cflag &= ~( CRTSCTS );
 +
            newtio.c_oflag &= ~OPOST;
 +
            newtio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
 +
            newtio.c_cflag &= ~(CSIZE|PARENB);
 +
            newtio.c_cflag |= CS8;
 +
       
 +
        newtio.c_cc[VMIN]=1;
 +
        newtio.c_cc[VTIME]=0;
 +
        tcsetattr(fd,TCSANOW,&newtio);
 +
       
 +
        /* loop while waiting for input. normally we would do something
 +
          useful here */
 +
             
 +
    while ( stop == 0)
 +
{
 +
       
 +
        usleep(100);
 +
        fflush (stdout);
 +
        res = read(fd,buf,10);
 +
            // strcat(buf,buf2);
 +
         
 +
            for( c=0;c<res; c=c+1)
 +
            {
 +
              if ( buf[c] == 0xd ) 
 +
              printf ("\n");
 +
              else
 +
              {
 +
if (buf[c] == 0x61) { printf("oko");stop = 1;}
 +
printf("%c", (char) buf[c]);
 +
      }
 +
           
 +
            }
 +
        }
 +
       
 +
        /* restore old port settings */
 +
        tcsetattr(fd,TCSANOW,&oldtio);
 +
return 0;
 +
}
 +
 +
</pre></code>

Revision as of 08:38, 29 March 2011

Разглеждане на сигнал на сериен порт

Задача 1 Създайте програма с безкраен цикъл, която да изпраща една буква към серийния порт. Използвайте специално пригодения кабел и разгледайте осцилограмата на серийни сигнал.



structures

cp, mv, mkdir, rm


приемане

#include <termios.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
      
#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyS0"
       
 int  main()
 {
 
        int fd,c, res, stop = 0;
        struct termios oldtio,newtio;
        char buf[255];
        
        /* open the device to be non-blocking (read will return immediatly) */
        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
        if (fd < 0) {perror(MODEMDEVICE); exit(-1);}
        
        fcntl(fd, F_SETFL, 0);
	    fcntl(fd, F_SETFL, FNDELAY);        
        
        
               
        tcgetattr(fd,&oldtio); /* save current port settings */
        /* set new port settings for canonical input processing */
        newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
        newtio.c_iflag = IGNPAR | ICRNL;
        newtio.c_oflag = 0;
        newtio.c_lflag = ICANON;
        
        tcflush(fd, TCIFLUSH);
        
            newtio.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
            newtio.c_cflag &= ~( CRTSCTS );
            newtio.c_oflag &= ~OPOST;
            newtio.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
            newtio.c_cflag &= ~(CSIZE|PARENB);
            newtio.c_cflag |= CS8;
        
        newtio.c_cc[VMIN]=1;
        newtio.c_cc[VTIME]=0;
        tcsetattr(fd,TCSANOW,&newtio);
         
        /* loop while waiting for input. normally we would do something
           useful here */ 
              
    while ( stop == 0)
	{
         
         usleep(100); 
         fflush (stdout);
         res = read(fd,buf,10);
            // strcat(buf,buf2);
          
            for( c=0;c<res; c=c+1)
            {
              if ( buf[c] == 0xd )  
              printf ("\n");
              else 
              {
		if (buf[c] == 0x61) { printf("oko");stop = 1;}
		printf("%c", (char) buf[c]);
	      }
            
            }
        }
        
        /* restore old port settings */
        tcsetattr(fd,TCSANOW,&oldtio);
 return 0;
 }