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