LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Writing/reading to a modem via serial device? (http://www.linuxquestions.org/questions/programming-9/writing-reading-to-a-modem-via-serial-device-854672/)

R00ts 01-06-2011 01:38 PM

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. :doh: 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.

shuuhen 01-06-2011 03:00 PM

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/

R00ts 01-06-2011 03:49 PM

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.

theNbomr 01-06-2011 05:02 PM

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.


All times are GMT -5. The time now is 08:31 AM.