Send serial ping to trigger transmit from arduino

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;

Is the problem that your PC program waits for data from the Arduino?

Is the problem that your PC program waits for data from the Arduino?

No, I was trying to stream data constantly from the Arduino so that I could just pull a value from the serial port whenever I wanted, but this approach doesn't work. The connection times out and the Arduino stops transmitting. Now I want to just send a signal from the PC to wake the Arduino up, and have it transmit a serial value back.

I think you're worrying about a problem that doesn't exist. Modern computers have an input buffer on serial ports, which stores incoming data that arrives while your program is off doing other things. As long as you read from the port's buffer often enough to keep the buffer from overflowing, you don't have to worry about missing any data. As a result, you also don't have to worry about having to read from the serial port at the exact time that the data is coming in. The OS takes care of that for you and stores the data in a buffer until you're ready for it.

All you need to do is write some arbitrary value to your serial port, to trigger the Arduino to send its data back to you. Once it does that, you'll be able to use your readComm() function to get the Arduino's data from the input buffer. You'll just have to modify readComm() to read one data packet, instead of trying to read from the port forever.

All you need to do is write some arbitrary value to your serial port, to trigger the Arduino to send its data back to you.

Yes I agree with you. But the million dollar question is "What code will allow me to do this?"

All you need to do is write some arbitrary value to your serial port, to trigger the Arduino to send its data back to you.

//small echo server
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  if (Serial.available() > 0) {  //recieved some data
    Serial.print( Serial.read() ); //print it back
  }
}

[edit]Note to self: Read the whole thread before answering ;)[/edit]

Dear Alpha Beta,

Thankyou for the small echo server but what I'm looking for is a way to send data from the C++ program. The Arduino is already working with regards to putting out data after any input has been recieved. I just don't know how to send anything to it from the C++ program over the serial connection.

And I'm trying to avoid the mess of overlapping/non-overlapping IO described on the MSDN page...

I'm just looking for a quick, dirty way to send one little bit of information to the Arduino in C++;

Are you looking for something like this...

DWORD nWritten;

if ( WriteFile( AudioInComm, "X", 1, &nWritten, NULL ) )
{
  if ( nWritten != 1 )
  {
    // Serial port problem.  Call ClearCommError to determine what went wrong.
  }
}
else
{
  // WriteFile failed.  Call GetLastError for the reason.
}

Again, you're worrying about a problem that does not exist. You do not need to concern yourself with overlapped I/O, since you've probably only got one program reading and writing to the serial port at any given time. If you were running several programs (or one program that spawns several threads) that tried to read and write to the serial port at the same time, you'd have to worry about overlapped I/O. But i'm betting you're not doing that.

The way to write bytes to the serial port is exactly the opposite of reading from it. That is, you use the ReadFile() function to Read from the port, and you use the WriteFile() function to Write to the serial port. It's all on the page you linked.

If I get you right, you just want to send some data to the Arduino from your PC to trigger the Arduino to send some data back. Your question is a desktop-based C++ one.

I'm not sure how this would be done in a C++ program, but I expect you'd grab a serial interface library and use its send function in a very similar way that you use Serial.print() on the Arduino. You'd also declare somewhere your COM port that your Arduino is hooked up to.

I've never done this, but a Google search yielded http://cpp.snippets.org/ which contains a serial library which has at least one recommendation. See if you can make heads or tails of it.

I assume you've tested the Arduino using the Serial Monitor or PuTTy and that works fine?