0

I am new to Linux operating system, i am trying to interface GSM module with USB,i interfaced USB with GSM module successfully, but i have problem while reading the data from GSM module, i can able to write data to GSM module but while reading its too difficult for me, some time i can able to read data but some time i can't able to read data, here i copy and pasted my code, please go through the code and please let me where i am failing to read data,

i need to write data normally to USB port and data should be read on interrupt based

USB configuration

int init_ttyusb0(int speed,int parity) {

fd=open("/dev/ttyUSB1",O_RDWR|O_NDELAY|O_NOCTTY|O_APPEND,S_IRWXU|S_IRWXG|S_IRWXO);|O_NOCTTY|O_SYNC|O_NDELAY);
        perror("open");
        if(fd==-1)
        {
                perror("open");
                printf("open usb driver failed\n");
                //close(fd);
                return 1;
        }


        saio.sa_handler= signal_handler_IO;
        saio.sa_flags=0;
        saio.sa_restorer=NULL;
        sigaction(SIGIO,&saio,NULL);

        fcntl(fd,F_SETFL,FNDELAY);
        fcntl(fd,F_SETOWN,getpid());

        tcgetattr(fd,&tty);
        perror("tcgetattr");
        cfsetospeed(&tty,speed);
        perror("cfsetospeed");
        cfsetispeed(&tty,speed);
        perror("cfsetispeed");
        tty.c_cflag=(tty.c_cflag&~CSIZE)|CS8;      // 8bit character
        tty.c_iflag&=~IGNBRK;
//      tty.c_lflag=0;                            // No signaling break process
//      tty.c_oflag=0;                            //No remaping no delay
        tty.c_cc[VMIN]=0;                        // read dosen't block
        tty.c_cc[VTIME]=10;                       //0.5 seconds
        tty.c_iflag&=~(IXON|IXOFF|IXANY);
        tty.c_cflag|=CLOCAL;             // igniore modem controls
        tty.c_cflag|=CREAD;
        tty.c_cflag&=~(PARENB|PARODD);
        tty.c_cflag&=~CSTOPB;
        tty.c_cflag&=~CRTSCTS;
         tty.c_cflag&=~ICANON;      // Disabling cananicle from disbling newline 
        tty.c_cflag&=~ISIG;      // Disabling interrupt signaling bits
//      tty.c_iflag&=~(IXON|IXOFF|IXANY); // Disabling software flow control
        tty.c_lflag&=~ECHO;
        tty.c_lflag&=~ECHOE;
        tty.c_lflag&=~ECHONL;
        tty.c_lflag&=~(IXON|IXOFF|IXANY);
        tty.c_oflag&=~OPOST;


        tcsetattr(fd,TCSANOW,&tty);
        perror("tcsetattr");
        printf("UART configured.....................\n");
}

Interrupt handler
-----------------------

void signal_handler_IO(int status)
{
        static int gsm_count
        read(fd,&zfc_buffer[gsm_count],sizeof(zfc_buffer));
        printf("%s\n",zfc_buffer);
        memset(zfc_buffer,0,sizeof(zfc_buffer));
        fcntl(fd,F_SETFL,!(O_ASYNC));


}


int main(void)
{
int init_ttyusb0(B9600,0)
 while(1)
        {


        printf("\n  eneter option > ");
        scanf("%d",&operation);
        int m=26;
       switch(operation)
       {
               case 0:
         memset(buf_rcv,0,sizeof(buf_rcv));
        buf[0]='A';
        buf[1]='T';
        buf[2]='\r';
        buf[3]='\0';
        write(fd,"AT\r",3);
        usleep((3+25)*100);
        fcntl(fd,F_SETFL,O_ASYNC);  // Enabling interrupt after write
        usleep((3+25)*100);
        perror("write");
        break;

          case 1:

        write(fd,"AT+CFUN=1\r",10); 
        usleep(175000);
        fcntl(fd,F_SETFL,O_ASYNC);// Enabling interrupt after write
        break;

               case 2:
        memset(buf,0,sizeof(buf));
        strcpy(buf,"AT+CCID\r");

        write(fd,"AT+CPIN?\r",9);
        usleep(150000);
        fcntl(fd,F_SETFL,O_ASYNC);// Enabling interrupt after write

        break;

}

sawdust
  • 16,103
  • 3
  • 40
  • 50
MKT
  • 1
  • 2
  • USB is a bus, and not a device. You are accessing a serial terminal (note the "tty" in the device name), and trying to use the **termios** interface. Apparently you have copied & using code that you don't understand. "*data should be read on interrupt based*" -- Why, when you code only sleeps for the signal? That's a lot of busy work for nothing, when the OS provides a simpler and efficient scheme. See https://stackoverflow.com/questions/66733268/best-practise-to-read-desired-amount-of-data-from-serial?noredirect=1&lq=1 – sawdust Jun 09 '22 at 17:48
  • Your use of **printf()** in a signal handler is problematic. See https://stackoverflow.com/questions/16891019/how-to-avoid-using-printf-in-a-signal-handler But if your code was reasonably constructed for a basic command-response dialog with a modem, then you wouldn't even need to use a signal handler. – sawdust Jun 09 '22 at 18:11

0 Answers0