Advertisement
Guest User

uart

a guest
Jul 12th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.35 KB | None | 0 0
  1. #define TERMINAL    "/dev/ttyS2"
  2.  
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <termios.h>
  9. #include <unistd.h>
  10.  
  11. int set_interface_attribs(int fd, int speed)
  12. {
  13.     struct termios tty;
  14.  
  15.     if (tcgetattr(fd, &tty) < 0) {
  16.         printf("Error from tcgetattr: %s\n", strerror(errno));
  17.         return -1;
  18.     }
  19.  
  20.     cfsetospeed(&tty, (speed_t)speed);
  21.     cfsetispeed(&tty, (speed_t)speed);
  22.  
  23.     tty.c_cflag |= (CLOCAL | CREAD);    /* ignore modem controls */
  24.     tty.c_cflag &= ~CSIZE;
  25.     tty.c_cflag |= CS8;         /* 8-bit characters */
  26.     tty.c_cflag &= ~PARENB;     /* no parity bit */
  27.     tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
  28.     tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */
  29.  
  30.     /* setup for non-canonical mode */
  31.     tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
  32.     tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
  33.     tty.c_oflag &= ~OPOST;
  34.  
  35.     /* fetch bytes as they become available */
  36.     tty.c_cc[VMIN] = 1;
  37.     tty.c_cc[VTIME] = 1;
  38.  
  39.     if (tcsetattr(fd, TCSANOW, &tty) != 0) {
  40.         printf("Error from tcsetattr: %s\n", strerror(errno));
  41.         return -1;
  42.     }
  43.     return 0;
  44. }
  45.  
  46. void set_mincount(int fd, int mcount)
  47. {
  48.     struct termios tty;
  49.  
  50.     if (tcgetattr(fd, &tty) < 0) {
  51.         printf("Error tcgetattr: %s\n", strerror(errno));
  52.         return;
  53.     }
  54.  
  55.     tty.c_cc[VMIN] = mcount ? 1 : 0;
  56.     tty.c_cc[VTIME] = 5;        /* half second timer */
  57.  
  58.     if (tcsetattr(fd, TCSANOW, &tty) < 0)
  59.         printf("Error tcsetattr: %s\n", strerror(errno));
  60. }
  61.  
  62.  
  63. int main()
  64. {
  65.     char *portname = TERMINAL;
  66.     int fd;
  67.     int wlen;
  68.     char *xstr = "Hello!\n";
  69.     int xlen = strlen(xstr);
  70.        
  71.     //  FILE *fp;
  72. //  fp = fopen("out.txt", "w");
  73.     fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  74.     if (fd < 0) {
  75.         printf("Error opening %s: %s\n", portname, strerror(errno));
  76.         return -1;
  77.     }
  78.     /*baudrate 115200, 8 bits, no parity, 1 stop bit */
  79.     set_interface_attribs(fd, B115200);
  80.    // set_mincount(fd, 0);                /* set to pure timed read */
  81.  
  82.     /* simple output */
  83.     wlen = write(fd, xstr, xlen);
  84.     if (wlen != xlen) {
  85.         printf("Error from write: %d, %d\n", wlen, errno);
  86.     }
  87.     tcdrain(fd);    /* delay for output */
  88.  
  89.  
  90.     /* simple noncanonical input */
  91.    // do {
  92.         unsigned char buf[80];
  93.     //  unsigned char MeasData[512];
  94.         int rdlen;
  95.         int i, time, offset, date, p, buffsize;
  96.         unsigned short lambda, type, pages;
  97.         float Yvalue;
  98.         rdlen = read(fd, buf, sizeof(buf) - 1);
  99.        
  100.         if (((int)buf[0]==122)&&((int)buf[2])==20)
  101.         {
  102.        
  103.         printf("LD 07....OK \n");          
  104.         pages = *(unsigned short *)(&buf[12]);
  105.         printf("PAGES TO READ %d\n", pages);
  106.        
  107.        
  108.         //unsigned char MeasData[512*4096];
  109.         //unsigned char * MeasData = ( unsigned char * )malloc( 512*pages * sizeof( unsigned char ) );
  110.         unsigned char MeasData[512*pages];
  111.     //  unsigned char *MeasData = malloc(sizeof(unsigned char)*(512*pages));
  112.        
  113.         for (i=0;i<512*pages; i++)
  114.        {       
  115.        
  116.         rdlen = read(fd, buf, sizeof(buf)-1);
  117.         MeasData[i] = buf[12];             
  118.    
  119.         }
  120.  
  121.         for (i=0; i<512*pages; i++)
  122.        
  123.         {
  124.         date     = *(int *)(&MeasData[0+offset]);
  125.         time     = *(int *)(&MeasData[4+offset]);
  126.         lambda   = *(unsigned short *)(&MeasData[8+offset]);       
  127.         Yvalue   = *(float *)(&MeasData[12+offset]);   
  128.         type     = *(unsigned short*)(&MeasData[16+offset]) ;
  129.        
  130.         printf("Date: %02d.%02d", (date&0xFF),((date>>8)&0xFF));
  131.         printf(" Time: %02d:%02d:%02d", ((time>>16)&0xFF),((time>>8)&0xFF),(time&0xFF));
  132.         printf(" λ: 0.%d:", lambda);
  133.         printf(" Yvalue: %f", Yvalue);
  134.         printf(" type: %d", type);
  135.         printf("\n");
  136.         offset += 32;
  137.         }  
  138.         //fwrite(&MeasData, sizeof(MeasData), 1, fp);
  139.         //fclose(fp);
  140.    
  141.         if (rdlen > 0) {
  142.             /* display hex */
  143.           //  unsigned char   *p;
  144.          //   printf("Read %d:", rdlen);
  145.          //  for (p = buf; rdlen-- > 0; p++)
  146.             //  printf(" 0x%x", *p);
  147.         //      printf("\n");
  148.  
  149.        
  150.        
  151.         } else if (rdlen < 0) {
  152.             printf("Error from read: %d: %s\n", rdlen, strerror(errno));
  153.         } else {  /* rdlen == 0 */
  154.             printf("Timeout from read\n");
  155.         }              
  156.         /* repeat read to get full message */
  157.   // } //while (1);
  158.         }else
  159.         {
  160.             printf ("Restart App\n");
  161.         }
  162.        
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement