Solved!
The CPP for Windows class and header code is here: (Arduino Playground - CPPWindows). I have compiled this with Code::Blocks using MINGW. I also have the Arduino IDE code. I used a rev 3 UNO with v1.0.1 of the IDE.
Because the Arduino code outputs a PWM waveform (variable width pulse) to pin 9 on the Arduino, I used a scope. You can try a resistor in series with an LED (a 1K usually works for a red LED) to ground. It should vary with the value that you send the Arduino.
Here is the Arduino IDE code:
//LEDBrightSerialread.ino
const int ledPin = 9; // the pin that the LED is attached to
byte brightness, digit, decval;
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
if (Serial.available())
{
// read the most recent byte (which will be from 0 to 255):
digit = Serial.read();
decval=digit-48;
if ((decval>=0) && (decval<=9))
{
brightness=brightness*10+decval;
}
else if (digit=13)
{
// set the brightness of the LED:
Serial.println(brightness);
analogWrite(ledPin, brightness);
brightness=0;
}
}
}//End loop()
By the way, there is an ASCII table here: http://www.asciitable.com.
The code reads an ASCII digit (digit = Serial.read();), and then converts it to a decimal value (decval=digit-48;). If the value is between zero and nine, it is added to the end of the brightness value. So when it receives ASCII 1 2 3, the brightness value becomes 123. Receiving an ASCII 13 (carriage return) ends the accumulation and outputs the brightness value to pin 9 as well as to the serial port (to the Windows' Hyper Terminal (HT) program if you have it running and connected to the port.
For the PC side, I am running Windows XP Pro SP3. I used Code::Blocks(CB) 10.05. I had MINGW already installed, but you can simply install a CB version that includes MINGW. Once CB is installed, simply create a console project. I created a folder "CPPWinCodeBlocks" in that project folder and put the SerialClass.cpp and SerialClass.h into it.
In the CB project bring in the two SerialClass files by right-clicking the project name and selecting Add files. Select both files in the Open dialog window. CB will place them in the project structure at the far left. Then modify the main.cpp as follows:
//Main.cpp
#include <iostream>
#include "H:\CB10_05\CPPWinCodeBlocks\serialclass.h"
using namespace std;
void Test()
{
char numb1[4]={49, 50, 57,13}; //"127";
char numb2[4]={50, 53, 53,13}; //"255";
char numb3[3]={ 49, 49,13}; //"11";
char numb4[2]={ 48, 13}; //"0";
char pn[5]="COM4"; //port name
Serial* sp4= new Serial(pn);
BOOL xx;
int numb1size=sizeof(numb1);
int numb2size=sizeof(numb2);
int numb3size=sizeof(numb3);
int numb4size=sizeof(numb4);
xx=sp4->IsConnected();
if (xx)
{
xx=sp4->WriteData(numb1, numb1size);
Sleep(2000);
xx=sp4->WriteData(numb2, numb2size);
Sleep(2000);
xx=sp4->WriteData(numb3, numb3size);
Sleep(2000);
xx=sp4->WriteData(numb4, numb4size);
}
}
int main()
{
cout << "Hello world!" << endl;
Test();
return 0;
}
The code just writes out ASCII digits to com port 4 (COM4) followed by a 13 (carriage return), and then delays for 2 seconds. Four sequences are written in total. The LED should be dark at the start and then go half=bright (127), then full bright (255), and then darker (11), and then completely dark (0).
Keep in mind that on the PC, you can only have one program using the port at a time. You must stop executing your PC program in order to change and upload your Arduino-resident program. If you use Hyper Terminal, you can "hang up" the connection to the port and yet still keep HT running, then reconnect using the icons in the toolbar. You will have to hang-up to run your PC program.
This is pretty simple, but you should, of course, cut main.cpp to a bare minimum to see how it works. Good luck.
If this post belongs in a wiki somewhere, pls let me know, although I really don't know how to do that.