Hi,
I am trying to send AT commands to a USB modem using VC++. I found the code below online and have tried using it. I do not get any errors but it doesn't appear to be working.
I used Putty to set AT+CMGF= 0. However after I run this code, when I check the value of AT+CMGF it is still 0, so although no error is being thrown up, it doesn't seem to be sending correctly.
Any help would be great thanks.
#include <iostream>
#include <Windows.h>
#include <string.h>
using std::string;
int main() {
std::string data = "AT+CMGF=1";
DCB dcb;
DWORD byteswritten;
HANDLE hPort = CreateFile(
"COM3", //"\\\\.\\COM10"
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_READ| FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
0,
NULL
);
DWORD dwerror = GetLastError();
if (GetCommState(hPort,&dcb) == 0) {
return false; }
dcb.BaudRate = CBR_9600; //9600 Baud
dcb.ByteSize = 8; //8 data bits
dcb.Parity = NOPARITY; //no parity
dcb.StopBits = ONESTOPBIT; //1 stop
if (!SetCommState(hPort,&dcb))
return false;
bool retVal = WriteFile(hPort,&data,200,&byteswritten,NULL);
dwerror = GetLastError();
CloseHandle(hPort); //close the handle
return retVal;
}