Advertisement
Guest User

Untitled

a guest
Sep 12th, 2018
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.94 KB | None | 0 0
  1. #include <errno.h>
  2. #include <fcntl.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <termios.h>
  7. #include <unistd.h>
  8. #include <stdint.h>
  9.  
  10. static int fd;
  11. #define MAX_STR_LEN 128
  12. #define MODE_8E1 /**< enables the uart to be in 8E1 mode */
  13. #define DDDEBUG
  14.  
  15. static void close_fd(void);
  16. static int configure_uart(int rawbaud);
  17. static int register_read_request(const uint16_t chip, const uint16_t address, const uint16_t naddr);
  18.  
  19. static void close_fd(void)
  20. {
  21.     int ret = close(fd);
  22.     if (ret)
  23.         printf("Could not close serial device... %s\n\r", strerror(errno));
  24. }
  25.  
  26. int init_uart(char * port)
  27. {
  28.     /* open and configure serial port                    */
  29.     /* O_RDWR   - Read/Write access to serial port       */
  30.     /* O_NOCTTY - No terminal will control the process   */
  31.     /* Open in blocking mode,read will wait              */
  32.     fd = open(port, O_RDWR | O_NOCTTY);
  33.     if (fd < 0){
  34.         printf("Could not open port %s... %s\n\r", port, strerror(errno));
  35.         return -1;
  36.     }
  37.     int ret = configure_uart(B230400);
  38.     if (ret)
  39.         return -1;
  40.  
  41.     printf("Connected to %s\n",port);
  42.     return 0;
  43. }
  44.  
  45. static int configure_uart(int rawbaud)
  46. {
  47.     int ret = 0;
  48.  
  49.     /* causes read to return 0 (non-blocking) */
  50. #if 0
  51.     fcntl(fd, F_SETFL, FNDELAY);
  52.     /* causes read to block */
  53. #endif
  54.     fcntl(fd, F_SETFL, 0);
  55.  
  56.     struct termios attr;
  57.     ret = tcgetattr(fd, &attr);
  58.     if (ret < 0) {
  59.         printf("from tcgetattr: %s\n", strerror(errno));
  60.         return -1;
  61.     }
  62.    
  63.     ret = cfsetispeed(&attr,rawbaud);
  64.     if (ret){
  65.         printf("from cfsetispeed: %s\n",strerror(errno));
  66.         return -1;
  67.     }
  68.    
  69.     ret = cfsetospeed(&attr,rawbaud);
  70.     if (ret){
  71.         printf("from cfsetospeed: %s\n",strerror(errno));
  72.         return -1;
  73.     }
  74.  
  75.     /* 8E1 mode */
  76. #if defined(MODE_8E1)
  77. #warning("MODE_8E1 ENABLED!");
  78.     attr.c_cflag |= PARENB;  /* enable parity */
  79.     attr.c_cflag &= ~PARODD; /* even parity */
  80.     attr.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
  81.     attr.c_cflag &= ~CSIZE;  /* Clears the mask for setting the data size */
  82.     attr.c_cflag |= CS8;     /* 8-bit characters */
  83. #elif defined(MODE_8N1)
  84. #warning("MODE_8N1 ENABLED!");
  85.     attr.c_cflag &= ~PARENB; /* disable parity */
  86.     attr.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
  87.     attr.c_cflag &= ~CSIZE;  /* Clears the mask for setting the data size */
  88.     attr.c_cflag |= CS8;     /* 8-bit characters */
  89. #else
  90.     printf("Must define a mode\n");
  91.     return -1;
  92. #endif
  93.  
  94.     attr.c_cflag &= ~CRTSCTS;         /* no hardware flowcontrol */
  95.     attr.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
  96.  
  97.     ret = tcsetattr(fd,TCSANOW,&attr);
  98.     if(ret){
  99.         printf("from tcsetattr: %s\n", strerror(errno));
  100.         return -1;  
  101.     }
  102.  
  103.     // tcflush(fd,TCIOFLUSH);
  104.  
  105.     return 0;
  106. }
  107.  
  108. static int register_read_request(const uint16_t chip, const uint16_t address, const uint16_t naddr)
  109. {
  110.     uint16_t chipAddr = chip+address;
  111.     char cmd[MAX_STR_LEN];
  112.     memset(&cmd, MAX_STR_LEN, '\0');
  113.     sprintf(cmd,"r %02x %02x%c",chipAddr,naddr,0xa);
  114.     printf("strlen %d : %s\n",(int)strlen(cmd),cmd);
  115.     #ifdef DDDEBUG
  116.         int s = (int)strlen(cmd);
  117.         int i;
  118.         for(i = 0; i < s; i++){
  119.             printf("tx cmd[%d] = %02x %c\n",i,cmd[i],cmd[i]);
  120.             // ssize_t bytes_tx = write(fd, (char *)&cmd[i], 1);
  121.             // printf("bytes : %d\n",(int)bytes_tx);
  122.             // usleep(100);
  123.             // tcflush(fd,TCIOFLUSH);
  124.             // usleep(100);
  125.         }
  126.     #endif
  127.     ssize_t bytes_tx = write(fd, (char *)cmd, strlen(cmd));
  128.     printf("bytes_tx = %d\n",bytes_tx);
  129.     printf("sizeof(\\n) : %d\n",sizeof('\n'));
  130.     if (bytes_tx < 0){
  131.         printf("%s\n",strerror(errno));
  132.         return -1;
  133.     }
  134.     return 0;
  135. }
  136.  
  137. int main()
  138. {
  139.     init_uart("/dev/ttyUSB0");
  140.     register_read_request(0x40, 0x0, 0x0);
  141.     close_fd();
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement