ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:
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:
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.
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?
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.