NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io



#include <iostream>
#include <sstream>
#include <iomanip>
#include <thread>
#include <string>
#include <vector>


#pragma once

#define ARDUINO_WAIT_TIME 2000
#define INPUT_DATA_BYTES 35 // Number of bytes for received string "60.00rn"

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

class SerialPort
{
public:
SerialPort(char* portName);
~SerialPort();

int ReadSerialPort(char* buffer, unsigned int buf_size);
bool WriteSerialPort(char* buffer, unsigned int buf_size);
bool isConnected();

private:
HANDLE handletoCOM;
bool connected;
COMSTAT status;
DWORD errors;

};


SerialPort::SerialPort(char* portName) {

errors = 0;
status = { 0 };

connected = false;

// Create & open the COM I/O device
// This returns a handle to the COM device
handletoCOM = CreateFileA(static_cast<LPCSTR>(portName), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

// *** Handle errors ***
// Use win32 API func. to get last error

DWORD errMsg = GetLastError();

if (errMsg == 2)
{
printf("Plug, the device");
}
if (errMsg == 5)
{
printf("Another app using the device");
}
// Configure Serial Port
if (errMsg == 0)
{
// Device control box, a structure that holds the COM port settings

DCB dcbSerialParams = { 0 };

if (!GetCommState(handletoCOM, &dcbSerialParams))
{
printf("Failed to get current serial params.");
}
else {
dcbSerialParams.BaudRate = CBR_9600;
dcbSerialParams.ByteSize = 8;
dcbSerialParams.StopBits = ONE5STOPBITS;
dcbSerialParams.Parity = NOPARITY;
dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;

if (!SetCommState(handletoCOM, &dcbSerialParams))
{
printf("ALERT! could not set serial port params.");
}
else {
connected = true;
PurgeComm(handletoCOM, PURGE_RXCLEAR | PURGE_TXCLEAR);
Sleep(ARDUINO_WAIT_TIME);
}

}
}
}

SerialPort::~SerialPort() {
if (connected == true)
{
connected = false;
CloseHandle(handletoCOM);
}
}

int SerialPort::ReadSerialPort(char* buffer, unsigned int buf_size) {




DWORD bytesREAD;
unsigned int toRead = 0;

ClearCommError(handletoCOM, &errors, &status);

if (status.cbInQue > 0)
{
if (status.cbInQue > buf_size)
{
toRead = buf_size;
}
else {
toRead = status.cbInQue;
}
}

if (ReadFile(handletoCOM, buffer, toRead, &bytesREAD, NULL))
{
return bytesREAD;
}

return 0;
}

bool SerialPort::isConnected() {
return connected;
}

char inputData[INPUT_DATA_BYTES];
double inputValDbl = 0.0;
double oldFreq = 60.0;


char comport[] = "COM6";
char* port = comport;



std::vector<double> acc_x;
std::vector<double> acc_y;
std::vector<double> acc_z;
std::vector<char> LargeBuffer(100);

int main()
{

SerialPort arduino(port);

if (arduino.isConnected())
{
std::cout << "connectted to " << port << std::endl;
}
else {
std::cout << " ****** NOT ****** connectted to " << port << std::endl;
}


while (arduino.isConnected())
{
arduino.ReadSerialPort(inputData, INPUT_DATA_BYTES);

//std::string inputValStr(inputData);
std::stringstream s(inputData);


/*
inputValDbl = std::stod(inputValStr);

std::cout << std::fixed << std::setprecision(3);
std::cout << inputValDbl << 'r';
*/

std::cout << s.str() << 'n';



/*std::this_thread::sleep_for(std::chrono::microseconds(1000));
double acc_x, acc_y, acc_z;
std::string str_acc_x, str_acc_y, str_acc_z;
str_acc_x.resize(6);
str_acc_y.resize(6);
str_acc_z.resize(6);
acc_x = 0; acc_y = 0; acc_z = 0;*/
//if (inputValStr.size() == 35)
//{
//
// size_t rfind = inputValStr.find('r');
// //std::cout << rfind << std::endl;

// size_t nfind = inputValStr.find('n');
//std::cout << nfind << std::endl;
/*
if (rfind == 22 && nfind == 23 )
{
for (int i = 0; i < 6; i++)
{
str_acc_x[i] = inputValStr[i];
}

for (int i = 6 + 2; i < 6 + 2 + 6; i++)
{
str_acc_y[i-(6+2)] = inputValStr[i];
}

for (int i = 6 + 2 + 6 + 2; i < 6 + 2 + 6 + 2 + 6; i++)
{
str_acc_z[i-(6 + 2 + 6 + 2)] = inputValStr[i];
}
acc_x = std::stod(str_acc_x);
acc_y = std::stod(str_acc_y);
acc_z = std::stod(str_acc_z);

std::cout << acc_x << "," << acc_y << "," << acc_z << std::endl;

}
*/

//std::this_thread::sleep_for(std::chrono::microseconds(10));
//}
}

}

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.