LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-04-2008, 09:17 AM   #1
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Rep: Reputation: 32
Can read from /dev/ttyS0, but not write?


Hello,
I have a Siemens C45 GSM connected to my serial port. I can play with its modem using the AT commands via minicom. I want to bypass minicom and use the serial modem in my own scripts/programs. I've run simple experiments and it seems I can only read from /dev/ttyS0, but not write.
I've tried in bash:
Code:
### write session
$ cat > /dev/ttyS0
at+clip=?
at+clip=1

### read session at the same time
$ cat /dev/ttyS0
at+clip=?
at+clip=1

RING

ERROR
Both of these commands at+clip must return some output, but all I can see in the read session is the commands themselves. Also I can read 'RING' from the device when it recieve incomming call.

I've tried in C:
Code:
        fp = fopen(serial, "rw");
        if (!fp){
                printf("Can't open serial!\n");
                exit(1);
        }
        fputs("AT+CLIP=1\n", fp);
        while (fgets(buff, 1024, fp)){
                printf("BUF: %s...\n", buff);
        }
And again only read:
Code:
BUF: 
...
BUF: RING
...
I am sure the AT+CLIP=1 command is not reached to the device.
Here are the device permissions:
Code:
$ ls -l /dev/ttyS0                                    
lrwxrwxrwx 1 root root 5 2008-11-03 22:22 /dev/ttyS0 -> tts/0
$ ls -l /dev/tts/0                                    
crw-rw-rw- 1 root uucp 4, 64 2008-11-04 17:02 /dev/tts/0
Any ideas what is wrong with the write process?
 
Old 11-04-2008, 11:07 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
For starters, in your C code, try
Code:
fp = fopen("/dev/ttyS0", "rw");
rather than opening 'serial'.

Have you studied Serial Programming Guide for POSIX Operating Systems, or Serial Programming HOWTO? These have working code that can be used as the basis for your application.

--- rod.

Last edited by theNbomr; 11-04-2008 at 11:10 AM.
 
Old 11-04-2008, 03:41 PM   #3
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Original Poster
Rep: Reputation: 32
Oh, I forgot to complete the copy-paste from the code I have that in variable declarations:
Code:
char *serial = "/dev/ttyS0";
I'm going to check these HOWTO's but I think I've checked these before.
-----------------------------------------------------------------------------------
I've tried some source from these links, like that:
Code:
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */

void send(int fd, char *string){
        int num = strlen(string);
        int n = write(fd, string, num);
        printf("Sent: %s, %d chars\n", string, n);
}
 
int main(){
        int fd;
        int size;
        char buff[255];
        fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
        if (fd == -1){
                printf("Can't open port!\n");
                return 1;
        }
        else {
                fcntl(fd, F_SETFL, 0);
                send(fd, "AT+CLIP=1");

                while(1){
                        buff[0] = '\0';    //reset the buffer, becouse I'm going to display it every time
                        size = read(fd, buff, 255);
                        printf("GOT: %s\n",buff);
                }
                return 0;
        }
}
I'm testing it and it can't write to the serial port, agian. Again the written characters are fetched/redirected for the reading buffer.
Reading is not fine too - read() returns only about 8 characters at once and sometimes I see only garbage:
Code:
Sent: AT+CLIP=1
, 10 chars
GOT: AT+CLIP=
GOT: 1
+CLIP=
GOT: 
RING

GOT: 
+CLIP:
GOT:  "+35988
GOT: 6499768"
GOT: ,145,,,,0
·À¶å·Ðß¿ü5ü·2ü·aÖý·h4ü·àß¿ã~ý·
GOT: 
0RROR
·À¶å·Ðß¿ü5ü·2ü·aÖý·h4ü·àß¿ã~ý·
GOT:
It seems using the serial port is kinda more difficult than using a simple file
I think I should not set anything in special (baud rate, flow control, etc), becouse minicom is communicating well enough without special settings.

Last edited by ivanatora; 11-04-2008 at 04:05 PM.
 
Old 11-04-2008, 08:24 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by ivanatora View Post
It seems using the serial port is kinda more difficult than using a simple file
I think I should not set anything in special (baud rate, flow control, etc), becouse minicom is communicating well enough without special settings.
This is not always the correct assumption. An application is free to use whatever settings it sees as appropriate. A well-behaved application, after having done so, puts things back the way the were before it changed anything. So, minicom may have set up the port to its liking, but not left it in a configuration that suits your application.

BTW, what evidence is telling you that writes are not happening?

--- rod.
 
Old 11-05-2008, 02:31 PM   #5
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Original Poster
Rep: Reputation: 32
Quote:
Originally Posted by theNbomr View Post
BTW, what evidence is telling you that writes are not happening?

--- rod.
After I submit that command "AT+CLIP=1" later it could be checked if it is executed. It should first return an "OK" or "ERROR" string (which it does not), and later when I log into minicom and try "AT+CLIP=?" it should read the status and it should return "1" if the command is executed or "0" if not - and it is "0". This is why I think the write process is not right.
 
Old 11-05-2008, 04:00 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Perhaps you need to flush the output buffer, &/or append a linefeed &/or carriage return to your output string.
--- rod.
 
Old 11-06-2008, 10:48 AM   #7
ivanatora
Member
 
Registered: Sep 2003
Location: Bulgaria
Distribution: Ubuntu 9.10, FreeBSD 7.2
Posts: 459

Original Poster
Rep: Reputation: 32
I've tried sending "command" as well as "command\n", "command\r" and combinations of both.
Quote:
This is not always the correct assumption. An application is free to use whatever settings it sees as appropriate. A well-behaved application, after having done so, puts things back the way the were before it changed anything. So, minicom may have set up the port to its liking, but not left it in a configuration that suits your application.
You are right about that. I will check the minicom default configuration and implement it in my code.
 
Old 11-06-2008, 05:20 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, a few other ideas. The serial port may be inhibited from sending without the presence of the required signals on modem control lines. This can be over-ridden with the correct settings either in termios (see in particular CLOCAL & CRTSCTS, maybe more) or using stty, or may be supplied using a properly configured cable.
Since this is evidently a modem that you are talking to, I wouldn't expect it to matter, but perhaps 'command\r\n' or 'command\n\r' are worth a try.

--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I read and write to /dev/ttyUSB0 in C? LinuxTexan Linux - Newbie 12 11-24-2010 03:03 AM
How to read serial port (/dev/ttyS0) seraph-seph Linux - Hardware 4 11-01-2006 06:55 AM
How to read serial port (/dev/ttyS0) without end of transmission seraph-seph Programming 1 10-22-2006 01:03 AM
read/write access for ttys0 pete_bogg Linux - General 1 08-15-2006 12:31 AM
can write to but not read from ttyS0 philetus Linux - Hardware 9 06-24-2004 05:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration