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.