Hello, I have some code here intended to communicate with the motor without Cosmos. It uses a class called CSerial: "
http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503/CSerial--A-C-Class-for-Serial-Communications.htm"
However, it fails to open the port. It communicates fine with com1, the communications port. Can anyone tell me what's going on?
///////////////////////////////////////////////////////////////////////////////////////////////
// VXM Controller.cpp
///////////////////////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <iostream>
#include <string>
#include "Serial.h"
using namespace std;
int* MOTOR_NUM;
int main()
{
cout << "Enter the com port number: " << endl;
int portInt;
cin >> portInt;
MOTOR_NUM = &portInt;
CSerial serialPort;
CSerial* P_serialPort = &serialPort;
if (P_serialPort->Open(*MOTOR_NUM, 9600))
{
cout << "Port opened successfully" << endl;
string* command;
string userInput = "";
do
{
//Get the command in the form of a string
cout << "What would you like to do?" << endl
<< "Enable on-line mode (E or F)" << endl
<< "Verify status (V)" << endl
<< "Move x units (I1Mx)" << endl
<< "Quit C++ dialog (q)" << endl;
cin >> userInput;
command = &userInput;
//Change the command (string) into a const char*
const char* message = (*command).c_str();
//Send the command that is in the form of a const char*
cout << "Sending message: " << message << endl;
int bytesRead = P_serialPort -> SendData(message, (*command).length());
cout << "There were " << bytesRead << " bytes read to the serial port." << endl;
//Check that all of it sent
assert(bytesRead == strlen(message));
char* lpBuffer = new char[500];
int nBytesRead = P_serialPort->ReadData(lpBuffer, 500);
delete[]lpBuffer;
cout << "Data returned: " << *lpBuffer << endl;
} while (*command != "q");
}
else
{
cout << "Failed to open port!" << endl;
return 0;
}
cout << "Ending program. " << endl;
P_serialPort->Close();
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////