I'm just getting started with arduino and for my first project I'm trying to use opencv to do facetracking, and move a motor when certain conditions are met. I seem to be stuck on serial write on the visual studio side.
when I use the serial monitor to send eg. "999" it works just fine, but using the C++ code I found online to write to the port doesn't seem to work. I know it connects because the motor does move after I open the com port, but the arduino receives small negative numbers seemingly at random (ie. only occasionally when I do serial.write)
any idea what the problem could be? Been stuck on this for a few days now, clearly I'm missing something..
arduino code:
#include <AccelStepper.h>
AccelStepper motor(AccelStepper::DRIVER, 3,4);
void setup() {
Serial.begin(9600);
motor.setMaxSpeed(4000);
motor.setAcceleration(2500);
}
unsigned int integerValue=0; // Max value is 65535
char incomingByte;
void loop() {
if (Serial.available() > 0) { // something came across serial
integerValue = 0; // throw away previous integerValue
while(1) { // force into a loop until 'n' is received
incomingByte = Serial.read();
if (incomingByte == -1) continue; // if no characters are in the buffer read() returns -1
else if (incomingByte < 48 || incomingByte > 57){
break; // if not numeric exit the while(1), we're done receiving
}
integerValue *= 10; // shift left 1 decimal place
// convert ASCII to integer, add, and shift left 1 decimal place
integerValue = ((incomingByte - 48) + integerValue);
}
if(integerValue > 0){
Serial.println(integerValue); // Do something with the value
motor.move(integerValue);
}
}
motor.run();
}
Serial library
/** Serial.cpp
*
* A very simple serial port control class that does NOT require MFC/AFX.
*
* @author Hans de Ruiter
*
* @version 0.1 -- 28 October 2008
*/
#include <iostream>
using namespace std;
#include "Serial.h"
Serial::Serial(tstring &commPortName, int bitRate)
{
commHandle = CreateFile(commPortName.c_str(), GENERIC_READ|GENERIC_WRITE,0,NULL, OPEN_EXISTING,
0, NULL);
if(commHandle == INVALID_HANDLE_VALUE)
{
throw("ERROR: Could not open com port");
}
else
{
// set timeouts
COMMTIMEOUTS cto = { MAXDWORD, 0, 0, 0, 0};
DCB dcb;
if(!SetCommTimeouts(commHandle,&cto))
{
Serial::~Serial();
throw("ERROR: Could not set com port time-outs");
}
// set DCB
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = bitRate;
dcb.fBinary = 1;
dcb.fOutxCtsFlow = 0;
dcb.fOutxDsrFlow = 0;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = 0;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fOutX = 0;
dcb.fInX = 0;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
if(!SetCommState(commHandle,&dcb))
{
Serial::~Serial();
throw("ERROR: Could not set com port parameters");
}
}
}
Serial::~Serial()
{
CloseHandle(commHandle);
}
int Serial::write(const char *buffer)
{
DWORD numWritten;
WriteFile(commHandle, buffer, strlen(buffer), &numWritten, NULL);
return numWritten;
}
int Serial::write(const char *buffer, int buffLen)
{
DWORD numWritten;
WriteFile(commHandle, buffer, buffLen, &numWritten, NULL);
return numWritten;
}
int Serial::read(char *buffer, int buffLen, bool nullTerminate)
{
DWORD numRead;
if(nullTerminate)
{
--buffLen;
}
BOOL ret = ReadFile(commHandle, buffer, buffLen, &numRead, NULL);
if(!ret)
{
return 0;
}
if(nullTerminate)
{
buffer[numRead] = '\0';
}
return numRead;
}
#define FLUSH_BUFFSIZE 10
void Serial::flush()
{
char buffer[FLUSH_BUFFSIZE];
int numBytes = read(buffer, FLUSH_BUFFSIZE, false);
while(numBytes != 0)
{
numBytes = read(buffer, FLUSH_BUFFSIZE, false);
}
}
Main
// SerialTest.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <windows.h>
#include "Serial.h"
#include "stdafx.h"
using namespace std;
#define RX_BUFFSIZE 20
void printUsage(_TCHAR progName[]);
int _tmain(int argc, _TCHAR* argv[])
{
try
{
cout << "Opening com port"<< endl;
tstring commPortName(TEXT("COM3"), 9600);
Serial serial(commPortName);
cout << "Port opened" << endl;
while (1){
Sleep(2000);
serial.flush();
cout << "writing something to the serial port" << endl;
char hi[] = "9999!";
int bytesWritten = serial.write(hi);
cout << bytesWritten << " bytes were written to the serial port" << endl;
if (bytesWritten != sizeof(hi) - 1)
{
cout << "Writing to the serial port timed out" << endl;
}
}
}catch(const char *msg)
{
cout << msg << endl;
}
cout << "press any key and enter to quit" << endl;
char temp;
cin >> temp;
return 0;
}
void printUsage(_TCHAR progName[])
{
#if defined(UNICODE)
wcout << progName << " <comm port>" << endl
<< "e.g., " << progName << " COM1" << endl;
#else
cout << progName << " <comm port>" << endl
<< "e.g., " << progName << " COM1" << endl;
#endif
}