Data Transfer between Arduino Nano and PC

Hi all!
I'm newbie. Sorry for the inconvenience!
My project:

  • Hardware diagram:
  • Software:
    PC: I want to write a program by Visual C++ 2008. I use SerialPort class in MSDN. "Send request to arduino and read sensor value from arduino"
....................
void readAnalog()
{
//request: send "1" to arduino
........... (help me)
if(serial available)
{
//read analog value from arduino
............ (help me)
}
}
void readFrequency()
{
//request: send "2" to arduino
........... (help me)
if(serial available)
{
//read frequency value from arduino
............ (help me)
}
}
.........................

Arduino Nano: read request from PC and send sensor value to PC

#include <FreqMeasure.h>
void setup() {
  Serial.begin(57600);
  FreqMeasure.begin();
}
double sum=0;
int count=0;
int temp=0;
int request=0;
double frequency=0;
int voltage=0;

void loop() {
  if(Serial.available()>0)
  {
    request = Serial.read();
    if(request == 2)
    {
      //read frequency from sensor
      readFreq();
     //send "frequency" value to PC
      .............(help me)
    }
    else
    {
      if(request == 1)
      {
        voltage = analogRead(A0);
        //send "anaglog" value to PC
        ............. (help me)
      }
    }
  }
}
double readFreq()
{
  if (FreqMeasure.available()) 
      {
        // average several reading together
        sum = sum + FreqMeasure.read();
        count = count + 1;
        if (count > 30) {
          frequency = F_CPU / (sum / count);
          sum = 0;
          count = 0;   
        }  
      }
      else
      {
        temp = temp + 1;
        if(temp >3)
        {
          frequency = 0;
          temp = 0;
        }
      }
      return frequency;
}
  • If my doing wrong, people fix to help me.
  • And if my doing right, please help me complete code in "..........(hepme)"

Thank in advance!

int request=0;

Only used in loop(). Should not be global.

    request = Serial.read();
    if(request == 2)

Make sure that the VC app sends the data in binary, not as text.

      readFreq();

Why doesn't a function called readSomething() return a value?

If it does, why are you throwing it away?

Most of your global variables should not be.

    else
    {
      if(request == 1)

No. This should be:

    else if(request == 1)

If Serial.read() gets data from the serial port, it should be obvious that Serial.print(), Serial.println(), and Serial.write() send data to the serial port. You need to look at the documentation for those functions to determine which is appropriate for your needs.