LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-06-2011, 01:38 PM   #1
R00ts
Member
 
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:

Code:
//----------------------------------------------------------------------------
// 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;

	fclose(G_deviceFile);
	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
shuuhen
Member
 
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: http://www.arduino.cc/playground/Mai...ngWithSoftware
- POSIX serial programming: http://www.easysw.com/~mike/serial/
 
Old 01-06-2011, 03:49 PM   #3
R00ts
Member
 
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
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
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-Howto

Serial Programming Howto
Serial Programming Guide for POSIX OS's

--- 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
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

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

All times are GMT -5. The time now is 10:14 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