Im following this video were it is really good explained how to write an class to read serial communication. I use visualMicro to write code on to the arduino. In the serial window it is working good, the expectetd output is shown.
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (Serial.availableForWrite())
{
Serial.write("60.00\r\n");
}
delay(500);
}
However, when i use the C++ class i get an incorrect output:
Can anyone see a mistake in the code?
Header for class:
#pragma once
#define INPUT_DATA_BYTES 7
#define ARDUINO_WAIT_TIME 1500
/*#define SERIAL_START_MESSAGE "<"
#define SERIAL_STOP_MESSAGE ">"*/
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
class SerialPort
{
public:
SerialPort(char* COM_PORT);
~SerialPort();
int ReadSerialPort(char* Buffer, unsigned int BufferSize);
bool IsConnected();
private:
HANDLE SerialPortHandler;
bool ComPortConnected;
COMSTAT status;
DWORD errors;
DWORD errorMsg;
};
SourceCode for class
#include "SerialPort.h"
SerialPort::SerialPort(char* COM_PORT)
{
errors = NULL;
status = { 0 };
ComPortConnected = false;
SerialPortHandler = CreateFileA(static_cast<LPCSTR>(COM_PORT), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
errorMsg = GetLastError();
if (errorMsg == 2)
{
std::cout << "ERROR: COM PORT NOT FOUND!" << std::endl;
}
if (errorMsg == 5)
{
std::cout << "ERROR: COM PORT ACCES DENIED!" << std::endl;
}
//If Port found procced with connection
else if(errorMsg == 0)
{
DCB SerialPortSettings = { 0 };
if (!GetCommState(SerialPortHandler, &SerialPortSettings))
{
std::cout << "ERROR: FAILED TO GET CURRENT SERIAL PARAMETERS!" << std::endl;
}
//Set ComPort Settings
else
{
SerialPortSettings.BaudRate = CBR_115200;
SerialPortSettings.fDtrControl = DTR_CONTROL_DISABLE;
SerialPortSettings.ByteSize = 8;
SerialPortSettings.Parity = NOPARITY;
SerialPortSettings.StopBits = ONESTOPBIT;
//Check for failed COM Port settings
if (!SetCommState(SerialPortHandler, &SerialPortSettings))
{
std::cout << "ERROR: FAILED TO SET COM PORT PARAMETERS" << std::endl;
}
else
{
ComPortConnected = true;
PurgeComm(SerialPortHandler, PURGE_RXCLEAR | PURGE_TXCLEAR);
Sleep(ARDUINO_WAIT_TIME);
}
}
}
}
SerialPort::~SerialPort()
{
if (ComPortConnected)
{
ComPortConnected = false;
CloseHandle(SerialPortHandler);
std::cout << "COM PORT CLOSED" << std::endl;
}
}
int SerialPort::ReadSerialPort(char* Buffer, unsigned int BufferSize)
{
DWORD BytesRead;
unsigned int BytesToRead = 0;
ClearCommError(SerialPortHandler, &errors, &status);
if (status.cbInQue > 0)
{
if (status.cbInQue > BufferSize)
{
BytesToRead = BufferSize;
}
else
{
BytesToRead = status.cbInQue;
}
}
if (ReadFile(SerialPortHandler, Buffer, BytesToRead, &BytesRead, NULL))
{
return BytesRead;
}
return 0;
}
bool SerialPort::IsConnected()
{
if (ComPortConnected)
{
return true;
}
else
{
return false;
}
}
Main:
#include <iostream>
#include <iomanip>
#include <string>
#include <Windows.h>
#include <thread>
#include "SerialPort.h"
int main()
{
char InputData[INPUT_DATA_BYTES];
double InputValueDouble = 0.0;
char Port[] = "COM5";
char* COM_PORT = Port;
SerialPort ArduinoPort(COM_PORT);
if (ArduinoPort.IsConnected())
{
std::cout << "CONNECTED TO ARDUINO ON " << Port << std::endl;
}
while (ArduinoPort.IsConnected())
{
ArduinoPort.ReadSerialPort(InputData, INPUT_DATA_BYTES);
std::string InputDataString(InputData);
std::cout << InputDataString << '\n';
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
return 0;
}
I already tried with different board (UNO/MEGA), different Baudrates (9600/115200), checked the device manager(Baudrate was correct to....
Any help is appreciated