From ab779072ee585176bc11b4a116cf4a35e0e5962a Mon Sep 17 00:00:00 2001 From: Anonim17PL <76495160+Anonim17PL@users.noreply.github.com> Date: Sat, 13 Feb 2021 13:18:48 +0100 Subject: [PATCH] Remove cmd window --- A17BBI.def | 10 ++++ Serial.cpp | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Serial.h | 41 +++++++++++++ dllmain.cpp | 125 +++++++++++++++++++++++++++++++++++++++ framework.h | 5 ++ pch.cpp | 5 ++ pch.h | 13 ++++ 7 files changed, 366 insertions(+) create mode 100644 A17BBI.def create mode 100644 Serial.cpp create mode 100644 Serial.h create mode 100644 dllmain.cpp create mode 100644 framework.h create mode 100644 pch.cpp create mode 100644 pch.h diff --git a/A17BBI.def b/A17BBI.def new file mode 100644 index 0000000..5d2794a --- /dev/null +++ b/A17BBI.def @@ -0,0 +1,10 @@ +LIBRARY A17BBI.dll + + +EXPORTS +PluginStart +AccessVariable +AccessTrigger +AccessSystemVariable +AccessStringVariable +PluginFinalize diff --git a/Serial.cpp b/Serial.cpp new file mode 100644 index 0000000..a8f9a7f --- /dev/null +++ b/Serial.cpp @@ -0,0 +1,167 @@ +// Serial.cpp + +#include "pch.h" +#include "Serial.h" + +CSerial::CSerial() +{ + + memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) ); + memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) ); + m_hIDComDev = NULL; + m_bOpened = FALSE; + +} + +CSerial::~CSerial() +{ + + Close(); + +} + +BOOL CSerial::Open( int nPort, int nBaud ) +{ + + if( m_bOpened ) return( TRUE ); + + char szPort[15]; + char szComParams[50]; + DCB dcb; + + wsprintf( szPort, "COM%d", nPort ); + m_hIDComDev = CreateFile( szPort, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL ); + if( m_hIDComDev == NULL ) return( FALSE ); + + memset( &m_OverlappedRead, 0, sizeof( OVERLAPPED ) ); + memset( &m_OverlappedWrite, 0, sizeof( OVERLAPPED ) ); + + COMMTIMEOUTS CommTimeOuts; + CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF; + CommTimeOuts.ReadTotalTimeoutMultiplier = 0; + CommTimeOuts.ReadTotalTimeoutConstant = 0; + CommTimeOuts.WriteTotalTimeoutMultiplier = 50; + CommTimeOuts.WriteTotalTimeoutConstant = 50; + SetCommTimeouts( m_hIDComDev, &CommTimeOuts ); + + wsprintf( szComParams, "COM%d:%d,n,8,1", nPort, nBaud ); + + m_OverlappedRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); + m_OverlappedWrite.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); + + dcb.DCBlength = sizeof( DCB ); + GetCommState( m_hIDComDev, &dcb ); + dcb.BaudRate = nBaud; + dcb.ByteSize = 8; + unsigned char ucSet; + ucSet = (unsigned char) ( ( FC_RTSCTS & FC_DTRDSR ) != 0 ); + ucSet = (unsigned char) ( ( FC_RTSCTS & FC_RTSCTS ) != 0 ); + ucSet = (unsigned char) ( ( FC_RTSCTS & FC_XONXOFF ) != 0 ); + if( !SetCommState( m_hIDComDev, &dcb ) || + !SetupComm( m_hIDComDev, 10000, 10000 ) || + m_OverlappedRead.hEvent == NULL || + m_OverlappedWrite.hEvent == NULL ){ + DWORD dwError = GetLastError(); + if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent ); + if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent ); + CloseHandle( m_hIDComDev ); + return( FALSE ); + } + + m_bOpened = TRUE; + + return( m_bOpened ); + +} + +BOOL CSerial::Close( void ) +{ + + if( !m_bOpened || m_hIDComDev == NULL ) return( TRUE ); + + if( m_OverlappedRead.hEvent != NULL ) CloseHandle( m_OverlappedRead.hEvent ); + if( m_OverlappedWrite.hEvent != NULL ) CloseHandle( m_OverlappedWrite.hEvent ); + CloseHandle( m_hIDComDev ); + m_bOpened = FALSE; + m_hIDComDev = NULL; + + return( TRUE ); + +} + +BOOL CSerial::WriteCommByte( unsigned char ucByte ) +{ + BOOL bWriteStat; + DWORD dwBytesWritten; + + bWriteStat = WriteFile( m_hIDComDev, (LPSTR) &ucByte, 1, &dwBytesWritten, &m_OverlappedWrite ); + if( !bWriteStat && ( GetLastError() == ERROR_IO_PENDING ) ){ + if( WaitForSingleObject( m_OverlappedWrite.hEvent, 1000 ) ) dwBytesWritten = 0; + else{ + GetOverlappedResult( m_hIDComDev, &m_OverlappedWrite, &dwBytesWritten, FALSE ); + m_OverlappedWrite.Offset += dwBytesWritten; + } + } + + return( TRUE ); + +} + +int CSerial::SendData( const char *buffer, int size ) +{ + + if( !m_bOpened || m_hIDComDev == NULL ) return( 0 ); + + DWORD dwBytesWritten = 0; + int i; + for( i=0; i +#include "Serial.h" +#include +#include +#include +#include +#include + +CSerial serial; + +using namespace std; + + + +// Omnibus2.cpp : Defines the exported functions for the DLL application. +// + +extern "C" __declspec(dllexport)void __stdcall PluginStart(void* aOwner); +extern "C" __declspec(dllexport)void __stdcall AccessVariable(unsigned short varindex, float* value, bool* write); +extern "C" __declspec(dllexport)void __stdcall AccessTrigger(unsigned short triggerindex, bool* active); +extern "C" __declspec(dllexport)void __stdcall AccessSystemVariable(unsigned short varindex, float* value, bool* write); +extern "C" __declspec(dllexport)void __stdcall AccessStringVariable(unsigned short varindex, char* value, bool* write); +extern "C" __declspec(dllexport)void __stdcall PluginFinalize(); + + +void __stdcall PluginStart(void* aOwner) +{ + //AllocConsole(); + //freopen("CONOUT$", "w", stdout); + + if (serial.Open(4, 115200)) + MessageBox(0, "Port opened successfully", "OMSI Plugin", MB_OK); + else + MessageBox(0, "Failed to open port!", "OMSI Plugin", MB_OK); +} + + +void __stdcall AccessVariable(unsigned short varindex, float* value, bool* write) +{ + + char szMessage[64]; + sprintf(szMessage, "f%u,%f\n", varindex, *value); + serial.SendData(szMessage, strlen(szMessage)); + + + char* lpBuffer = new char[20]; + int nBytesRead = serial.ReadData(lpBuffer, 20); + stringstream ss; + ss << string(lpBuffer); + string output; + getline(ss, output); + delete[]lpBuffer; + int indeksnowejlinii = output.find("\n"); + int indeksprecinka = output.find(","); + string strf = output.substr(0, 1); + string str1 = output.substr(1, indeksprecinka - 1); + string str2 = output.substr(indeksprecinka + 1, indeksnowejlinii - 1); + + if (strf == "w") { + //*write = TRUE; + varindex = stoi(str1); + *write = TRUE; + bool vOut = stoi(str2) != 0; + *value = vOut; + char szMessage[64]; + //sprintf(szMessage, "%d,%f\n", varindex, *value); + //std::cout << szMessage << std::endl; + } + + +} + + +void __stdcall AccessTrigger(unsigned short triggerindex, bool* active) +{ + char* lpBuffer = new char[20]; + int nBytesRead = serial.ReadData(lpBuffer, 20); + stringstream ss; + ss << string(lpBuffer); + string output; + getline (ss, output); + delete[]lpBuffer; + int indeksnowejlinii = output.find("\n"); + int indeksprecinka = output.find(","); + string strf = output.substr(0, 1); + string str1 = output.substr(1, indeksprecinka-1); + string str2 = output.substr(indeksprecinka + 1, indeksnowejlinii-1); + + if (strf == "t") { + triggerindex = stoi(str1); + bool vOut = stoi(str2) != 0; + *active = vOut; + char szMessage[64]; + //sprintf(szMessage, "%d,%d\n", triggerindex, *active); + //std::cout << szMessage << std::endl; + } + +} + +void __stdcall AccessSystemVariable(unsigned short varindex, float* value, bool* write) +{ + char szMessage[64]; + sprintf(szMessage, "S%u,%f\n", varindex, *value); + serial.SendData(szMessage, strlen(szMessage)); + +} + +void __stdcall AccessStringVariable(unsigned short varindex, char* value, bool* write) +{ + + TCHAR szMsg[] = TEXT("Text als Platzhalter"); + wsprintf(szMsg, "%S", value); + char szMessage[64]; + snprintf(szMessage, sizeof szMessage, "s%u,%s\n", varindex, szMsg); + serial.SendData(szMessage, strlen(szMessage)); +} + +void __stdcall PluginFinalize() +{ + //Place your cleanup code here + //int msgbox = MessageBox(0,L"Stop",L"Message",MB_OK ); +} diff --git a/framework.h b/framework.h new file mode 100644 index 0000000..b44bf25 --- /dev/null +++ b/framework.h @@ -0,0 +1,5 @@ +#pragma once + +#define WIN32_LEAN_AND_MEAN // Wyklucz rzadko używane rzeczy z nagłówków systemu Windows +// Pliki nagłówkowe systemu Windows +#include diff --git a/pch.cpp b/pch.cpp new file mode 100644 index 0000000..0e5c2fd --- /dev/null +++ b/pch.cpp @@ -0,0 +1,5 @@ +// pch.cpp: plik źródłowy odpowiadający wstępnie skompilowanemu nagłówkowi + +#include "pch.h" + +// W przypadku korzystania ze wstępnie skompilowanych nagłówków ten plik źródłowy jest niezbędny, aby kompilacja powiodła się. diff --git a/pch.h b/pch.h new file mode 100644 index 0000000..b424e6f --- /dev/null +++ b/pch.h @@ -0,0 +1,13 @@ +// pch.h: wstępnie skompilowany plik nagłówka. +// Wymienione poniżej pliki są kompilowane tylko raz, co poprawia wydajność kompilacji dla przyszłych kompilacji. +// Ma to także wpływ na wydajność funkcji IntelliSense, w tym uzupełnianie kodu i wiele funkcji przeglądania kodu. +// Jednak WSZYSTKIE wymienione tutaj pliki będą ponownie kompilowane, jeśli którykolwiek z nich zostanie zaktualizowany między kompilacjami. +// Nie dodawaj tutaj plików, które będziesz często aktualizować (obniża to korzystny wpływ na wydajność). + +#ifndef PCH_H +#define PCH_H + +// w tym miejscu dodaj nagłówki, które mają być wstępnie kompilowane +#include "framework.h" + +#endif //PCH_H