Advertisement
Guest User

main.cpp

a guest
Feb 18th, 2016
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <cstring>
  7.  
  8. #include "pstools.h"
  9.  
  10. #define BAUD_RATE 9600
  11. using namespace std;
  12.  
  13. class NFSUG2
  14. {
  15.     ProcessTools ps;
  16.     HANDLE hProc;
  17.  
  18.     string exeName = "SPEED2.EXE";
  19.     static const int moneyAddr = 0x461e74;
  20.     static const int speedAddr = 0x3f09e8;
  21.     int baseAddr = 0;
  22.  
  23.     int money = 0;
  24.     float speed = 0.0;
  25.  
  26. public:
  27.     NFSUG2()
  28.     {
  29.         hProc = ps.OpenProcess(exeName.c_str(), PROCESS_ALL_ACCESS);
  30.         baseAddr = ps.GetProcessBaseAddress(ps.GetProcessId(exeName.c_str()));
  31.     }
  32.     void PrintInfo()
  33.     {
  34.         cout << "hProc: " << hex << hProc << endl;
  35.         cout << "baseAddres: 0x" << hex << baseAddr << endl;
  36.     }
  37.     int ReadMoney()
  38.     {
  39.         ReadProcessMemory(hProc, (LPVOID)(baseAddr + moneyAddr), &money, sizeof(int), NULL);
  40.         return money;
  41.     }
  42.     float ReadSpeed()
  43.     {
  44.         ReadProcessMemory(hProc, (LPVOID)(baseAddr + speedAddr), &speed, sizeof(float), NULL);
  45.         return speed;
  46.     }
  47.  
  48. };
  49.  
  50. class SerialPort
  51. {
  52.     DCB dcbControl;
  53.     HANDLE hPortHandle = NULL;
  54.     char lpReadBuffer[256] = {0};
  55.     char lpWriteBuffer[256] = {0};
  56.     DWORD dwBaudRate = 9600;
  57. private:
  58.  
  59. public:
  60.     bool OpenPort(char* lpPortName, DWORD dwAttr)
  61.     {
  62.         hPortHandle = CreateFile(lpPortName, dwAttr, 0, NULL,
  63.                                  OPEN_EXISTING, 0, NULL);
  64.         if(!hPortHandle)
  65.         {
  66.             cerr << "Couldn't open port! " << lpPortName << endl;
  67.             exit(1);
  68.         }
  69.         else
  70.         {
  71.             cout << "Port: " << lpPortName << " opened!\n";
  72.         }
  73.     }
  74.     void SetupDCB()
  75.     {
  76.         if(!hPortHandle)
  77.         {
  78.             cerr << "Port not opened!\n";
  79.             exit(1);
  80.         }
  81.  
  82.         dcbControl.DCBlength = sizeof(dcbControl);
  83.         dcbControl.BaudRate = BAUD_RATE;
  84.         dcbControl.fParity = FALSE;
  85.         dcbControl.Parity = NOPARITY;
  86.         dcbControl.StopBits = ONESTOPBIT;
  87.         dcbControl.ByteSize = 8;
  88.  
  89.         dcbControl.fDtrControl = DTR_CONTROL_DISABLE;
  90.         dcbControl.fRtsControl = RTS_CONTROL_DISABLE;
  91.  
  92.         dcbControl.fOutxCtsFlow = FALSE;
  93.         dcbControl.fOutxDsrFlow = FALSE;
  94.         dcbControl.fDsrSensitivity = FALSE;
  95.         dcbControl.fAbortOnError = FALSE;
  96.         dcbControl.fOutX = FALSE;
  97.         dcbControl.fInX = FALSE;
  98.         dcbControl.fErrorChar = FALSE;
  99.         dcbControl.fNull = FALSE;
  100.  
  101.         SetCommState(hPortHandle, &dcbControl);
  102.     }
  103.     DWORD WriteSerial(char text[256])
  104.     {
  105.         DWORD sent = 0;
  106.         strcpy(lpWriteBuffer, text);
  107.         //cout << "Write: " << text << endl;
  108.         //cout << hPortHandle << endl;
  109.         //cout << strlen(text);
  110.         WriteFile(hPortHandle, text, strlen(text), &sent, 0);
  111.         return sent;
  112.     }
  113. };
  114. int main()
  115. {
  116.     NFSUG2 nfs;
  117.     SerialPort sp;
  118.     sp.OpenPort("COM5", GENERIC_WRITE | GENERIC_READ);
  119.     sp.SetupDCB();
  120.     nfs.PrintInfo();
  121.  
  122.     char temp[256] = {0};
  123.  
  124.     string tempStr;
  125.     int tmpMoney = 0;
  126.     float tmpSpeed = 0;
  127.     float tmpSpeed2 = 0;
  128.     cout << nfs.ReadMoney() << endl;
  129.     cout << nfs.ReadSpeed() << endl;
  130.  
  131.     for(;;Sleep(1))
  132.     {
  133.         //sp.WriteSerial("#");
  134.         tmpSpeed = (nfs.ReadSpeed() * 1.6);
  135.         if(tmpSpeed == tmpSpeed2)
  136.             continue;
  137.         sprintf(temp, "%d", (int)tmpSpeed);
  138.         if(strlen(temp) == 1)
  139.         {
  140.             temp[2] = temp[0];
  141.             temp[0] = ' ';
  142.             temp[1] = ' ';
  143.         }
  144.         else if(strlen(temp) == 2)
  145.         {
  146.             temp[2] = temp[1];
  147.             temp[1] = temp[0];
  148.             temp[0] = ' ';
  149.         }
  150.         else if(strlen(temp) == 3)
  151.         {
  152.  
  153.         }
  154.  
  155.         //tempStr = ss.str();
  156.         //strcpy(temp, tempStr.c_str());
  157.         cout << "BYTES: " << sp.WriteSerial(temp);
  158.         sp.WriteSerial("#");
  159.         cout << " Written: " << temp << endl;
  160.         tmpSpeed2 = tmpSpeed;
  161.  
  162.         memset(temp, 0, 256);
  163.     }
  164.     return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement