Guest User

Untitled

a guest
Apr 15th, 2016
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.50 KB | None | 0 0
  1. // The one and only true one.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6. #include <tchar.h>
  7. #include <iostream>
  8. #include "SharedFileOut.h"
  9. #include "SerialClass.h"
  10. #include <chrono>
  11. #include <thread>
  12.  
  13. using namespace std;
  14.  
  15. char inData[8] = "";
  16.  
  17.  
  18. template <typename T, unsigned S>
  19. inline unsigned arraysize(const T(&v)[S])
  20. {
  21.     return S;
  22. }
  23.  
  24.  
  25. struct SMElement
  26. {
  27.     HANDLE hMapFile;
  28.     unsigned char* mapFileBuffer;
  29. };
  30.  
  31. SMElement m_graphics;
  32. SMElement m_physics;
  33. SMElement m_static;
  34.  
  35. void initPhysics()
  36. {
  37.     TCHAR szName[] = TEXT("Local\\acpmf_physics");
  38.     m_physics.hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SPageFilePhysics), szName);
  39.     if (!m_physics.hMapFile)
  40.     {
  41.         MessageBoxA(GetActiveWindow(), "CreateFileMapping failed", "ACS", MB_OK);
  42.     }
  43.     m_physics.mapFileBuffer = (unsigned char*)MapViewOfFile(m_physics.hMapFile, FILE_MAP_READ, 0, 0, sizeof(SPageFilePhysics));
  44.     if (!m_physics.mapFileBuffer)
  45.     {
  46.         MessageBoxA(GetActiveWindow(), "MapViewOfFile failed", "ACS", MB_OK);
  47.     }
  48. }
  49.  
  50. void initGraphics()
  51. {
  52.     TCHAR szName[] = TEXT("Local\\acpmf_graphics");
  53.     m_graphics.hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SPageFileGraphic), szName);
  54.     if (!m_graphics.hMapFile)
  55.     {
  56.         MessageBoxA(GetActiveWindow(), "CreateFileMapping failed", "ACS", MB_OK);
  57.     }
  58.     m_graphics.mapFileBuffer = (unsigned char*)MapViewOfFile(m_graphics.hMapFile, FILE_MAP_READ, 0, 0, sizeof(SPageFileGraphic));
  59.     if (!m_graphics.mapFileBuffer)
  60.     {
  61.         MessageBoxA(GetActiveWindow(), "MapViewOfFile failed", "ACS", MB_OK);
  62.     }
  63. }
  64.  
  65. void initStatic()
  66. {
  67.     TCHAR szName[] = TEXT("Local\\acpmf_static");
  68.     m_static.hMapFile = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(SPageFileStatic), szName);
  69.     if (!m_static.hMapFile)
  70.     {
  71.         MessageBoxA(GetActiveWindow(), "CreateFileMapping failed", "ACS", MB_OK);
  72.     }
  73.     m_static.mapFileBuffer = (unsigned char*)MapViewOfFile(m_static.hMapFile, FILE_MAP_READ, 0, 0, sizeof(SPageFileStatic));
  74.     if (!m_static.mapFileBuffer)
  75.     {
  76.         MessageBoxA(GetActiveWindow(), "MapViewOfFile failed", "ACS", MB_OK);
  77.     }
  78. }
  79.  
  80. void dismiss(SMElement element)
  81. {
  82.     UnmapViewOfFile(element.mapFileBuffer);
  83.     CloseHandle(element.hMapFile);
  84. }
  85.  
  86.  
  87. /*-------------------------------------------------*/
  88. /*-------------------MAIN PART---------------------*/
  89. /*-------------------------------------------------*/
  90.  
  91. int _tmain(int argc, _TCHAR* argv[])
  92. {
  93.     initPhysics();
  94.     initGraphics();
  95.     initStatic();
  96.  
  97.     printf("Welcome to the serial test app!\n\n");
  98.  
  99.     Serial* SP = new Serial("COM3");    /* CHANGE COM PORT TO THE ONE THE ARDUINO IS CONNECTED TO*/
  100.  
  101.     if (SP->IsConnected())              /*Check connection to port*/
  102.         printf("We're connected");
  103.     else return 0;
  104.  
  105.     SPageFilePhysics* pf = (SPageFilePhysics*)m_physics.mapFileBuffer;  /*Pointer used to access physics data*/
  106.  
  107.     char Data[8]="";
  108.     char stop[2] = { char(13) };
  109.  
  110.  
  111.     while (true)
  112.     {
  113.  
  114.         snprintf(Data, sizeof(Data)-1, "%3.3f", (int)(pf->speedKmh)*1.0);   /*Float to char conversion, to send it through the serial port*/
  115.  
  116.         SP->WriteData(Data, sizeof(Data));                  /*Write data on Serial Port*/
  117.         SP->WriteData(stop, sizeof(stop));
  118.  
  119.         printf("Speed KmH : %s\t", Data);
  120.        
  121.        
  122.         int readResult = SP->ReadData(inData, sizeof(inData));
  123.          printf("\t Bytes read:  %i\t",readResult);
  124.          printf("\t");
  125.          for (int i = 0; i < sizeof(inData); i++)
  126.          {
  127.              printf("%i,", inData[i]);
  128.              inData[i] = 0;
  129.          }
  130.          printf("\n");
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment