I would like to control my Arduino UNO with my PC using C++.
I can write code for arduino, but do not know how to work with PC USB COM ports in C++.
Using serialmon, I can send commands via the USB to the arduino and read back it's response.
That I can do.
I want to do that not using serialmon, but C++.
Where can I find source for a basic program in C++ (minGW/Eclipse) that will :
Where can I find source for a basic program in C++ (minGW/Eclipse) that will :
big profits
If you want big profits from an application, YOU need to write the application.
Look in the Playground. There is a section on interfacing with the PC, showing how to do that in a variety of languages. C++ is just about the hardest (at least if you are using Windoze). C# is, on the other hand, just about the easiest (at least if you are using Windoze).
Opening a serial port on windows using c++, there is the odd bit missing but its a good start.
int connectport(char * port, unsigned long BAUD)
{
char port_name[20];
sprintf( port_name, "\\\\.\\%s", port );
hCom = CreateFileA( port_name, GENERIC_READ|GENERIC_WRITE,
0, 0, OPEN_EXISTING, 0, 0 );
if( hCom==INVALID_HANDLE_VALUE )
{
printf( "\terror: %s is not available.\n", port );
return 0;
}
DCB dcbSerialParams = {0};
dcbSerialParams.DCBlength=sizeof(dcbSerialParams);
if (!GetCommState(hCom, &dcbSerialParams))
{
printf("Unable to get the state of serial port");
//error getting state
}
dcbSerialParams.BaudRate= BAUD; //= CBR_9600;
dcbSerialParams.ByteSize=8;
dcbSerialParams.StopBits=ONESTOPBIT;
dcbSerialParams.Parity=NOPARITY;
if(!SetCommState(hCom, &dcbSerialParams))
{
printf("Unable to set serial port settings\n");
//error setting serial port state
}
/* DCB optional part ends here */
/* COMTIMEOUTS Optional Part but very usefull especially against ReadHAngs */
COMMTIMEOUTS timeouts= {1};
//previous values
timeouts.ReadIntervalTimeout=1;//=MAXDWORD;//=50;
timeouts.ReadTotalTimeoutConstant=1;//=50;
timeouts.ReadTotalTimeoutMultiplier=1;//=10;
timeouts.WriteTotalTimeoutConstant=10;//=50;
timeouts.WriteTotalTimeoutMultiplier=2;//=10;
if(!SetCommTimeouts(hCom, &timeouts))
{
printf("Error setting Serial Port timeouts property\n");
//error occureed. Inform user
}
return 1;
}
/////////////////////////////////////////////////////////////////////////////////////////////
Reading Writing...
///////////////////////////////////////////////////////////////////////////
unsigned char ReadData(HANDLE hComi)
{
unsigned char Buf[1];
DWORD bytesRead;
unsigned int toRead=1;
if(ReadFile(hComi, Buf, toRead, &bytesRead, NULL) )
{
return Buf[0];
}
return 0;
}
///////////////////////////////////////////////////////////////////////////
bool WriteData(unsigned char *buffer, unsigned int nbChar,HANDLE hComi)
{
DWORD bytesSend;
//Try to write the buffer on the Serial port
if(!WriteFile(hComi, (void *)buffer, nbChar, &bytesSend, 0))
{
//In case it don't work get comm error and return false
//ClearCommError(hCom, &this->errors, &this->status);
return false;
}
else
return true;
}
This code is non working its just bit's pulled from a project i'm working on. Its all based on or the same as stuff i found on the internet. Once you start looking you will recognise some of it.