Difference between revisions of "Сериен порт"

From Ilianko
m (Protected "Упражнение 3. Сериен порт (1)" ([edit=sysop] (indefinite) [move=sysop] (indefinite)))
Line 3: Line 3:
 
<code><pre>
 
<code><pre>
  
−
#include <termios.h>
+
#include <stdio.h>  /* Standard input/output definitions */
−
#include <stdlib.h>
+
#include <string.h> /* String function definitions */
−
#include <stdio.h>
+
#include <unistd.h> /* UNIX standard function definitions */
−
#include <unistd.h>
+
#include <fcntl.h>   /* File control definitions */
−
#include <fcntl.h>
+
#include <errno.h>   /* Error number definitions */
−
     
+
#include <termios.h> /* POSIX terminal control definitions */
−
#define BAUDRATE B2400
+
 
−
#define MODEMDEVICE "/dev/ttyS0"
+
int main(void)
−
     
+
{
−
int  main()
+
    int fd; // File descriptor for the port
−
{
+
    struct termios options; // Serial port settings
−
+
−
        int fd,c, res, stop = 0;
+
−
        struct termios oldtio,newtio;
+
    fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
−
        char buf[255];
+
 +
    if (fd == -1)//Could not open the port.
 +
perror("open_port: Unable to open port");
 +
    else
 +
      fcntl(fd, F_SETFL, 0);
 +
 +
    tcgetattr(fd, &options);
 +
    cfsetispeed(&options, B2400);
 +
    cfsetospeed(&options, B2400);
 +
   
 +
    options.c_cflag |= ( CREAD);
 +
    options.c_cflag |= ( CLOCAL);
 +
    options.c_cflag &= ( ~CSTOPB);
 
          
 
          
−
        /* open the device to be non-blocking (read will return immediatly) */
+
    tcsetattr(fd,TCSANOW, &options);
−
        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
+
 
−
        if (fd < 0) {perror(MODEMDEVICE); exit(-1);}
+
     write(fd, "a \n", 3); \\write to serial port
−
       
+
         
−
        fcntl(fd, F_SETFL, 0);
+
    return 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>
 
</pre></code>

Revision as of 21:35, 19 March 2011

Сериен порт


#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 */

int main(void)
{
    int fd; // File descriptor for the port 
    struct termios options; // Serial port settings
	
	
    fd = open( "/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK);
	
    if (fd == -1)//Could not open the port.
	perror("open_port: Unable to open port");
    else 
       fcntl(fd, F_SETFL, 0);
	
    tcgetattr(fd, &options);
    cfsetispeed(&options, B2400);
    cfsetospeed(&options, B2400);
    
    options.c_cflag |= ( CREAD);
    options.c_cflag |= ( CLOCAL);
    options.c_cflag &= ( ~CSTOPB);
        
    tcsetattr(fd,TCSANOW, &options);

    write(fd, "a \n", 3); \\write to serial port
           
    return 0;
}