Advertisement
Guest User

USB Modem

a guest
Feb 14th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 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. void write_command(int fd, const void* cmd, size_t len)
  63. {
  64.     if (len == ~(size_t)0)
  65.         len = strlen((const char*)cmd);
  66.    
  67.     printf("Send %d: %s\n", (int)len, (const char*)cmd);
  68.  
  69.     int wlen = write(fd, cmd, len);
  70.  
  71.     if (wlen != len) {
  72.         printf("Error from write: %d, %d\n", wlen, errno);
  73.     }
  74.     tcdrain(fd);    /* delay for output */
  75. }
  76.  
  77. void read_response(int fd)
  78. {
  79.     char buf[256];
  80.     int rlen = read(fd, buf, sizeof(buf) - 1);
  81.  
  82.     if (rlen > 0) {
  83. #ifdef DISPLAY_STRING
  84.         buf[rlen] = 0;
  85.         printf("Read %d: \"%s\"\n", rlen, buf);
  86. #else /* display hex */
  87.         unsigned char *p;
  88.         printf("Read %d:", rlen);
  89.         for (p = buf; rlen-- > 0; p++)
  90.             printf(" 0x%x", *p);
  91.         printf("\n");
  92. #endif
  93.     } else if (rlen < 0) {
  94.         printf("Error from read: %d: %s\n", rlen, strerror(errno));
  95.     } else {  /* rlen == 0 */
  96.         printf("Timeout from read\n");
  97.     }
  98. }
  99.  
  100. int main(int argc, char* argv[])
  101. {
  102.     int fd;
  103.     unsigned char buf[80];
  104.     const char portname[] = "/dev/ttyACM0";
  105.  
  106.     fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
  107.     if (fd < 0) {
  108.         printf("Error opening %s: %s\n", portname, strerror(errno));
  109.         return -1;
  110.     }
  111.  
  112.     /*baudrate 115200, 8 bits, no parity, 1 stop bit */
  113.     set_interface_attribs(fd, B115200);
  114.     //set_mincount(fd, 0);                /* set to pure timed read */
  115.    
  116.     write_command(fd, "AT", 2);  /* Attention */
  117.    
  118.     read_response(fd);
  119.  
  120.     write_command(fd, "E0", 2);  /* Echo off */
  121.    
  122.     read_response(fd);
  123.  
  124.     write_command(fd, "ATZ", 3); /* Reset */
  125.    
  126.     read_response(fd);
  127.  
  128.     write_command(fd, "AT+CID=?", -1); /* Query CallerID */
  129.  
  130.     read_response(fd);
  131.    
  132.     write_command(fd, "AT&F%E1", -1);  /* http://www.modemhelp.org/inits/rockwell.html */
  133.  
  134.     read_response(fd);
  135.  
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement