Read from serial port


 
Thread Tools Search this Thread
Top Forums Programming Read from serial port
# 1  
Old 06-10-2013
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 Terminal..." back.

Thanks for some help!

Code:
//
// Skriv och las fran com-port
//
// Av: David Miller
//
// Andringshistorik:
// 
// Datum	Sign	Kommentar
// ----------------------------------------------------
// 12-05-21	DM	Grundversion

#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 */
#include <time.h>

main()
{
	int fd;
	struct	termios options;
	char	b[255];  	// Input buffer
	char	*bufptr;      // Current char in buffer
	int		nbytes;       // Number of bytes read
	int		tries;        // Number of tries so far
	int		x;
	
	// open the port
    fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY );
    fcntl(fd, F_SETFL, 0);

	
	
	// get the current options
	tcgetattr(fd, &options);
	tcsetattr(fd, TCSAFLUSH, &options); // activate the set options
	cfsetispeed(&options, B9600); // 9600 baud input
	cfsetospeed(&options, B9600); // 9600 baud output
	options.c_cflag |= (CLOCAL | CREAD); // enable receiver, local mode
	options.c_lflag &= ~( ICANON | ECHO | ECHOE | ISIG | IEXTEN ); // ensure raw input
	options.c_oflag &= ~OPOST; // ensure raw output

	// set the options
	tcsetattr(fd, TCSANOW, &options);

	printf("\nOpen: %d", fd);


	x = write(fd, "ATI\t", 4);

	printf("\nWrite: %d", x);

	sleep(1);

	x = 0;
	
	x = read(fd, b, 30);

	printf("\nRead: %s", b);
	close(fd);

}

# 2  
Old 06-10-2013
I think you need to look at this maybe:

Send SMS using AT commands
# 3  
Old 06-10-2013
Thanks for the quick answer!

I think there is a solution, but I still want to know why my code doesent work...
# 4  
Old 06-10-2013
I cannot know exactly, but I think you may be violating the comm protocol rules the modem uses. You may be getting an error code back. Do you have a programmer's guide to serial communication for the modem? -- I do not know where to get one.
# 5  
Old 06-10-2013
The modem is AT-compatible.

.flygt.de/1114471.pdf

It's working great with a terminal emulator from the same computer...
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

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

3. Programming

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... (7 Replies)
Discussion started by: yashwantkumar
7 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. 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

6. Windows & DOS: Issues & Discussions

Telnet to a serial port

Hello all. I have an old computer with M$-DOS 7. I want to use it like a dumb terminal, with telnet. I need to connect it to my desktop These are my questions: How can i configure the serial port on dos? How can i telnet form dos to the serial port? How can i set up a telnet server on the... (12 Replies)
Discussion started by: mghis
12 Replies

7. Programming

How to tell if a string to serial port has been sent

I have a need to determine when a string has been completely sent via a serial port from a standard 'C' application. The code is as follows: SerialPort_Send = open (pPortString, O_WRONLY | O_NOCTTY | O_NONBLOCK); write (SerialPort_Send, pCommandString, strlen (pCommandString)); ... (2 Replies)
Discussion started by: ExDes
2 Replies

8. Solaris

Serial port issue

Hi, I am working with Sun Solaris 9 Sparc,Sun-Blade-100.In my application,I need to monitor the UPS using Serial port. When I am using JRE 1.4 in my application,I am able to monitor it but when I am upgrading the JRE version from 1.4 to JRE1.6. I am not able to monitor the UPS.It is showing the... (1 Reply)
Discussion started by: smartgupta
1 Replies

9. Linux

urgent......Serial port

I think my serial port(on board) is not working ...... because when I am running same code on two machines its working good....but when my system comes into picture ....it doesnt show any output comming from serial port.......... can anybody tell me how to configure my serial port.........or to... (16 Replies)
Discussion started by: arunchaudhary19
16 Replies

10. UNIX for Dummies Questions & Answers

Serial port communication

We're running SCO Unix Openserver 5.05 and I'm having trouble with serial communication between the com2 serial port and a handheld device. Downloading data from Unix to the handheld works perfectly, but the other way around creates a major problem. I don't know whether it's a buffer overflow or... (1 Reply)
Discussion started by: Aretha
1 Replies
Login or Register to Ask a Question