Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- #include <windows.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <chrono>
- #include <vector>
- #include <clocale>
- #include <locale>
- #include <string>
- #include <vector>
- #define CONNECTION_WAIT_TIME 2000
- typedef std::chrono::high_resolution_clock Clock;
- typedef unsigned char byte;
- class SerialReceiver {
- public:
- SerialReceiver() :
- hSerial(NULL),
- connected(false)
- {
- }
- ~SerialReceiver() {
- }
- public:
- std::string w2s(const std::wstring &var)
- {
- static std::locale loc("");
- auto &facet = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(loc);
- return std::wstring_convert<std::remove_reference<decltype(facet)>::type, wchar_t>(&facet).to_bytes(var);
- }
- std::wstring s2w(const std::string &var)
- {
- static std::locale loc("");
- auto &facet = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(loc);
- return std::wstring_convert<std::remove_reference<decltype(facet)>::type, wchar_t>(&facet).from_bytes(var);
- }
- //bool openSerialConnection(LPCWSTR port)
- bool openSerialConnection(std::string port)
- {
- connected = false;
- portName = s2w(port);
- //Try to connect to the given port throuh CreateFile
- hSerial = CreateFile(portName.c_str(),
- GENERIC_READ | GENERIC_WRITE,
- 0,
- NULL,
- OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,
- NULL);
- //Check if the connection was successfull
- if (hSerial == INVALID_HANDLE_VALUE)
- {
- closeSerialConnection();
- //If not success full display an Error
- if (GetLastError() == ERROR_FILE_NOT_FOUND) {
- //Print Error if neccessary
- printf("ERROR: Handle was not attached. Reason: %ls not available.\n", portName.c_str());
- }
- else
- {
- printf("ERROR!!!");
- }
- }
- else
- {
- //If connected we try to set the comm parameters
- DCB dcbSerialParams = { 0 };
- //Try to get the current
- if (!GetCommState(hSerial, &dcbSerialParams))
- {
- //If impossible, show an error
- printf("failed to get current serial parameters!");
- }
- else
- {
- //Define serial connection parameters for the arduino board
- //dcbSerialParams.BaudRate = CBR_115200;
- dcbSerialParams.BaudRate = CBR_115200;
- dcbSerialParams.ByteSize = 8;
- dcbSerialParams.StopBits = ONESTOPBIT;
- dcbSerialParams.Parity = NOPARITY;
- //Setting the DTR to Control_Enable ensures that the Arduino is properly
- //reset upon establishing a connection
- dcbSerialParams.fDtrControl = DTR_CONTROL_ENABLE;
- //Set the parameters and check for their proper application
- if (!SetCommState(hSerial, &dcbSerialParams))
- {
- printf("ALERT: Could not set Serial Port parameters");
- connected = false;
- }
- else
- {
- //If everything went fine we're connected
- connected = true;
- //Flush any remaining characters in the buffers
- PurgeComm(hSerial, PURGE_RXCLEAR | PURGE_TXCLEAR);
- //We wait 2s as the arduino board will be reseting
- Sleep(CONNECTION_WAIT_TIME);
- printf("Serial connection on port %ls established\n", portName.c_str());
- // flush serial pipeline
- ClearCommError(hSerial, &errors, &status);
- if (status.cbInQue > 0)
- {
- int byteAvailable = status.cbInQue;
- DWORD byteflushed = status.cbInQue;
- char *temp = new char[byteAvailable];
- ReadFile(hSerial, temp, byteAvailable, &byteflushed, NULL);
- delete temp;
- }
- return true;
- }
- }
- }
- return false;
- }
- inline u_short ByteSwap(u_short in)
- {
- u_short out;
- char *indata = (char *)∈
- char *outdata = (char *)&out;
- outdata[0] = indata[1];
- outdata[1] = indata[0];
- return out;
- }
- bool closeSerialConnection() {
- Sleep(100);
- connected = false;
- std::cout << "Closing connection..." << std::endl;
- int success = (int)CloseHandle(hSerial);
- if (success == 0)
- return false;
- return true; // close serial connection
- }
- int ReadData(char *buffer, const unsigned int nbChar)
- {
- DWORD bytesRead;
- unsigned int toRead;
- ClearCommError(hSerial, &errors, &status);
- if (status.cbInQue>0)
- {
- if (status.cbInQue >= nbChar)
- {
- toRead = nbChar;
- }
- else // not enough for one package, return
- return 0;
- if (ReadFile(hSerial, buffer, toRead, &bytesRead, NULL))
- {
- return bytesRead;
- }
- }
- return 0;
- }
- void UpdateData(){
- readResult = ReadData(incomingData, dataLength);
- }
- bool isConnected() { return connected; }
- private:
- std::wstring portName;
- HANDLE hSerial = NULL;
- COMSTAT status; //Get various information about the connection
- DWORD errors; //Keep track of last error
- bool connected = false;
- std::vector<char> incomingDataCollection;
- char incomingData[16] = ""; // don't forget to pre-allocate memory
- int dataLength = 16;
- int readResult = 0;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement