I have a program on my Arduino that sends an integer value whenever the arduino recieves any data at all from a PC.
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
// say what you got:
Serial.print(findContext(xVoltage, yVoltage, avgXE, avgYE),DEC);
}
How do I send a piece of arbitrary data to the Arduino down a serial connection from a C++ program to trigger a transmitting event on the Arduino to the PC? I already had it rigged up so that I could receive a constant stream of data, but my program needs to access other functions (and I don't want to multi-thread). This is my current function for setting up the COM port and then receiving data from it.
int initCom()
{
TCHAR *pcComPort = TEXT("COM3");
//Open up the com port
AudioInComm = CreateFile(pcComPort,
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
//error checking
if (AudioInComm == INVALID_HANDLE_VALUE)
{
//printf("CreateFile failed with error %d.\n", GetLastError());
return 33;
}
if(GetCommState(AudioInComm, &dcbConfig))
{
dcbConfig.BaudRate = CBR_9600;
dcbConfig.ByteSize = 8;
dcbConfig.Parity = NOPARITY;
dcbConfig.StopBits = ONESTOPBIT;
dcbConfig.fBinary = TRUE;
dcbConfig.fParity = TRUE;
}
//error checking
else
{
//printf("GetCommState failed with error %d.\n", GetLastError());
return 34;
}
//error checking
if(!SetCommState(AudioInComm, &dcbConfig))
{
//printf ("SetCommState failed with error %d.\n", GetLastError());
return 35;
}
else
{
//_tprintf(TEXT("Serial port %s successfully reconfigured.\n"), pcComPort);
}
//Prepare the operation rules for when the COM port times out
if(GetCommTimeouts(AudioInComm, &commTimeout))
{
commTimeout.ReadIntervalTimeout = 10000; //milliseconds
commTimeout.ReadTotalTimeoutConstant = 10000; //milliseconds
commTimeout.ReadTotalTimeoutMultiplier = 10000; //milliseconds
commTimeout.WriteTotalTimeoutConstant = 10000; //milliseconds
commTimeout.WriteTotalTimeoutMultiplier = 10000; //milliseconds
}
//error checking
else
{
//printf ("GetCommTimeouts failed with error %d.\n", GetLastError());
return 36;
}
//error checking
if(!SetCommTimeouts(AudioInComm, &commTimeout))
{
//printf ("SetCommTimeouts failed with error %d.\n", GetLastError());
return 37;
}
//Error checking for if the mask was created properly
if(!SetCommMask(AudioInComm, EV_RXCHAR))
{
//printf ("SetCommMAsk failed with error %d.\n", GetLastError());
return 38;
}
return 0;
}
int readCom(HANDLE AudioInComm, DWORD dwEventMask, stringbuf sb )
{
//This is the buffer for the incomming data
if(WaitCommEvent(AudioInComm, &dwEventMask, NULL))
{
char szBuf;
unsigned long dwIncommingReadSize; //This will always be 1 byte unless otherwise specified
int data = 4;
//while data is streaming into the port, capture it one byte at a time.
do
{
//for(int x = 0; x < 2000; x++)
//{
if(ReadFile(AudioInComm, &szBuf, 1, &dwIncommingReadSize, NULL) != 0)
{
if(dwIncommingReadSize > 0)
{
//push the incoming data into a buffer
sb.sputn(&szBuf, dwIncommingReadSize);
//convert the data in the buffer to an integer
data = (int)szBuf;
}
}
//error checking
else
{
printf ("ReadFile failed with error %d.\n", GetLastError());
return 555;
}
//}
return data;
} while(dwIncommingReadSize > 0); //as long as data is streaming in from the COM port, this loop will not end.
}
return 777;
}
What can I tweak in my C++ code to allow the serial port to transmit and receive without having to deal with the intricacies of overlapped/non-overlapped IO and all those other crazy settings? This is the only thing that is holding me up, I just need a quick and dirty way to send one bit then receive the data from the Arduino. Can you please help? Example code GREATLY appreciated;