unable to send read and write serial port


 
Thread Tools Search this Thread
Top Forums Programming unable to send read and write serial port
# 1  
Old 10-10-2011
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.

Code:
/* 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 !!

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags

Last edited by pludi; 10-10-2011 at 04:36 PM..
# 2  
Old 10-10-2011
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?
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-10-2011
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.
This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 10-11-2011
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 :-

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 !

Last edited by pludi; 10-11-2011 at 03:47 AM..
# 5  
Old 10-11-2011
Once more: please use [CODE] tags when posting source code, command lines, ...
# 6  
Old 10-11-2011
Quote:
Originally Posted by yashwantkumar
i am using geomeda device .. and opening serail port, mapped in /dev/modem.
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..
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 10-11-2011
thanxx corona....
open () is returning value 3.
write() also returning no of bits its writing .
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Cabling and adapters to communicate to service processor serial port from Windows PC with USB port.

Hello, I have an unloaded T5140 machine and want to access the ILOM for the first time and subsequently the network port after that., and then load Solaris 10 the final January 2011 build. The first part is what confuses me -the cabling. I am coming from a Windows machine (w/appropriate... (5 Replies)
Discussion started by: joboy
5 Replies

2. Programming

Read from serial port

Hi I try to communicate with a GSM modem, from C, for sending SMS. I use standart AT-commands. Working well with terminal. There is no problem writing ti the port. But when I try to read I only get a echo, I write "ATI" and get "ATI" back, I should get somthing like "SIEMENS 35... (4 Replies)
Discussion started by: dmiller
4 Replies

3. Programming

Wrong data with Read from a serial port.

hi, I've a problem on my C/C++ program with Posix Library. I have to read data from the serial but I have incorrect data, in fact I get a bunch of zeros: "2953.3174, 2785.2126, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0 , 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ,... (24 Replies)
Discussion started by: enaud
24 Replies

4. Solaris

How to enable Serial port on ILOM, when Network Port is enabled in parallel

Hi Everyone, In my environment, I have few T5220. On the iLOM Management Card, I have both Network and Serial port are cabled, I don't have any issues while I try to connect using Network Management port, but when I try to connect the serial port for the same server which is actually connected... (3 Replies)
Discussion started by: bobby320
3 Replies

5. Solaris

Unable to access serial port from non-global solaris zone on netra 240

I am trying to use a serial communications device that is connected to /dev/ttyb on a netra 240 server. This is a solaris zone configuration using solaris 10 0910. I am able to access /dev/ttyb from the global zone but not throught he non-global zone. I have enabled all of the tty devices in my... (0 Replies)
Discussion started by: disagreeable
0 Replies

6. Shell Programming and Scripting

Need help with serial port

Hi, I have a external board connected to my serial port. I need to execute "shutdown -r now" command when system boot up. When system boots up it requires a username ans password. Then I need to run my command. I can use rc script but that is rebooting system before it asks for username and... (0 Replies)
Discussion started by: charlie.arya
0 Replies

7. IP Networking

read/write,write/write lock with smbclient fails

Hi, We have smb client running on two of the linux boxes and smb server on another linux system. During a backup operation which uses smb, read of a file was allowed while write to the same file was going on.Also simultaneous writes to the same file were allowed.Following are the settings in the... (1 Reply)
Discussion started by: swatidas11
1 Replies

8. AIX

Serial port in AIX

Hi, How can i configure my modem in AIX thru serial port (sa0-->tty0) I have two port serial card configured as sa0 I created tty1 which port is tty0 and which port is tty1 how can i know?? (1 Reply)
Discussion started by: pchangba
1 Replies

9. Solaris

serial port signal

hi i am using solaris 9 on sparc . i was wondering if there was a command to control my serial interface , as to send a signal periodically every interval of time to the input of a 555 timer . thanks for your help .... (0 Replies)
Discussion started by: ppass
0 Replies

10. Programming

serial port reading

Hai there, Can any one provide me with a ansi c source code for opening com1 or com2 and read data. The port is connected to another serial communication device on rs232 port. The o/s is Sco Unix 5.0.6 Matter urgent Viswanath (0 Replies)
Discussion started by: viswanath
0 Replies
Login or Register to Ask a Question