Advertisement
Guest User

USB Modem

a guest
Feb 14th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.13 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.  
  9. #define DISPLAY_STRING 1
  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. int main(int argc, char* argv[])
  63. {
  64.     int fd, rlen, wlen;
  65.     unsigned char buf[80];
  66.     const char portname[] = "/dev/ttyACM0";
  67.  
  68.     fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  69.     if (fd < 0) {
  70.         printf("Error opening %s: %s\n", portname, strerror(errno));
  71.         return -1;
  72.     }
  73.  
  74.     /*baudrate 115200, 8 bits, no parity, 1 stop bit */
  75.     set_interface_attribs(fd, B115200);
  76.     //set_mincount(fd, 0);                /* set to pure timed read */
  77.  
  78.     wlen = write(fd, "ATZ", 3);
  79.     if (wlen != 3) {
  80.         printf("Error from write: %d, %d\n", wlen, errno);
  81.     }
  82.     tcdrain(fd);    /* delay for output */
  83.  
  84.     wlen = write(fd, "AT+CID=?", 8);
  85.     if (wlen != 8) {
  86.         printf("Error from write: %d, %d\n", wlen, errno);
  87.     }
  88.     tcdrain(fd);    /* delay for output */
  89.  
  90.     /* simple noncanonical input */
  91.     do
  92.     {      
  93.         rlen = read(fd, buf, sizeof(buf) - 1);
  94.         if (rlen > 0) {
  95. #ifdef DISPLAY_STRING
  96.             buf[rlen] = 0;
  97.             printf("Read %d: \"%s\"\n", rlen, buf);
  98. #else /* display hex */
  99.             unsigned char *p;
  100.             printf("Read %d:", rlen);
  101.             for (p = buf; rlen-- > 0; p++)
  102.                 printf(" 0x%x", *p);
  103.             printf("\n");
  104. #endif
  105.         } else if (rlen < 0) {
  106.             printf("Error from read: %d: %s\n", rlen, strerror(errno));
  107.         } else {  /* rlen == 0 */
  108.             printf("Timeout from read\n");
  109.         }              
  110.         /* repeat read to get full message */
  111.     } while (1);
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement