LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Can read from /dev/ttyS0, but not write? (https://www.linuxquestions.org/questions/programming-9/can-read-from-dev-ttys0-but-not-write-681050/)

ivanatora 11-04-2008 09:17 AM

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?

theNbomr 11-04-2008 11:07 AM

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.

ivanatora 11-04-2008 03:41 PM

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.

theNbomr 11-04-2008 08:24 PM

Quote:

Originally Posted by ivanatora (Post 3331741)
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.

ivanatora 11-05-2008 02:31 PM

Quote:

Originally Posted by theNbomr (Post 3331955)
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.

theNbomr 11-05-2008 04:00 PM

Perhaps you need to flush the output buffer, &/or append a linefeed &/or carriage return to your output string.
--- rod.

ivanatora 11-06-2008 10:48 AM

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.

theNbomr 11-06-2008 05:20 PM

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.


All times are GMT -5. The time now is 07:02 PM.