C++ Code to create com port using USB


#1

Hi,

I am trying to write some code to create a com port using C++ to communicate with the Fastrack Supreme 10. I am able to connect to the device using the terminal emulator but what i need is to create a class within C++ program that will be run on the PC and communcate with Fastrack modem. I have tried using a the WIN32 API and was unsuccessful.

Has anyone out there with a C++ program that is able to talk to the fastrack supreme can show me the code on how to initiate this communication?

Regards
Jas


#2

As far as the PC is concerned, the Fastrack appears as just another COM port.

Therefore your program can handle it in exactly the same way as any other COM port.


#3

Thanks Anweil for that info. I am having difficulties in creating at com port serial link that works using C++. Do you have a sample code in C++ that shows how to connect to the Fastrack supreme and just send an AT command. Basically emulating an Hyperterminal. I am intending to write and application using the wavecom but am having trouble creating a connection. Any help will be greatly appreciated?

Regards
Jas


#4

You really need to consider this in two separate steps:

1. Sending text to a COM port, and receiving text from a COM port

For this part, what is connected to the COM port is totally irrelevant - it’s purely a matter of sending & receiving text.
Any standard COM port examples will do; eg, see “the usual suspects” here: viewtopic.php?f=37&t=2207&p=8348&hilit=usual+suspects#p8348

2. Using the facilities to send AT commands, and receive the responses

Once you have developed the routines to send & receive text, it’s just a matter of using them to send the AT commands, and receive the responses.
You will need to study the AT commands manual to know what commands to send, and what responses to expect.

Also study this thread, and the linked references: viewtopic.php?f=34&t=1509&p=5576&hilit=api+send+sms#p5576


#5

Hi Anweil,

Thank you very much for your suggestion. That was my intention. I am struggling desparetly to get the connection working. I have written a basic program that opens a port on COM2 and then tries to just send an at command. So far i have been unsuccessful. The writing part is where i am getting stuck at the moment.

I have attached the code here. Can you give me any pointers on where i am going wrong?

#include <windows.h>
#include <stdio.h>
#include <iostream.h>
#include <tchar.h>

int main()
{

bool TestCom;
DWORD ModemStat;
int TestRes;
char TextSt[] = “AT+6122222222;\r”;
TCHAR TestString[] = “AT/r”;

DCB m_dcb; //DCB structure.
HANDLE hComm;
COMMTIMEOUTS commtimeouts; // COMMTIEMOUTS structure (not used yet)
OVERLAPPED m_overlap; //OVERLAPPED structure
LPOVERLAPPED_COMPLETION_ROUTINE Routine = LPOVERLAPPED_COMPLETION_ROUTINE(); // Routine to finalise write statment

cout << “Start of Program” << endl;
Sleep(2000);

hComm = CreateFile(“COM2”, GENERIC_READ | GENERIC_WRITE, 0,0, OPEN_ALWAYS,FILE_FLAG_OVERLAPPED, 0);

TestRes = GetCommModemStatus(hComm,&ModemStat);
if (TestRes==0) cout << “Status Failed” << endl;
else
{
cout << “Status Success” << endl;
cout << "Status is " << ModemStat << endl;
}

Sleep(5000);

m_dcb.BaudRate =9600;
m_dcb.ByteSize = 8;
m_dcb.Parity =0 ;
m_dcb.StopBits =0;
m_dcb.fBinary=TRUE;
m_dcb.fDsrSensitivity=false;
m_dcb.fParity=false;
m_dcb.fOutX=false;
m_dcb.fInX=false;
m_dcb.fNull=false;
m_dcb.fAbortOnError=TRUE;
m_dcb.fOutxCtsFlow=FALSE;
m_dcb.fOutxDsrFlow=false;
m_dcb.fDtrControl=DTR_CONTROL_DISABLE;
m_dcb.fDsrSensitivity=false;
m_dcb.fRtsControl=RTS_CONTROL_DISABLE;
m_dcb.fOutxCtsFlow=false;
m_dcb.fOutxCtsFlow=false;

TestRes = SetCommState(hComm,&m_dcb);
if (TestRes==0) cout << “UnSuccessful Config” << endl;
else cout << “Succesfully configured port” << endl;

cout << TestString << endl;
cout << "Size of TEXT (No of bytes) " << sizeof(TestString) << endl;
cout << *TestString << endl;
cout << &TestString << endl;

TestRes = WriteFileEx(hComm,&TestString,sizeof(TestString),&m_overlap,Routine);
if (TestRes != 0) cout << “The write was sent successfully” << endl;
else cout << "Unsucessful WRITE operation " << endl;
Sleep(5000);

cout<< Routine << endl;

cout << “Closing Port” << endl;

TestCom = CloseHandle(hComm);
if (TestCom) cout << “Port Closed Successfully” << endl;
else cout << “Error in closing port” << endl;
Sleep(5000);

return 0;
};

I get a message stating that the Write message wasn’t sent correctly? I am at a lost as to why this is occuring?

Regards
Jas