Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-06-2011, 01:38 PM   #1
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
Writing/reading to a modem via serial device?

I have a CDMA modem connected to an embedded system via USB. The driver for this device creates a device file at /dev/ttyUSB2. I wish to send AT commands to the modem and read the result returned by the modem. Minicom works for doing this, but I need to do this in an application. I wrote the following C++ code to test that this works:

// Simple app for testing sending/receiving AT commands from a port

#include <cstdio>
#include <iostream>
#include <string>

#define G_DEVICE_NAME "/dev/ttyUSB2"

FILE* G_deviceFile = NULL;

void SendCommand(const char* command, int length) {
	fwrite(command, 1, length, G_deviceFile);

char* ReadResult() {
	static char buffer[100];
	fread(buffer, 1, 100, G_deviceFile);
	return buffer;

using namespace std;

int main() {
	string commandOne = "ATZ";
	string commandTwo = "AT^SYSINFO";
	string commandThree = "AT+VROM?";

	G_deviceFile = fopen(G_DEVICE_NAME, "w+");
	if (G_deviceFile == NULL) {
		cout << "ERROR: could not open file: " << G_DEVICE_NAME << endl;
		return 1;

	cout << "SENT: " << commandOne << endl;
	SendCommand(commandOne.c_str(), commandOne.size());
	cout << "RECV: " << ReadResult() << endl;

	cout << "SENT: " << commandTwo << endl;
	SendCommand(commandTwo.c_str(), commandTwo.size());
	cout << "RECV: " << ReadResult() << endl;

	cout << "SENT: " << commandThree << endl;
	SendCommand(commandThree.c_str(), commandThree.size());
	cout << "RECV: " << ReadResult() << endl;

	return 0;
However running this simple application managed to crash the entire system. I'm not sure if the problem is with my application or with the driver for this device, as the manufacturer of the modem is...well, they don't give me a lot of confidence in their product. I can't seem to capture the crash report either (doesn't show up in the syslog).

So I was wondering if I am way off base here with this sample code (i.e., this is not the way you want to write/read AT commands to the modem). Thanks for any input you can give.
Old 01-06-2011, 03:00 PM   #2
Registered: Jun 2004
Distribution: Mac OS X 10.6.6, Gentoo Linux, FreeBSD 6.0
Posts: 127

Rep: Reputation: 20
Sounds like this would be similar to working with Arduino boards or XBee radios. You may want to dig around the Arduino site for examples.

- Arduino examples:
- POSIX serial programming:
Old 01-06-2011, 03:49 PM   #3
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
This looks extremely relevant, thanks for sharing. I've been searching so hard to find something like this. I'll give it a try tomorrow once I have access to the device again.
Old 01-06-2011, 05:02 PM   #4
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
I won't say it can't work, and I don't really know C++, but in this kind of application it is uncommon to use the stream oriented functions for talking to devices. Try using the open()/read()/write()/close() style of IO. Also, since you are talking to a serial port, of sorts, the termios API is probably going to be useful, and it operates on file descriptors, not stream oriented FILE pointers.

For good information and sample code on programming serial interfaces, see


Serial Programming Howto
Serial Programming Guide for POSIX OS's

--- rod.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Problem writing on serial device maus Programming 6 10-14-2009 04:46 PM
Reading/Writing to serial ports (Rs-232). linux_chris Linux - Software 9 12-10-2008 01:05 PM
Reading and Writing to a Serial Port without SU Steef223 Programming 2 06-16-2008 09:16 AM
Facing problems in reading from and writing to serial port. vineel Linux - Hardware 2 02-27-2008 02:18 PM
reading and writing to a serial device (modem) Xanadu Linux - Laptop and Netbook 0 01-04-2005 10:05 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:46 PM.

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