unable to send read and write serial port

hey frns pls help me out !!
i hav a code of c that i have to include in my project.
i am using a device (geomeda) that has unix based OS. it also support SIM card for connecting to server . I need to send SMS to user from this device..
below code is not working .. i am unable to send sms and the read system call is always returning 1.

/* terminal flags are set in above part of code*/
        tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);//argv[1] is /dev/modem
          unsigned char buff_in[255];
        unsigned char buff_out[255];
        memset(buff_out,'\0',sizeof(buff_out))        sprintf(buff_in,"AT\r");
         int m = write(tty_fd,buff_in,strlen(buff_in));
           memset(buff_out,'\0',sizeof(buff_out));
         if (read(tty_fd,&buff_out,2)>0)
         {
                 printf("\nRead from tty_fd sucess");
                 if(strncmp(buff_out,"OK",2)==0)
                 {
                         printf("\nConnection with Modem works Fine");
                 }else
                 {
                         printf("\nError in connection with modem");
                         return 0;
                 }
         }
         memset(buff_in,'\0',sizeof(buff_in));
         sprintf(buff_in,"AT+CMSS=1\r");
         write(tty_fd,&buff_in,sizeof(buff_in));
         memset(buff_out,'\0',sizeof(buff_out));
         if (read(tty_fd,&buff_out,2)>0)
         {
                 if(strcmp(buff_out,"OK")==0)
                 {
                         printf("\nSIM supports SMS services");
                 }else
                 {
                         printf("\nSIM do not supports SMS services");
                         return 0;
                 }
         }
         memset(buff_in,'\0',sizeof(buff_in));
         sprintf(buff_in,"AT+CMGS=\"+91xxxxxxxx\"\r");
        write(tty_fd,buff_in,strlen(buff_in));
        sleep(5);
        memset(buff_in,'\0',sizeof(buff_in));
         sprintf(buff_in,"Hello SMS Test\r\x1A\r");
        write(tty_fd,buff_in,strlen(buff_in));                      
        memset(buff_out,'\0',sizeof(buff_out));
         if (read(tty_fd,&buff_out,35)>0)
         {
                 int value = buff_out[7];
                 printf("\n Value of buff_out[7] is :- %d",value);
                 if(value <0 || value >255)
                 {
                         printf("\nError in sending msg");
                         return 0;
                 }else
                 {
                         printf("\nMsg sent Sucessfully");
                 }
         }          close(tty_fd);
         return 0;
 }

But this code is not working .. i am unable to send sms .. and the read system call always returning -1

please help me !!

Please post in your own thread and be more specific about your problem. What device are you opening? Did you actually check to make sure it succeeds?

1 Like

This really looks like homework to me. It seems like is an android question. Development there nears little relationship to Linux or unix.

If this is not homework, please give the specific answers corona688 requested.

1 Like

i am using geomeda device .. and opening serail port, mapped in /dev/modem.
i have checked these "AT" commands from terminal.. thats works fine..
i have tested a basic code :-

int main(int argc,char** argv)
{
        struct termios tio;
        struct termios stdio;
        int tty_fd;
        fd_set rdset;
        unsigned char c='D';
        printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
        memset(&stdio,0,sizeof(stdio));
        stdio.c_iflag=0;
        stdio.c_oflag=0;
        stdio.c_cflag=0;
        stdio.c_lflag=0;
        stdio.c_cc[VMIN]=1;
        stdio.c_cc[VTIME]=0;
        tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
        tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
        fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);       // make the reads non-blocking
 

        memset(&tio,0,sizeof(tio));
        tio.c_iflag=0;
        tio.c_oflag=0;
        tio.c_cflag=CS8|CREAD|CLOCAL;           // 8n1, see termios.h for more information
        tio.c_lflag=0;
        tio.c_cc[VMIN]=1;
        tio.c_cc[VTIME]=5;
        tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
        cfsetospeed(&tio,B115200);            // 115200 baud
        cfsetispeed(&tio,B115200);            // 115200 baud
        tcsetattr(tty_fd,TCSANOW,&tio);
        while (c!='q')
        {
                if (read(tty_fd,&c,1)>0)        write(STDOUT_FILENO,&c,1);              // if new data is available on the serial port, print it out
                if (read(STDIN_FILENO,&c,1)>0)  write(tty_fd,&c,1);                     // if new data is available on the console, send it to the serial port
        }
        close(tty_fd);
}

its working for stdout but if i try to pass "AT" commands using c var. .. its not working properly !
thanx for help !

Once more: please use [CODE] tags when posting source code, command lines, ...

So, have you checked to make sure the open() call succeeds in your other program? Check the return codes of everything. also try perror("write failed") so you can see what the error actually is.

if write() is returning error, you may need to wait for it to become available. You've opened it with O_NONBLOCK, meaning it'll just return error when not ready..

1 Like

thanxx corona....
open () is returning value 3.
write() also returning no of bits its writing .

Did you try perror to get an explanation of why it's returning error yet?