From e66efbbe8df2dd4e7de0a1e9bd129cf92e00f92f Mon Sep 17 00:00:00 2001 From: vikle012 Date: Wed, 11 Sep 2019 13:57:14 +0200 Subject: Initializes repo. --- Kod/display/ClientServerApp/Client/Client.cpp | 151 ++++++ Kod/display/ClientServerApp/Client/Client.vcproj | 188 ++++++++ Kod/display/ClientServerApp/Client/Client.vcxproj | 165 +++++++ Kod/display/ClientServerApp/ClientServerApp.sln | 53 +++ Kod/display/ClientServerApp/ClientServerApp.suo | Bin 0 -> 25088 bytes Kod/display/ClientServerApp/Common/ComConduit.cpp | 151 ++++++ Kod/display/ClientServerApp/Common/ComConduit.h | 59 +++ Kod/display/ClientServerApp/Common/Common.vcproj | 203 ++++++++ Kod/display/ClientServerApp/Common/Common.vcxproj | 139 ++++++ Kod/display/ClientServerApp/Common/Conduit.cpp | 529 +++++++++++++++++++++ Kod/display/ClientServerApp/Common/Conduit.h | 145 ++++++ Kod/display/ClientServerApp/Common/ReadABuffer.cpp | 36 ++ Kod/display/ClientServerApp/Common/ReadABuffer.h | 20 + .../ClientServerApp/Common/WriteABuffer.cpp | 67 +++ Kod/display/ClientServerApp/Common/WriteABuffer.h | 22 + Kod/display/ClientServerApp/Common/ipclink.h | 80 ++++ Kod/display/ClientServerApp/Release/Client.exe | Bin 0 -> 10240 bytes Kod/display/ClientServerApp/Release/Server.exe | Bin 0 -> 13312 bytes .../ClientServerApp/Release/matlabclient.mexw32 | Bin 0 -> 12800 bytes .../ClientServerApp/Release/matlabclient.mexw64 | Bin 0 -> 14848 bytes .../ClientServerApp/Release/startServer.bat | 15 + Kod/display/ClientServerApp/Server/Server.cpp | 418 ++++++++++++++++ Kod/display/ClientServerApp/Server/Server.vcproj | 189 ++++++++ Kod/display/ClientServerApp/Server/Server.vcxproj | 167 +++++++ .../ClientServerApp/matlabClient/matlabClient.cpp | 400 ++++++++++++++++ .../matlabClient/matlabClient.vcproj | 178 +++++++ .../matlabClient/matlabClient.vcxproj | 158 ++++++ .../ClientServerApp/matlabClient/stdafx.cpp | 8 + Kod/display/ClientServerApp/matlabClient/stdafx.h | 15 + .../ClientServerApp/matlabClient/targetver.h | 13 + Kod/display/ClientServerApp/matlabMake.bat | 16 + Kod/display/DisplaySpec/edip320-8e.pdf | Bin 0 -> 1183861 bytes Kod/display/display_exempel.m | 161 +++++++ 33 files changed, 3746 insertions(+) create mode 100644 Kod/display/ClientServerApp/Client/Client.cpp create mode 100644 Kod/display/ClientServerApp/Client/Client.vcproj create mode 100644 Kod/display/ClientServerApp/Client/Client.vcxproj create mode 100644 Kod/display/ClientServerApp/ClientServerApp.sln create mode 100644 Kod/display/ClientServerApp/ClientServerApp.suo create mode 100644 Kod/display/ClientServerApp/Common/ComConduit.cpp create mode 100644 Kod/display/ClientServerApp/Common/ComConduit.h create mode 100644 Kod/display/ClientServerApp/Common/Common.vcproj create mode 100644 Kod/display/ClientServerApp/Common/Common.vcxproj create mode 100644 Kod/display/ClientServerApp/Common/Conduit.cpp create mode 100644 Kod/display/ClientServerApp/Common/Conduit.h create mode 100644 Kod/display/ClientServerApp/Common/ReadABuffer.cpp create mode 100644 Kod/display/ClientServerApp/Common/ReadABuffer.h create mode 100644 Kod/display/ClientServerApp/Common/WriteABuffer.cpp create mode 100644 Kod/display/ClientServerApp/Common/WriteABuffer.h create mode 100644 Kod/display/ClientServerApp/Common/ipclink.h create mode 100644 Kod/display/ClientServerApp/Release/Client.exe create mode 100644 Kod/display/ClientServerApp/Release/Server.exe create mode 100644 Kod/display/ClientServerApp/Release/matlabclient.mexw32 create mode 100644 Kod/display/ClientServerApp/Release/matlabclient.mexw64 create mode 100644 Kod/display/ClientServerApp/Release/startServer.bat create mode 100644 Kod/display/ClientServerApp/Server/Server.cpp create mode 100644 Kod/display/ClientServerApp/Server/Server.vcproj create mode 100644 Kod/display/ClientServerApp/Server/Server.vcxproj create mode 100644 Kod/display/ClientServerApp/matlabClient/matlabClient.cpp create mode 100644 Kod/display/ClientServerApp/matlabClient/matlabClient.vcproj create mode 100644 Kod/display/ClientServerApp/matlabClient/matlabClient.vcxproj create mode 100644 Kod/display/ClientServerApp/matlabClient/stdafx.cpp create mode 100644 Kod/display/ClientServerApp/matlabClient/stdafx.h create mode 100644 Kod/display/ClientServerApp/matlabClient/targetver.h create mode 100644 Kod/display/ClientServerApp/matlabMake.bat create mode 100644 Kod/display/DisplaySpec/edip320-8e.pdf create mode 100644 Kod/display/display_exempel.m (limited to 'Kod/display') diff --git a/Kod/display/ClientServerApp/Client/Client.cpp b/Kod/display/ClientServerApp/Client/Client.cpp new file mode 100644 index 0000000..efd08fe --- /dev/null +++ b/Kod/display/ClientServerApp/Client/Client.cpp @@ -0,0 +1,151 @@ +//============================================================================ +// Name : Client.cpp +// Author : Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 +// Modified : Emil Larsson, lime@isy.liu.se, 2012-01-13 +// +// Description : The client requests data to be sent by the server. +// +//============================================================================ + +#include +#include +#include "ComConduit.h" + +// +// Show the command-line usage +// +void Usage(char* argv[]) { + + printf("\nUsage: %s \n\n",argv[0]); + printf(" -S= Send message, ex -S=\"hello world\".\n"); + printf(" -X Shutdown server.\n"); + printf("\n\n"); +} + + + + + +// +// Module main function +// +int main(int argc, char* argv[]) { + + // Conduit object + ComConduit Link; + // Key + KeyType key[KEY_LEN] = COM_KEY; + // Work variables + wchar_t data[COMSTR_LEN]; + bool doShutdown; + FlagType ret; + int i,j; + char c,*ptr; + WaitResult wr; + + // Initialize data + data[0] = '\0'; + doShutdown = false; + + // + // Parse the command-line. + // + for (i=1; i= COMSTR_LEN) { + fprintf(stderr,"\nToo long message.\n"); + return EXIT_FAIL; + } + data[j] = '\0'; + break; + + case 'X': + case 'x': + // Shutdown + doShutdown = true; + break; + } + } + + // Check valid args + if(!doShutdown && data[0] == '\0') { + Usage(argv); + return EXIT_FAIL; + } + + // + // Start client + // + if(!Link.StartClient(key)) { + fprintf(stderr,"StartClient() failed.\n"); + return EXIT_FAIL; + } + + ret = EXIT_OK; + if(doShutdown) { + // Tell server to shutdown + if(!Link.CauseShutdown()) { + fprintf(stderr,"\nCauseShutdown() failed.\n"); + ret = EXIT_FAIL; + } + } + else { + do { + // Check if server is ready + wr = Link.WaitForEvent(COM_READY,0); + if(wr != WAIT_OK) { + fprintf(stderr,"Server is not ready.\n"); + ret = EXIT_FAIL; + break; + } + + // Set data in shm + if(!Link.SetData(data)) { + fprintf(stderr,"SetData() failed.\n"); + ret = EXIT_FAIL; + break; + } + + // Request that the server send the data + if(!Link.CauseEvent(COM_REQUEST)) { + fprintf(stderr,"CauseEvent() failed.\n"); + ret = EXIT_FAIL; + break; + } + } while(false); + } + + // + // Shutdown + // + if(!Link.Shutdown()) { + fprintf(stderr,"Shutdown() failed.\n"); + ret = EXIT_FAIL; + } + if(ret == EXIT_OK) + fprintf(stdout, "\nClean exit.\n"); + + return ret; +} + diff --git a/Kod/display/ClientServerApp/Client/Client.vcproj b/Kod/display/ClientServerApp/Client/Client.vcproj new file mode 100644 index 0000000..dce2a22 --- /dev/null +++ b/Kod/display/ClientServerApp/Client/Client.vcproj @@ -0,0 +1,188 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Kod/display/ClientServerApp/Client/Client.vcxproj b/Kod/display/ClientServerApp/Client/Client.vcxproj new file mode 100644 index 0000000..7847220 --- /dev/null +++ b/Kod/display/ClientServerApp/Client/Client.vcxproj @@ -0,0 +1,165 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {3941DB40-83AA-41FD-A225-29B47EEFD704} + Client + Win32Proj + + + + Application + Unicode + true + + + Application + Unicode + true + + + Application + Unicode + + + Application + Unicode + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + true + true + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + false + false + + + + Disabled + ..\Common;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + + Level3 + EditAndContinue + + + true + Console + MachineX86 + + + + + Disabled + ..\Common;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + EnableFastChecks + MultiThreadedDebugDLL + + + Level3 + ProgramDatabase + + + true + Console + + + + + MaxSpeed + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + + Level3 + ProgramDatabase + "..\Common";%(AdditionalIncludeDirectories) + + + true + Console + true + true + MachineX86 + + + + + MaxSpeed + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + + Level3 + ProgramDatabase + "..\Common";%(AdditionalIncludeDirectories) + + + true + Console + true + true + + + + + + + + {8c1e78f5-0564-498a-958d-37a997bbfe49} + false + + + + + + \ No newline at end of file diff --git a/Kod/display/ClientServerApp/ClientServerApp.sln b/Kod/display/ClientServerApp/ClientServerApp.sln new file mode 100644 index 0000000..f23f502 --- /dev/null +++ b/Kod/display/ClientServerApp/ClientServerApp.sln @@ -0,0 +1,53 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Server", "Server\Server.vcxproj", "{7B05F65E-9D5F-49CE-9339-959759F4E98C}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Client", "Client\Client.vcxproj", "{3941DB40-83AA-41FD-A225-29B47EEFD704}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Common", "Common\Common.vcxproj", "{8C1E78F5-0564-498A-958D-37A997BBFE49}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "matlabClient", "matlabClient\matlabClient.vcxproj", "{3094F281-B0A8-4B46-B1CC-A4B8B5CC596A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Debug|x64 = Debug|x64 + Release|Win32 = Release|Win32 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7B05F65E-9D5F-49CE-9339-959759F4E98C}.Debug|Win32.ActiveCfg = Debug|Win32 + {7B05F65E-9D5F-49CE-9339-959759F4E98C}.Debug|Win32.Build.0 = Debug|Win32 + {7B05F65E-9D5F-49CE-9339-959759F4E98C}.Debug|x64.ActiveCfg = Debug|Win32 + {7B05F65E-9D5F-49CE-9339-959759F4E98C}.Release|Win32.ActiveCfg = Release|Win32 + {7B05F65E-9D5F-49CE-9339-959759F4E98C}.Release|Win32.Build.0 = Release|Win32 + {7B05F65E-9D5F-49CE-9339-959759F4E98C}.Release|x64.ActiveCfg = Release|x64 + {7B05F65E-9D5F-49CE-9339-959759F4E98C}.Release|x64.Build.0 = Release|x64 + {3941DB40-83AA-41FD-A225-29B47EEFD704}.Debug|Win32.ActiveCfg = Debug|Win32 + {3941DB40-83AA-41FD-A225-29B47EEFD704}.Debug|Win32.Build.0 = Debug|Win32 + {3941DB40-83AA-41FD-A225-29B47EEFD704}.Debug|x64.ActiveCfg = Debug|x64 + {3941DB40-83AA-41FD-A225-29B47EEFD704}.Debug|x64.Build.0 = Debug|x64 + {3941DB40-83AA-41FD-A225-29B47EEFD704}.Release|Win32.ActiveCfg = Release|Win32 + {3941DB40-83AA-41FD-A225-29B47EEFD704}.Release|Win32.Build.0 = Release|Win32 + {3941DB40-83AA-41FD-A225-29B47EEFD704}.Release|x64.ActiveCfg = Release|x64 + {3941DB40-83AA-41FD-A225-29B47EEFD704}.Release|x64.Build.0 = Release|x64 + {8C1E78F5-0564-498A-958D-37A997BBFE49}.Debug|Win32.ActiveCfg = Debug|Win32 + {8C1E78F5-0564-498A-958D-37A997BBFE49}.Debug|Win32.Build.0 = Debug|Win32 + {8C1E78F5-0564-498A-958D-37A997BBFE49}.Debug|x64.ActiveCfg = Debug|Win32 + {8C1E78F5-0564-498A-958D-37A997BBFE49}.Release|Win32.ActiveCfg = Release|Win32 + {8C1E78F5-0564-498A-958D-37A997BBFE49}.Release|Win32.Build.0 = Release|Win32 + {8C1E78F5-0564-498A-958D-37A997BBFE49}.Release|x64.ActiveCfg = Release|x64 + {8C1E78F5-0564-498A-958D-37A997BBFE49}.Release|x64.Build.0 = Release|x64 + {3094F281-B0A8-4B46-B1CC-A4B8B5CC596A}.Debug|Win32.ActiveCfg = Debug|Win32 + {3094F281-B0A8-4B46-B1CC-A4B8B5CC596A}.Debug|Win32.Build.0 = Debug|Win32 + {3094F281-B0A8-4B46-B1CC-A4B8B5CC596A}.Debug|x64.ActiveCfg = Debug|Win32 + {3094F281-B0A8-4B46-B1CC-A4B8B5CC596A}.Release|Win32.ActiveCfg = Release|Win32 + {3094F281-B0A8-4B46-B1CC-A4B8B5CC596A}.Release|Win32.Build.0 = Release|Win32 + {3094F281-B0A8-4B46-B1CC-A4B8B5CC596A}.Release|x64.ActiveCfg = Release|x64 + {3094F281-B0A8-4B46-B1CC-A4B8B5CC596A}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Kod/display/ClientServerApp/ClientServerApp.suo b/Kod/display/ClientServerApp/ClientServerApp.suo new file mode 100644 index 0000000..3de3b49 Binary files /dev/null and b/Kod/display/ClientServerApp/ClientServerApp.suo differ diff --git a/Kod/display/ClientServerApp/Common/ComConduit.cpp b/Kod/display/ClientServerApp/Common/ComConduit.cpp new file mode 100644 index 0000000..1bb952f --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ComConduit.cpp @@ -0,0 +1,151 @@ +/* + * ComConduit.cpp + * + * Definition of the ComConduit class + * + * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 + * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 + */ + +#include "ComConduit.h" +#include +//#include "mex.h" + +// Constructor +ComConduit::ComConduit() { + + ptrData = NULL; +} + +// Destructor +ComConduit::~ComConduit() { + + // Issue shutdown function + Shutdown(); +} + +// Detach and remove shared memory segment +bool ComConduit::Shutdown() { + + if(Conduit::Shutdown()) { + ptrData = NULL; + return true; + } + else + return false; +} + +// Server initialization +bool ComConduit::StartServer(KeyType* key) { + + if(!Conduit::StartServer(key)) + return false; + + ptrData = (COMdata*)ptrShm; + ptrData->Common.Flag = NULL_FLAG; + ptrData->Common.DoShutdown = false; + ptrData->string[0] = '\0'; + + return true; +} + +// Client initialization +bool ComConduit::StartClient(KeyType* key) { + + if(!Conduit::StartClient(key)) + return false; + + ptrData = (COMdata*)ptrShm; + + return true; +} + +// Get shared data size +int ComConduit::GetDataSize() const { + + return sizeof(COMdata); +} + +// Get pointer to common shared data +CommonData* ComConduit::GetCommonData() const { + + if(ptrData == NULL) + return(NULL); + else + return(&ptrData->Common); +} + +// Get number of events +int ComConduit::GetNumEvents() const { + + return COM_NEVENTS; +} + +// Set the data +bool ComConduit::SetData(const wchar_t* data) { + + int ii; + + if(ptrData == NULL) + return false; + + // Request lock + if(!RequestLock()) + return false; + + // Copy string +// wcscpy_s(ptrData->string,COMSTR_LEN, data); + + /* Copy the wchar_t-string manual */ + for (ii=0; iistring[ii] = data[ii]; + // printf("\nptrData->string[%d]: %d \n", ii, ptrData->string[ii]); + } + /* Stop with a "null" char */ + ptrData->string[ii] = '\0'; + + // Release lock + if(!ReleaseLock()) + return false; + + return true; +} + +// Get the data +bool ComConduit::GetData(wchar_t* data) { + + int ii; + + if(ptrData == NULL) + return false; + + // Request lock + if(!RequestLock()) + return false; + + // Copy string +// wcscpy_s(data,COMSTR_LEN,ptrData->string); +// strcpy((char*) data, ptrData->string); + + //printf("\tComConduit:GetData: %d\n",ptrData->string[1]); + + /* Copy the wchar_t-string manual */ + for (ii=0; iistring[1]+EXTRA_LEN; ii++) + { + data[ii] = ptrData->string[ii]; + //printf("\nGetData:data[%d]: %d \n", ii, data[ii]); + } + /* Stop with a "null" char */ + data[ii] = (wchar_t) '\0'; + //printf("\nGetData:data[%d]: %d \n", ii, data[ii]); + + // Release lock + if(!ReleaseLock()) + return false; + + return true; +} + + + diff --git a/Kod/display/ClientServerApp/Common/ComConduit.h b/Kod/display/ClientServerApp/Common/ComConduit.h new file mode 100644 index 0000000..3948355 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ComConduit.h @@ -0,0 +1,59 @@ +/* + * ComConduit.h + * + * Declaration of the ComConduit class + * + * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 + * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 + */ + +#ifndef COMCONDUIT_H_ +#define COMCONDUIT_H_ + +// Include conduit base class +#include "Conduit.h" +// Include ipc interface +#include "ipclink.h" + +#include +// +// The ComConduit class +// +class ComConduit : public Conduit { + +public: + + // Construct & Destruct + ComConduit(); + virtual ~ComConduit(); + + // Server initialization + bool StartServer(KeyType* key); + // Client initialization + bool StartClient(KeyType* key); + // Detach and remove shared memory segment + bool Shutdown(); + + // Get shared data size + int GetDataSize() const; + // Get number of events + int GetNumEvents() const; + // Get pointer to common shared data + CommonData* GetCommonData() const; + + // Write data + bool SetData(const wchar_t* data); + + // Data read + bool GetData(wchar_t* data); + +private: + + // Pointer to shared data + COMdata* ptrData; + +}; + + + +#endif /* COMCONDUIT_H_ */ diff --git a/Kod/display/ClientServerApp/Common/Common.vcproj b/Kod/display/ClientServerApp/Common/Common.vcproj new file mode 100644 index 0000000..b5be92d --- /dev/null +++ b/Kod/display/ClientServerApp/Common/Common.vcproj @@ -0,0 +1,203 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Kod/display/ClientServerApp/Common/Common.vcxproj b/Kod/display/ClientServerApp/Common/Common.vcxproj new file mode 100644 index 0000000..be80a98 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/Common.vcxproj @@ -0,0 +1,139 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {8C1E78F5-0564-498A-958D-37A997BBFE49} + Common + + + + StaticLibrary + MultiByte + true + + + StaticLibrary + MultiByte + true + + + StaticLibrary + NotSet + + + StaticLibrary + NotSet + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + + + + Disabled + %(AdditionalIncludeDirectories) + true + EnableFastChecks + MultiThreadedDebugDLL + Level3 + EditAndContinue + + + + + Disabled + %(AdditionalIncludeDirectories) + EnableFastChecks + MultiThreadedDebugDLL + Level3 + ProgramDatabase + + + + + MaxSpeed + true + MultiThreadedDLL + true + Level3 + ProgramDatabase + + + true + true + true + MachineX86 + + + + + MaxSpeed + true + MultiThreadedDLL + true + Level3 + ProgramDatabase + + + true + true + true + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Common/Conduit.cpp b/Kod/display/ClientServerApp/Common/Conduit.cpp new file mode 100644 index 0000000..1d827f4 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/Conduit.cpp @@ -0,0 +1,529 @@ +/* + * Conduit.cpp + * + * Definition of the Conduit class + * + * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 + * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 + */ + +#include +#include "Conduit.h" +#include + +// Constructor +Conduit::Conduit() { + + ptrShm = NULL; + isServer = false; + iNumSems = 0; + + hMapObject = NULL; + for(int i=0; i MAX_NUM_SEM) + return false; + + + // WIN32 IPC: Shared memory based on file mapping. + + char strKey[KEY_LEN+2]; + SECURITY_ATTRIBUTES attr; + + // Fill struct with security attributes + attr.nLength = sizeof(SECURITY_ATTRIBUTES); + attr.lpSecurityDescriptor = NULL; + attr.bInheritHandle = FALSE; + + // Convert integer array 'key' to char array 'strKey' + for(i=0; i 0))); +} + +// Wait for an event (reserve associated semaphore) +WaitResult Conduit::WaitForEvent(int index) { + + bool ret,flag; + + if(!ValidIndex(index)) + return WAIT_FAIL; + + // Perform wait + ret = Wait(index + FIRST_EVENT_SEM); + + if(!ret || !GetShutdownFlag(flag)) + return WAIT_FAIL; + if(flag) + return WAIT_CLOSE; + + return WAIT_OK; +} + +// Wait for an event with timeout +WaitResult Conduit::WaitForEvent(int index, int timeout) { + + bool ret,flag; + + if(!ValidIndex(index)) + return WAIT_FAIL; + + // Perform wait + ret = Wait(index + FIRST_EVENT_SEM,timeout); + + if(!ret || !GetShutdownFlag(flag)) + return WAIT_FAIL; + if(flag) + return WAIT_CLOSE; + + return WAIT_OK; +} + +// Cause an event (release associated sempahore) +bool Conduit::CauseEvent(int index) { + + if(!ValidIndex(index)) + return false; + else + return Signal(index + FIRST_EVENT_SEM); +} + +// Cause shutdown event +bool Conduit::CauseShutdown() { + + int i; + + // Indicate shutdown + if(!SetShutdownFlag(true)) + return false; + + // Release all semaphores but the mutex + for(i=1; iFlag; + else + ret = false; + + // Release lock + if(!ReleaseLock()) + return false; + + return ret; +} + +// Set state flag +bool Conduit::SetFlag(FlagType flag) { + + bool ret = true; + CommonData *ptr; + + // Request lock + if(!RequestLock()) + return false; + + if((ptr = GetCommonData()) != NULL) + ptr->Flag = flag; + else + ret = false; + + // Release lock + if(!ReleaseLock()) + return false; + + // Indicate change + if(!IndicateChangedFlag()) + return false; + + return ret; +} + +// Get shutdown boolean +bool Conduit::GetShutdownFlag(bool& flag) { + + bool ret = true; + CommonData *ptr; + + // Request lock + if(!RequestLock()) + return false; + + if((ptr = GetCommonData()) != NULL) + flag = ptr->DoShutdown; + else + ret = false; + + // Release lock + if(!ReleaseLock()) + return false; + + return ret; +} + +// Set shutdown boolean +bool Conduit::SetShutdownFlag(bool flag) { + + bool ret = true; + CommonData *ptr; + + // Request lock + if(!RequestLock()) + return false; + + if((ptr = GetCommonData()) != NULL) + ptr->DoShutdown = flag; + else + ret = false; + + // Release lock + if(!ReleaseLock()) + return false; + + return ret; +} + +// Check validity of semaphore index +bool Conduit::ValidIndex(int index) { + + if(index < 0 || index > (iNumSems - FIRST_EVENT_SEM)) + return false; + + return true; +} + +// Calculate number of semaphores +int Conduit::NumberOfSemaphores() { + + return(FIRST_EVENT_SEM + GetNumEvents()); +} + + + diff --git a/Kod/display/ClientServerApp/Common/Conduit.h b/Kod/display/ClientServerApp/Common/Conduit.h new file mode 100644 index 0000000..012d3f6 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/Conduit.h @@ -0,0 +1,145 @@ +/* + * Conduit.h + * + * Declaration of the Conduit class + * + * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 + * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 + */ + +#ifndef CONDUIT_H_ +#define CONDUIT_H_ + +#include "ipclink.h" +#include + +// Segment key length +#define KEY_LEN 4 +// Data type of segment key +typedef unsigned short int KeyType; + +// Maximum number of semaphores +#define MAX_NUM_SEM 10 + +// First semaphore for event tracking +// Semaphore 0 : mutual exclusion +// Semaphore 1 : change of data +#define FIRST_EVENT_SEM 2 + + +#if defined(__GNU_LIBRARY__) && !defined(_SEM_SEMUN_UNDEFINED) +// union semun is defined by including +#else +// according to X/OPEN we have to define it ourselves +union semun { + int val; // value for SETVAL + struct semid_ds *buf; // buffer for IPC_STAT, IPC_SET + wchar_t *array; // array for GETALL, SETALL + // Linux specific part: + struct seminfo *__buf; // buffer for IPC_INFO +}; +#endif + + +// +// The Conduit class +// +class Conduit { + +public: + + // Construct & Destruct + Conduit(); + virtual ~Conduit(); + + // Server initialization + bool StartServer(KeyType* key); + // Client initialization + bool StartClient(KeyType* key); + // Detach and remove shared memory segment + bool Shutdown(); + + // Request lock on shared data + bool RequestLock(); + // Release lock on shared data + bool ReleaseLock(); + + // Wait for a flag + WaitResult WaitForFlag(FlagType flag); + // Wait for a flag with timeout + WaitResult WaitForFlag(FlagType flag, int timeout); + + // Wait for an event + WaitResult WaitForEvent(int index); + // Wait for an event with timeout [ms] + WaitResult WaitForEvent(int index, int timeout); + // Cause an event + bool CauseEvent(int index); + // Cause shutdown event + bool CauseShutdown(); + + // Get state flag + bool GetFlag(FlagType& flag); + // Set state flag + bool SetFlag(FlagType flag); + + // Get shutdown boolean + bool GetShutdownFlag(bool& flag); + // Set shutdown boolean + bool SetShutdownFlag(bool flag); + + // + // Pure virtual functions + // + // Get shared data size + virtual int GetDataSize() const = 0; + // Get number of events + virtual int GetNumEvents() const = 0; + // Get pointer to common shared data + virtual CommonData* GetCommonData() const = 0; + +protected: + + // Pointer to shared memory segment + void* ptrShm; + +private: + + // Wait-operation on a semaphore + bool Wait(int index); + // Wait-operation with timeout [ms] on a semaphore + bool Wait(int index, int timeout); + // Signal-operation on semaphore + bool Signal(int index); + + // Indicate change of flag + bool IndicateChangedFlag(); + // Wait for change of flag + bool WaitForChangedFlag(); + // Wait for change with timeout of flag + bool WaitForChangedFlag(int timeout); + // Get and compare flag + bool FlagEqual(FlagType flag, FlagType& current); + + // Check validity of semaphore index + bool ValidIndex(int index); + // Calculate number of semaphores + int NumberOfSemaphores(); + + // State flag + bool isServer; + // Number of semaphores + int iNumSems; + + // WIN32 IPC: Shared memory based on file mapping. + + // Shared memory segment handle + HANDLE hMapObject; + // Semaphore handle + HANDLE hSemaphore[MAX_NUM_SEM]; + +}; + + + +#endif /* CONDUIT_H_ */ diff --git a/Kod/display/ClientServerApp/Common/ReadABuffer.cpp b/Kod/display/ClientServerApp/Common/ReadABuffer.cpp new file mode 100644 index 0000000..97bd9b5 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ReadABuffer.cpp @@ -0,0 +1,36 @@ +#include "ReadABuffer.h" + +BOOL ReadABuffer(unsigned char * lpBuf, DWORD dwToWrite, HANDLE hComm) +{ + OVERLAPPED osWrite = {0}; + DWORD dwWritten; + BOOL fRes; + + // Create this writes OVERLAPPED structure hEvent. + osWrite.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); + if (osWrite.hEvent == NULL) + // Error creating overlapped event handle. + return FALSE; + + // Issue write. + if (!ReadFile(hComm, lpBuf, dwToWrite, &dwWritten, &osWrite)) { + if (GetLastError() != ERROR_IO_PENDING) { + // WriteFile failed, but it isn't delayed. Report error and abort. + fRes = FALSE; + } + else { + // Write is pending. + if (!GetOverlappedResult(hComm, &osWrite, &dwWritten, TRUE)) + fRes = FALSE; + else + // Write operation completed successfully. + fRes = TRUE; + } + } + else + // WriteFile completed immediately. + fRes = TRUE; + + CloseHandle(osWrite.hEvent); + return fRes; +} \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Common/ReadABuffer.h b/Kod/display/ClientServerApp/Common/ReadABuffer.h new file mode 100644 index 0000000..56cf400 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ReadABuffer.h @@ -0,0 +1,20 @@ +#ifndef READABUFFER_H +#define READABUFFER_H + +#include +#include "ipclink.h" + + +BOOL ReadABuffer(unsigned char * lpBuf, DWORD dwToWrite, HANDLE hComm); + + + + + + + + + + + +#endif \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Common/WriteABuffer.cpp b/Kod/display/ClientServerApp/Common/WriteABuffer.cpp new file mode 100644 index 0000000..41ecf52 --- /dev/null +++ b/Kod/display/ClientServerApp/Common/WriteABuffer.cpp @@ -0,0 +1,67 @@ + + +#include "WriteABuffer.h" + + +BOOL WriteABuffer(wchar_t* lpBuf, HANDLE hComm) +{ + OVERLAPPED osWrite = {0}; + DWORD dwWritten; + BOOL fRes; + + unsigned char printBuf[COMSTR_LEN]; + unsigned char* ptrPrintBuf; + ptrPrintBuf = &printBuf[0]; + + int lenBuf = (int) lpBuf[1]; + int ii; + +// printf("\tInteger: %d\n",lenBuf); + +// printf("\tLength of lpBuf: %d\n",wcslen(lpBuf)); + + for (ii=0; ii +#include +#include "ipclink.h" + +BOOL WriteABuffer(wchar_t * lpBuf, HANDLE hComm); + +BOOL ReadABuffer(wchar_t * lpBuf, DWORD dwToWrite, HANDLE hComm); + + + + + + + + + + + +#endif \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Common/ipclink.h b/Kod/display/ClientServerApp/Common/ipclink.h new file mode 100644 index 0000000..d62429e --- /dev/null +++ b/Kod/display/ClientServerApp/Common/ipclink.h @@ -0,0 +1,80 @@ +/* + * ipclink.h + * + * Interface for the shared memory communication + * + * Author: Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 + * Modified: Emil Larsson, lime@isy.liu.se, 2012-01-13 + */ + +#ifndef IPCLINK_H_ +#define IPCLINK_H_ + +// Include files for sleep-functions +#include + +// +// Shared data description +// + +// Keys +#define COM_KEY {3,1,4,6} + +// Define what is returned from wait operations +typedef unsigned int WaitResult; +#define WAIT_OK 0 +#define WAIT_FAIL 1 +#define WAIT_CLOSE 2 + +// Definition of flags +typedef unsigned int FlagType; +#define NULL_FLAG 0 + +// Exit flags +#define EXIT_OK 0 +#define EXIT_FAIL 1 + +// +// COMMON DATA +// +typedef struct structCommonData { + FlagType Flag; // State flag + bool DoShutdown; // Shutdown flag +} CommonData; + +// +// Length of , len, and bcc +// see manual for "smallprotocol package". +// +#define EXTRA_LEN 3 + + +// +// COM CONDUIT +// +// Maximum string length +//#define COMSTR_LEN 512 +// EXTRA_LEN is included +#define COMSTR_LEN 131 + + + +// Shared memory structure +typedef struct { + CommonData Common; // Common data + wchar_t string[COMSTR_LEN]; // Data string +} COMdata; + +// Flags +//#define COM_FLAG 0x01 // Example flag +// Events +#define COM_NEVENTS 2 // Number of events +#define COM_READY 0 // Ready +#define COM_REQUEST 1 // Request + +// Define sleep function +#define msleep(t) Sleep(t) + + + +#endif /* IPCLINK_H_ */ diff --git a/Kod/display/ClientServerApp/Release/Client.exe b/Kod/display/ClientServerApp/Release/Client.exe new file mode 100644 index 0000000..217d968 Binary files /dev/null and b/Kod/display/ClientServerApp/Release/Client.exe differ diff --git a/Kod/display/ClientServerApp/Release/Server.exe b/Kod/display/ClientServerApp/Release/Server.exe new file mode 100644 index 0000000..4974f18 Binary files /dev/null and b/Kod/display/ClientServerApp/Release/Server.exe differ diff --git a/Kod/display/ClientServerApp/Release/matlabclient.mexw32 b/Kod/display/ClientServerApp/Release/matlabclient.mexw32 new file mode 100644 index 0000000..d309fef Binary files /dev/null and b/Kod/display/ClientServerApp/Release/matlabclient.mexw32 differ diff --git a/Kod/display/ClientServerApp/Release/matlabclient.mexw64 b/Kod/display/ClientServerApp/Release/matlabclient.mexw64 new file mode 100644 index 0000000..e25873e Binary files /dev/null and b/Kod/display/ClientServerApp/Release/matlabclient.mexw64 differ diff --git a/Kod/display/ClientServerApp/Release/startServer.bat b/Kod/display/ClientServerApp/Release/startServer.bat new file mode 100644 index 0000000..2bcc5b9 --- /dev/null +++ b/Kod/display/ClientServerApp/Release/startServer.bat @@ -0,0 +1,15 @@ +@echo off +rem +rem Script to start modules +rem + +rem +rem 2007-09-04 +rem Erik Hellström, hellstrom@isy.liu.se. +rem + +rem | +rem | Start server +rem | +start "SERVER" cmd /C Server.exe -Pcom3 + diff --git a/Kod/display/ClientServerApp/Server/Server.cpp b/Kod/display/ClientServerApp/Server/Server.cpp new file mode 100644 index 0000000..7a7dc23 --- /dev/null +++ b/Kod/display/ClientServerApp/Server/Server.cpp @@ -0,0 +1,418 @@ +//============================================================================ +// Name : Server.cpp +// Author : Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 +// Modified : Emil Larsson, lime@isy.liu.se, 2012-01-13 +// +// Description : The server sends data on request from the client. Will +// loop until an error occurs or the client requests shutdown. +// +//============================================================================ + +#include +#include +#include +#include "ComConduit.h" +#include "WriteABuffer.h" +#include "ReadABuffer.h" + +// +// Show the command-line usage +// +void Usage(char* argv[]) { + + printf("\nUsage: %s \n\n",argv[0]); + printf(" -P= Selects the COM port, ex: -Pcom1.\n"); + printf("\n\n"); +} + + +// +// Module main function +// +int main(int argc, char* argv[]) { + + + // Conduit object + ComConduit Link; + // Key + KeyType key[KEY_LEN] = COM_KEY; + // Work variables + int i; + char c,*ptr; + wchar_t data[COMSTR_LEN]; + wchar_t* ptrData = &data[0]; + + // Data that will be saved to the buffer + wchar_t dataFromLCD[COMSTR_LEN]; + wchar_t* ptrDataFromLCD = &dataFromLCD[0]; + + FlagType ret; + unsigned char portnum; + char port[] = "COM1"; + // LPCWSTR port[] = "COM1"; + int counter=1; + + + DCB dcb; + COMMTIMEOUTS CommTimeouts; + HANDLE hCom; + + WaitResult wr; + + // Initialize data + portnum = '0'; + + // + // Parse the command-line. + // + for (i=1; i: %d\n",counter, DC); +// printf("Buffer nr: %d, \t: %d\n",counter, ptrData[1]); + + + + unsigned char readChar[COMSTR_LEN]; + unsigned char* ptr_readChar = &readChar[0]; + + int jj = 0; + if (DC == DC1){ + readBufLen = 1; + ReadABuffer(ptr_readChar,readBufLen,hCom); + dataFromLCD[0] = 0; + dataFromLCD[1] = readBufLen; + dataFromLCD[2] = (wchar_t) ptr_readChar[0]; + jj = 3; + } + else if (DC == DC2){ + ReadABuffer(ptr_readChar,3,hCom); + + dataFromLCD[2] = (wchar_t) ptr_readChar[0]; + dataFromLCD[3] = (wchar_t) ptr_readChar[1]; + dataFromLCD[4] = (wchar_t) ptr_readChar[2]; + readBufLen = (int) ptr_readChar[2]; + + for (jj=0; jj < readBufLen+2; jj++){ + ReadABuffer(ptr_readChar,1,hCom); + dataFromLCD[5+jj] = (wchar_t) ptr_readChar[0]; + } + jj = jj+5; + dataFromLCD[0] = 0; + dataFromLCD[1] = readBufLen+4; + } + else{ + printf("Data in Shared memory will not be written to the LCD buffer \nbecause the data package is not correct!!! \n\n"); + printSHM = 0; + }; + + /* Stop with a "null" char */ + dataFromLCD[jj] = (wchar_t) '\0'; + +// unsigned char readChar; +// unsigned char* ptr_readChar = &readChar; +// ReadABuffer(ptr_readChar,20,hCom); + + + if (printSHM == 1){ +// printf("After %d iteration(s), Content in Shared memory (after LCD \nbuffer is written to the Shared memory): \n",counter); + printf("After %d iteration(s), Content in Shared memory (after LCD buffer is written):\n[",counter); + for (ii=2; ii < dataFromLCD[1]+1; ii++) + { + // ReadABuffer(ptr_readChar,1,hCom); + printf("%d, ", dataFromLCD[ii]); + // printf("After %d iteration(s), Content in Shared memory: %d, ",counter, dataFromLCD[ii]); + } + printf("%d]", dataFromLCD[ii]); + printf("\n\n"); + } + +// ReadABuffer(ptr_readChar,10,hCom); + +// printf("\nSend buffer nr: %d, \t: %d\n",counter, ptr_readChar[0]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[1]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[2]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[3]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[4]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[5]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[6]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[7]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[8]); +// printf("Send buffer nr: %d, \t: %d\n",counter, ptr_readChar[9]); +// printf("Send buffer nr: %d, \t: %d\n\n",counter, ptr_readChar[10]); + + counter++; + +// int ii; +// for (ii=0; ii < 10; ii++) +// { +// +// // copy each element +// dataFromLCD[ii] = (wchar_t) ptr_readChar[ii]; +// +// } +// /* Stop with a "null" char */ +// dataFromLCD[ii] = (wchar_t) '\0'; + + + // Set data in shm + if(!Link.SetData(dataFromLCD)) { + fprintf(stderr,"SetData() failed.\n"); + return EXIT_FAIL; + } + + + // printf("\t\n"); + } + + + + + // + // Shutdown + // + + // Close COM port + if(CloseHandle(hCom) == 0) { + fprintf(stderr,"\nCan't close com-port.\n"); + ret = EXIT_FAIL; + } + + // Tell eventual clients to shutdown + if(!Link.CauseShutdown()) { + fprintf(stderr,"\nCauseShutdown() failed.\n"); + ret = EXIT_FAIL; + } + // Wait for clients to shutdown + msleep(500); + // Shutdown server + if(!Link.Shutdown()) { + fprintf(stderr,"Shutdown() failed.\n"); + ret = EXIT_FAIL; + } + // Exit + if(ret == EXIT_OK) + fprintf(stdout, "\nClean exit.\n"); + + return ret; +} \ No newline at end of file diff --git a/Kod/display/ClientServerApp/Server/Server.vcproj b/Kod/display/ClientServerApp/Server/Server.vcproj new file mode 100644 index 0000000..2444780 --- /dev/null +++ b/Kod/display/ClientServerApp/Server/Server.vcproj @@ -0,0 +1,189 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Kod/display/ClientServerApp/Server/Server.vcxproj b/Kod/display/ClientServerApp/Server/Server.vcxproj new file mode 100644 index 0000000..2d8c7c2 --- /dev/null +++ b/Kod/display/ClientServerApp/Server/Server.vcxproj @@ -0,0 +1,167 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {7B05F65E-9D5F-49CE-9339-959759F4E98C} + Server + Win32Proj + + + + Application + NotSet + false + + + Application + NotSet + false + + + Application + NotSet + + + Application + NotSet + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + true + true + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + false + false + + + + Disabled + ..\Common;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + + Level3 + EditAndContinue + + + %(AdditionalLibraryDirectories) + true + Console + MachineX86 + + + + + Disabled + ..\Common;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + EnableFastChecks + MultiThreadedDebugDLL + + + Level3 + ProgramDatabase + + + %(AdditionalLibraryDirectories) + true + Console + + + + + MaxSpeed + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + + Level3 + ProgramDatabase + "..\Common";%(AdditionalIncludeDirectories) + + + true + Console + true + true + MachineX86 + + + + + MaxSpeed + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + + Level3 + ProgramDatabase + "..\Common";%(AdditionalIncludeDirectories) + + + true + Console + true + true + + + + + + + + {8c1e78f5-0564-498a-958d-37a997bbfe49} + false + + + + + + \ No newline at end of file diff --git a/Kod/display/ClientServerApp/matlabClient/matlabClient.cpp b/Kod/display/ClientServerApp/matlabClient/matlabClient.cpp new file mode 100644 index 0000000..8f811b9 --- /dev/null +++ b/Kod/display/ClientServerApp/matlabClient/matlabClient.cpp @@ -0,0 +1,400 @@ +//============================================================================ +// Name : matlabClient.cpp +// Author : Erik Hellström, hellstrom@isy.liu.se, 2008-12-14 +// Modified : Emil Larsson, lime@isy.liu.se, 2012-01-13 +// Modified : Emil Larsson, lime@isy.liu.se, 2012-09-04 +// Data from LCD is written to the shared memory. By request, the +// data is copied to a local Matlab variable. +// +// Description : The client requests data to be sent by the server. +// +//============================================================================ + + +#include +#include +#include "ComConduit.h" +//#ifdef _CHAR16T +//#define CHAR16_T +//#endif +#include "mex.h" + +int send_communication(bool doShutdown,wchar_t* input_string,int buflen,int flag){ + // Conduit object + ComConduit Link; + // Key + KeyType key[KEY_LEN] = COM_KEY; + // Work variables + wchar_t data[COMSTR_LEN]; + wchar_t dataFromLCD[COMSTR_LEN]; + WaitResult wr; + + int retval, ii; + + + // Initialize data + retval = 0; + data[0] = '\0'; + + // strcpy((char*) data,input_string); + // wcscpy_s(&data[0],COMSTR_LEN, input_string); + + /* Copy data from input_string to data */ + for (ii=0; ii 0){ + flag = (int)*mxGetPr(prhs[0]); + }; + + plhs[1] = mxCreateDoubleMatrix(0,0,mxREAL); + // ptr_out = mxGetPr(plhs[1]); + + //if( (nrhs < 4) && (nrhs > 0)){ + // flag = (int)*mxGetPr(prhs[0]); + //}; + + + + if(flag==3){ + /* If no error, call send_communication */ + doShutdown = true; + } + + /* Do some checks of input arguments */ + if (nrhs > 0){ + if (!mxIsDouble(prhs[0])) { + mexPrintf("\nThe first input argument is not a double.\n"); + retval = 1; + } + else if (flag==4) { + mexPrintf("\nMatlab Client\n"); + mexPrintf("=============\n"); + mexPrintf("Version: %4.2f.\n", ver); + mexPrintf("Date: %s.\n", date); + retval = 4; + } + else if (!(mxGetN(prhs[0]) == 1) || !(mxGetM(prhs[0]) == 1)) { + mexPrintf("\nThe first input argument is not a scalar.\n"); + retval = 1; + } + else if (!(flag == 1 || flag == 2 || flag == 3)) { + mexPrintf("\nWrong numerical value of the first argument.\n"); + retval = 1; + } + else if (flag == 1 && nrhs < 2) { + mexPrintf("\nTo few input arguments.\n"); + retval = 1; + } + } + else{ + mexPrintf("\nTo few input arguments\n"); + retval = 1; + } + + if (nrhs > 1){ + if (!(flag == 1) || !mxIsDouble(prhs[1])) { + mexPrintf("\nFirst and second argument mismatch.\n"); + flag = 1; + retval = 1; + } + // if (!mxIsDouble(prhs[1])) { + // retval = 1; + // mexPrintf("The second argument is not a double vector.\n"); + // } + } + + if (nrhs > 2){ + mexPrintf("\nTo many input arguments\n"); + retval = 1; + } + + + /* Save output argument */ + if ((retval == 1) || (retval == 4)){ + plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL); + *(mxGetPr(plhs[0])) = retval; + } + + + + if ((nrhs > 0) && (retval == 0)){ + /* Get the length of the input string. */ + // buflen = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1; + + // mexPrintf("\nbuflen: %d \n", buflen); + + + /* Allocate memory for input and output strings. */ + // input_buf=(char *) mxCalloc(buflen, sizeof(char)); + // external_buf=(wchar_t *) mxCalloc(buflen+1, sizeof(wchar_t)); + + /* Copy the string data from prhs[0] into a C string + * input_buf. */ + // status = mxGetString(prhs[0], input_buf, buflen); + + // if (status != 0) + // mexWarnMsgTxt("Not enough space. String will be truncated."); + + + // unsigned char *start_of_pr; + // size_t bytes_to_copy; + + + /* get the length of the input vector and pointer to data*/ + if (nrhs > 1) { + nx = mxGetN(prhs[1]); + mx = mxGetM(prhs[1]); + prtData = mxGetPr(prhs[1]); + } + else { + nx = 1; + mx = 1; + prtData = mxGetPr(prhs[0]); + } + + + /* check if the argument is a row or col vector */ + if (nx > mx) + n = nx; + else + n = mx; + + + /* Check length of buffer */ + if ((n > COMSTR_LEN) && (retval == 0) && (nrhs > 1)){ + mexPrintf("\nToo long data package.\n"); + retval = 1; + } + else if ((n < 4) && (retval == 0) && (nrhs > 1)) { + mexPrintf("\nToo short data package.\n"); + retval = 1; + } + + /* Check length of data package */ + if ((int) prtData[1] != n-3 && (nrhs > 1)) { + mexPrintf("\nIncorrect data package.\n"); + retval = 1; + } + /* Check if the package starts with DC1 or DC2 */ + else if (((int) prtData[0] != 17) && ((int) prtData[0] != 18) && (nrhs > 1)) { + mexPrintf("\nIncorrect data package.\n"); +// mexPrintf("\nIncorrect data package. %d\n", (int) prtData[0]); + retval = 1; + } + + /* Copy the double input vector to a wchar_t-string */ + unsigned short int x_short[COMSTR_LEN]; + ii = 0; + for (ii=0; ii < n; ii++) + { + x_short[ii] = (unsigned short int) prtData[ii]; + + /* Calculate checksum bcc */ + if (ii < n-1) + bcc = bcc + x_short[ii]; + + /* Check if the element is too large */ + if((x_short[ii] > 255) && (retval == 0)) { + mexPrintf("Vector element %d is larger then 255.\n", ii+1); + retval = 1; + break; + } + + /* Copy each element */ + x_short_w[ii] = (wchar_t) x_short[ii]; + // mexPrintf("\nx_short[%d]: %d \n", ii, x_short[ii]); + + } + /* Stop with a "null" char */ + x_short_w[ii] = (wchar_t) '\0'; + + /* Compare the checksum bcc with the last element */ + if ((x_short[n-1] != bcc) && (retval == 0) && (nrhs > 1)) { + mexPrintf("\nIncorrect data package.\n"); + // mexPrintf("\nChecksum %d mismatch. \n", x_short[n-1]); + //printf("\nChecksum %d mismatch. \n", x_short[n-1]); + retval = 1; + } + + /* If no error, call send_communication */ + if (retval == 0){ + retval = send_communication(doShutdown,x_short_w,n,flag); + } + + /* Save output argument */ + plhs[0] = mxCreateDoubleMatrix(1,1, mxREAL); + *(mxGetPr(plhs[0])) = retval; + + /* Receive data from shared memory */ + if (flag == 2){ + + /* Length of Buffer */ + int lenBuf = (int) x_short_w[1]; + + //mexPrintf("\nHere: lenBuf %d: \n", lenBuf); + + /* Save in Shared memory */ + // int saveInSHM = 1; + + if ((x_short_w[1] == 1) && (x_short_w[2])){ + saveInSHM = 1; + } + else if ((x_short_w[1] == 1) && (x_short_w[2] == 21)){ + saveInSHM = 1; + } + else { + /* calculate checksum */ + bcc = 0; + for (ii=3; ii < (mwSize) x_short_w[4]+5; ii++){ + + /* Calculate checksum bcc */ + bcc = bcc + x_short_w[ii]; + } +// mexPrintf("\nHere: Checksum bcc %d: \n", bcc); + + /* Compare the checksum bcc with the last element */ + if (x_short_w[ii] != bcc){ +// mexPrintf("\nHere: Checksum %d mismatch. \n", x_short_w[lenBuf+1]); + retval = 1; + + saveInSHM = 0; + } + else { + saveInSHM = 1; + } + } + + + + if (saveInSHM == 1){ + + /* Save output argument */ + double *ptr_out; + plhs[1] = mxCreateDoubleMatrix(1,lenBuf,mxREAL); + ptr_out = mxGetPr(plhs[1]); + + for (ii=2; ii < (mwSize) lenBuf+2; ii++){ + ptr_out[ii-2] = (double) x_short_w[ii]; + // mexPrintf("\nx_short_w[%d]: %f \n", ii, ptr_out[ii-1]); + } + } + } + + } + +} + + + + + + + diff --git a/Kod/display/ClientServerApp/matlabClient/matlabClient.vcproj b/Kod/display/ClientServerApp/matlabClient/matlabClient.vcproj new file mode 100644 index 0000000..336b1f0 --- /dev/null +++ b/Kod/display/ClientServerApp/matlabClient/matlabClient.vcproj @@ -0,0 +1,178 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Kod/display/ClientServerApp/matlabClient/matlabClient.vcxproj b/Kod/display/ClientServerApp/matlabClient/matlabClient.vcxproj new file mode 100644 index 0000000..715e944 --- /dev/null +++ b/Kod/display/ClientServerApp/matlabClient/matlabClient.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {3094F281-B0A8-4B46-B1CC-A4B8B5CC596A} + matlabClient + Win32Proj + + + + StaticLibrary + Unicode + true + + + StaticLibrary + Unicode + true + + + StaticLibrary + NotSet + + + StaticLibrary + NotSet + + + + + + + + + + + + + + + + + + + <_ProjectFileVersion>10.0.30319.1 + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(SolutionDir)$(Configuration)\ + $(Configuration)\ + $(Configuration)\ + false + false + + + + Disabled + C:\Program Files %28x86%29\MATLAB\R2011b\extern\include;C:\stuff\svn\TFYY51\Code\Display\ClientServerApp\Common;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + EnableFastChecks + MultiThreadedDebugDLL + + + Level3 + EditAndContinue + + + + + Disabled + C:\Program Files %28x86%29\MATLAB\R2011b\extern\include;C:\stuff\svn\TFYY51\Code\Display\ClientServerApp\Common;%(AdditionalIncludeDirectories) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + EnableFastChecks + MultiThreadedDebugDLL + + + Level3 + ProgramDatabase + + + + + MaxSpeed + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + + Level3 + ProgramDatabase + C:\Program Files %28x86%29\MATLAB\R2011b\extern\include;..\Common + + + true + Console + true + true + MachineX86 + C:\Program Files %28x86%29\MATLAB\R2011b\extern\include;%(AdditionalLibraryDirectories) + libmx.lib;libmex.lib;libmat.lib;%(AdditionalDependencies) + + + + + MaxSpeed + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + MultiThreadedDLL + true + + + Level3 + ProgramDatabase + C:\Program Files\MATLAB\R2011b\extern\include;..\Common + + + true + Console + true + true + C:\Program Files %28x86%29\MATLAB\R2011b\extern\include;%(AdditionalLibraryDirectories) + libmx.lib;libmex.lib;libmat.lib;%(AdditionalDependencies) + + + + + + + + {8c1e78f5-0564-498a-958d-37a997bbfe49} + false + + + + + + \ No newline at end of file diff --git a/Kod/display/ClientServerApp/matlabClient/stdafx.cpp b/Kod/display/ClientServerApp/matlabClient/stdafx.cpp new file mode 100644 index 0000000..1d98a62 --- /dev/null +++ b/Kod/display/ClientServerApp/matlabClient/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// matlabClient.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/Kod/display/ClientServerApp/matlabClient/stdafx.h b/Kod/display/ClientServerApp/matlabClient/stdafx.h new file mode 100644 index 0000000..47a0d02 --- /dev/null +++ b/Kod/display/ClientServerApp/matlabClient/stdafx.h @@ -0,0 +1,15 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +#include +#include + + + +// TODO: reference additional headers your program requires here diff --git a/Kod/display/ClientServerApp/matlabClient/targetver.h b/Kod/display/ClientServerApp/matlabClient/targetver.h new file mode 100644 index 0000000..a38195a --- /dev/null +++ b/Kod/display/ClientServerApp/matlabClient/targetver.h @@ -0,0 +1,13 @@ +#pragma once + +// The following macros define the minimum required platform. The minimum required platform +// is the earliest version of Windows, Internet Explorer etc. that has the necessary features to run +// your application. The macros work by enabling all features available on platform versions up to and +// including the version specified. + +// Modify the following defines if you have to target a platform prior to the ones specified below. +// Refer to MSDN for the latest info on corresponding values for different platforms. +#ifndef _WIN32_WINNT // Specifies that the minimum required platform is Windows Vista. +#define _WIN32_WINNT 0x0600 // Change this to the appropriate value to target other versions of Windows. +#endif + diff --git a/Kod/display/ClientServerApp/matlabMake.bat b/Kod/display/ClientServerApp/matlabMake.bat new file mode 100644 index 0000000..3ee0c82 --- /dev/null +++ b/Kod/display/ClientServerApp/matlabMake.bat @@ -0,0 +1,16 @@ +rem +rem Run to make the mex-file +rem + +rem +rem Run first mex -setup for x86 architecture, i.e., in +rem directory: "C:\Program Files (x86)\MATLAB\R2011b\bin" +rem + +rem C:\PROGRA~2\MATLAB\R2011b\bin\mex matlabClient\Debug\matlabclient.obj Common\Release\Conduit.obj Common\Release\ComConduit.obj Common\Release\WriteABuffer.obj -outdir Release\ + +C:\PROGRA~1\MATLAB\R2011b\bin\mex matlabClient\Release\matlabclient.obj Common\Release\Conduit.obj Common\Release\ComConduit.obj Common\Release\WriteABuffer.obj -outdir Release\ + + +rem +rem move matlabClient\matlabclient.mexw32 Release \ No newline at end of file diff --git a/Kod/display/DisplaySpec/edip320-8e.pdf b/Kod/display/DisplaySpec/edip320-8e.pdf new file mode 100644 index 0000000..5250332 Binary files /dev/null and b/Kod/display/DisplaySpec/edip320-8e.pdf differ diff --git a/Kod/display/display_exempel.m b/Kod/display/display_exempel.m new file mode 100644 index 0000000..c920087 --- /dev/null +++ b/Kod/display/display_exempel.m @@ -0,0 +1,161 @@ +%% Version 0.1 +% Detta skript är exempel på hur kommunikation med den tryckkänsliga +% skärmen fungerar. + +%% Start Server +addpath ClientServerApp\Release + +cd ClientServerApp\Release + +!startServer +cd ../.. +%% Diagonal linje +DC1 = 17; +ESC = 27; +Code = 'GD'; + +% x1, y1, x2, y2, (320 x 240 pixlar) +% minst signifikanta bitar till vänster +% mest signifikanta bitar till höger +arg = [0, 0, 0, 0, 63, 1, 239, 0]; + +% Save the 'small package' as a string +data = [ESC, double(Code), arg]; +len = length(data); +initStr = [DC1, len, data]; +bcc = mod(sum(initStr), 256); +str = [initStr, bcc]; + +% Skriv +matlabclient(1, str') + +%% Rita en rektangel som är fylld med mönster +DC1 = 17; +ESC = 27; +Code = 'RM'; + +% x1, y1, x2, y2, (320 x 240 pixlar) +% minst signifikanta bitar till vänster +% mest signifikanta bitar till höger +arg1 = [57, 0, 100, 0, 1, 1, 180, 0]; + +% Pattern +arg2 = 8; + +% Save the 'small package' as a string +data = [ESC, double(Code), arg1, arg2]; +len = length(data); +initStr = [DC1, len, data]; +bcc = mod(sum(initStr), 256); +str = [initStr, bcc]; + +% Skriv +matlabclient(1, str') + +%% Skriv en Test-sträng +DC1 = 17; +ESC = 27; +Code = double('ZL'); + +% x1, y1, (320 x 240 pixlar) +% minst signifikanta bitar till vänster +% mest signifikanta bitar till höger +arg1 = [117, 0, 32, 0]; + +% Textsträng +%arg2 = 'Test'; + +arg2 = 'DISPLAY!!!!!'; + +% Null +arg3 = 0; + + +% Save the 'small package' as a string +data = [ESC, double(Code), arg1, double(arg2), arg3]; +len = length(data); +initStr = [DC1, len, data]; +bcc = mod(sum(initStr), 256); +str = [initStr, bcc]; + +% Skriv +matlabclient(1, str') + +%% Definierar en Touch-area +DC1 = 17; +ESC = 27; +Code = 'AK'; + +% x1, y1, x2, y2, (320 x 240 pixlar) +% minst signifikanta bitar till vänster +% mest signifikanta bitar till höger +%arg1 = [120, 0, 100, 0, 180, 0, 130, 0]; +%arg1 = [120, 0, 100, 0, 200, 0, 120, 0]; +%arg1 = [120, 0, 130, 0, 200, 0, 160, 0]; +arg1 = [120, 0, 170, 0, 200, 0, 190, 0]; + +arg2 = 150; +arg3 = 151; + +arg4 = 'CTouch Me'; + +% Null +arg5 = 0; + +% Save the 'small package' as a string +data = [ESC, double(Code), arg1, arg2, arg3, double(arg4), arg5]; +len = length(data); +initStr = [DC1, len, data]; +bcc = mod(sum(initStr), 256); +str = [initStr, bcc]; + +% Skriv +matlabclient(1, str') +%fwrite(lcd, str) + +%% Definierar en Touch-area +DC1 = 17; +ESC = 27; +Code = 'AJ'; + +% x1, y1, x2, y2, (320 x 240 pixlar) +% minst signifikanta bitar till vänster +% mest signifikanta bitar till höger +arg1 = [100, 0, 150, 0]; + +arg2 = 2; +arg3 = [20, 20]; + +arg4 = 'RMarkera mig'; + +% Null +arg5 = 0; + +% Save the 'small package' as a string +data = [ESC, double(Code), arg1, arg2, arg3, double(arg4), arg5]; +len = length(data); +initStr = [DC1, len, data]; +bcc = mod(sum(initStr), 256); +str = [initStr, bcc]; + +matlabclient(1, str') +%fwrite(lcd, str) + +%% Radera Displayen +DC1 = 17; +ESC = 27; +Code = 'DL'; + +% Save the 'small package' as a string +data = [ESC, double(Code)]; +len = length(data); +initStr = [DC1, len, data]; +bcc = mod(sum(initStr), 256); +str = [initStr, bcc]; + +% Skriv +matlabclient(1, str') +%fwrite(lcd, str) + +%% Avsluta kommunikation med display +matlabclient(3); \ No newline at end of file -- cgit v1.2.1