Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The one and only true one.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <windows.h>
- #include <tchar.h>
- #include <iostream>
- #include "SharedFileOut.h"
- #include "SerialClass.h"
- #include <chrono>
- #include <thread>
- using namespace std;
- char inData[8] = "";
- template <typename T, unsigned S>
- inline unsigned arraysize(const T(&v)[S])
- {
- return S;
- }
- struct SMElement
- {
- HANDLE hMapFile;
- unsigned char* mapFileBuffer;
- };
- SMElement m_graphics;
- SMElement m_physics;
- SMElement m_static;
- void initPhysics()
- {
- TCHAR szName[] = TEXT("Local\\acpmf_physics");
- m_physics.hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SPageFilePhysics), szName);
- if (!m_physics.hMapFile)
- {
- MessageBoxA(GetActiveWindow(), "CreateFileMapping failed", "ACS", MB_OK);
- }
- m_physics.mapFileBuffer = (unsigned char*)MapViewOfFile(m_physics.hMapFile, FILE_MAP_READ, 0, 0, sizeof(SPageFilePhysics));
- if (!m_physics.mapFileBuffer)
- {
- MessageBoxA(GetActiveWindow(), "MapViewOfFile failed", "ACS", MB_OK);
- }
- }
- void initGraphics()
- {
- TCHAR szName[] = TEXT("Local\\acpmf_graphics");
- m_graphics.hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SPageFileGraphic), szName);
- if (!m_graphics.hMapFile)
- {
- MessageBoxA(GetActiveWindow(), "CreateFileMapping failed", "ACS", MB_OK);
- }
- m_graphics.mapFileBuffer = (unsigned char*)MapViewOfFile(m_graphics.hMapFile, FILE_MAP_READ, 0, 0, sizeof(SPageFileGraphic));
- if (!m_graphics.mapFileBuffer)
- {
- MessageBoxA(GetActiveWindow(), "MapViewOfFile failed", "ACS", MB_OK);
- }
- }
- void initStatic()
- {
- TCHAR szName[] = TEXT("Local\\acpmf_static");
- m_static.hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SPageFileStatic), szName);
- if (!m_static.hMapFile)
- {
- MessageBoxA(GetActiveWindow(), "CreateFileMapping failed", "ACS", MB_OK);
- }
- m_static.mapFileBuffer = (unsigned char*)MapViewOfFile(m_static.hMapFile, FILE_MAP_READ, 0, 0, sizeof(SPageFileStatic));
- if (!m_static.mapFileBuffer)
- {
- MessageBoxA(GetActiveWindow(), "MapViewOfFile failed", "ACS", MB_OK);
- }
- }
- void dismiss(SMElement element)
- {
- UnmapViewOfFile(element.mapFileBuffer);
- CloseHandle(element.hMapFile);
- }
- /*-------------------------------------------------*/
- /*-------------------MAIN PART---------------------*/
- /*-------------------------------------------------*/
- int _tmain(int argc, _TCHAR* argv[])
- {
- initPhysics();
- initGraphics();
- initStatic();
- printf("Welcome to the serial test app!\n\n");
- Serial* SP = new Serial("COM3"); /* CHANGE COM PORT TO THE ONE THE ARDUINO IS CONNECTED TO*/
- if (SP->IsConnected()) /*Check connection to port*/
- printf("We're connected");
- else return 0;
- SPageFilePhysics* pf = (SPageFilePhysics*)m_physics.mapFileBuffer; /*Pointer used to access physics data*/
- char Data[8]="";
- char stop[2] = { char(13) };
- while (true)
- {
- snprintf(Data, sizeof(Data)-1, "%3.3f", (int)(pf->speedKmh)*1.0); /*Float to char conversion, to send it through the serial port*/
- SP->WriteData(Data, sizeof(Data)); /*Write data on Serial Port*/
- SP->WriteData(stop, sizeof(stop));
- printf("Speed KmH : %s\t", Data);
- int readResult = SP->ReadData(inData, sizeof(inData));
- printf("\t Bytes read: %i\t",readResult);
- printf("\t");
- for (int i = 0; i < sizeof(inData); i++)
- {
- printf("%i,", inData[i]);
- inData[i] = 0;
- }
- printf("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment