Serial Communication with C++

Hi guys, I normally don't do this because I know it's a bit of a burden, but I've been scouring the internet/debugging for answers for two days and I have yet to come up with anything.

Basically, I'm trying to get a C++ program (using CSerial found here: Serial library for C++ - CodeProject ) to ultimately issue commands (such as setting the digital outs to low and high) based on the program, through the program I'm writing. Right now I'm just testing it though. Basically, both the computer and the Arduino have no problem sending/receiving info from each other (at least it seems) but the problem arises when I try to make the Arduino interpret what I send. Here's an example of the code I've been playing around with (just trying to make the LED turn off). Hope you can help and thanks in advance.

Arduino Code:
int LED;
byte inByte;

void setup()
{
pinMode(LED, OUTPUT);
Serial.begin(9600);
digitalWrite(LED, HIGH);
}

void loop()
{
inByte = Serial.read();
if (inByte == '7'){
digitalWrite(LED, LOW);
}
}

C++ code:
//#define STRICT
#include <tchar.h>
#include
#include <windows.h>
#include
#include "Serial.h"

using namespace std;

int ShowError (LONG lError, LPCTSTR lptszMessage)
{
// Generate a message text
TCHAR tszMessage[256];
wsprintf(tszMessage,_T("%s\n(error code %d)"), lptszMessage, lError);

// Display message-box and return with an error-code
::MessageBox(0,tszMessage,_T("Hello world"), MB_ICONSTOP|MB_OK);
return 1;
}

int main()
{

CSerial serial;
LONG lLastError = ERROR_SUCCESS;

// Attempt to open the serial port (COM1)
lLastError = serial.Open(_T("COM3"),0,0,false);
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to open COM-port"));

// Setup the serial port (9600,N81) using hardware handshaking
lLastError = serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port setting"));

// Setup handshaking
lLastError = serial.SetupHandshaking(CSerial::EHandshakeHardware);
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to set COM-port handshaking"));

// The serial port is now ready and we can send/receive data. If
// the following call blocks, then the other side doesn't support
// hardware handshaking.
Sleep (15000);
while (true){
lLastError = serial.Write("7");
if (lLastError != ERROR_SUCCESS)
return ::ShowError(serial.GetLastError(), _T("Unable to send data"));
cout << "-";
}
// Close the port again

serial.Close();
return 0;
}

The goal of those two codes is to turn off the LED after 15 seconds, but that doesn't happen. Also, if I write a listener and tell the Arduino to send back what it read in as well, it sends back a 7, follwed by a few lines, then another 7 etc etc...

Sorry for long text, and thanks again :smiley:

inByte = Serial.read();

You should not do this until you have checked there is data to receive with the serial available call.

If there is no serial data to read, Serial.read will return -1. Since this is not '7', the LED will not be turned off.

Still, it is a good idea to verify that there is data to read, first, using Serial.available().

This looks like a problem, to me:

int LED;
.
.
.
pinMode(LED, OUTPUT);

What value does LED have? It looks to me like you just set the RX pin (pin 0) to be OUTPUT. I'm pretty sure pin 0 should be INPUT.

Then,

digitalWrite(LED, HIGH);

you tried to turn on the LED on pin 0. Is there an LED attached to pin 0?

After reading serial data, you try to turn off the LED on pin 0, too.

I suspect that if you assign a value to LED you'll have better luck.

Sorry about those little errors. I wrote that code on the fly since the actual code I have is very long since I'm defining a bunch of variables (one for each i/o plus others I will be using). However, after checking the Serial.available() I still come up with nothing, however, it did end up helping me find the solution. For whatever reason, no data will be sent to the Arduino unless the Arduino sends something to the computer and it reads it. Not really sure why this is the case, I imagine it's something weird with CSerial. I was planning on having this happen anyway so it's not a big deal. Thank you all very much. You're all my heroes :smiley:

No data is sent because you have requested handshaking. This means that both sides have to send something, in order to know that the other is there.