#pragma once //------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------BEGIN CUT AND PASTE BLOCK----------------------------------------------------------------------------------- /******************************************************************** FileName: Form1.h Dependencies: Windows Server 2003 R2 Platform SDK during development. When compiled, needs .NET framework 2.0 redistributable to run (under Microsoft Windows XP, Vista comes with .NET 2.0 redistributable package already installed) May also need Microsoft Visual C++ redistributable to run. Hardware: Need a free USB port to connect USB peripheral device programmed with appropriate MCHPUSB demo firmware. VID and PID in firmware must match the VID and PID in this program. Compiler: Microsoft Visual C++ 2005 Express Edition (or better) (Microsoft Visual C++ 2008 Express Edition is believed to work, but is currently untested) Company: Microchip Technology, Inc. Software License Agreement: The software supplied herewith by Microchip Technology Incorporated (the “Company”) for its PIC® Microcontroller is intended and supplied to you, the Company’s customer, for use solely and exclusively with Microchip PIC Microcontroller products. The software is owned by the Company and/or its supplier, and is protected under applicable copyright laws. All rights are reserved. Any use in violation of the foregoing restrictions may subject the user to criminal sanctions under applicable laws, as well as to civil liability for the breach of the terms and conditions of this license. THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. ******************************************************************** File Description: Change History: Rev Date Description 1.0 06/15/2008 Initial release ******************************************************************** NOTE: All user made code contained in this project is in the Form1.h file. All other code and files were generated automatically by either the new project wizard, or by the development environment (ex: code is automatically generated if you create a new button on the form, and then double click on it, which creates a click event handler function). All user made code is contained in clearly marked cut and paste blocks. NOTE2: Need Windows Server 2003 R2 Platform SDK. Untested with Windows SDK. If getting build errors when trying to compile this project, make sure the platform SDK is correctly installed, and that it has been integrated into the VC++ development environment. In other words, make sure the include paths have been setup correctly in this IDE. Microsoft created some small tutorial videos showing how to do this. These may be located at: http://msdn.microsoft.com/en-us/visualc/aa336415.aspx The link is functional as of 21 May 2008, but could move. If the above link does not work, try searching for "Video1_PSDK.wmv", which may be associated with: "Using Visual C++ Express Edition and the Platform SDK" ********************************************************************/ //Includes #include //Definitions for various common and not so common types like DWORD, PCHAR, HANDLE, etc. #include //Need this for definitions of WM_DEVICECHANGE messages #include "mpusbapi.h" //Make sure this header file is located in your project directory. //#include "ServerGame.h" //When modifying the firmware and changing the Vendor and Device ID's, make //sure to update the PC application as well. //Use the formatting: "Vid_xxxx&Pid_xxxx" where xxxx is a 16-bit hexadecimal number. //This is the USB device that this program will look for: #define DeviceVID_PID "vid_04d8&pid_000c" //-------------------------------------------------------END CUT AND PASTE BLOCK------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------------------------- namespace MCHPUSBPnPDemo { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; using namespace System::Net; using namespace System::Net::Sockets; //------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------BEGIN CUT AND PASTE BLOCK----------------------------------------------------------------------------------- using namespace System::Threading; using namespace System::Runtime::InteropServices; //Need this to support "unmanaged" code. /* In order to use these unmanaged functions from within the managed .NET environment, we need to explicitly import the functions which we will be using from other .DLL file(s). Simply including the appropriate header files is not enough. Note: In order to avoid potential name conflicts in the header files (which we still use), I have renamed the functions by adding "UM" (unmanaged) onto the end of them. To find documentation for the functions in MSDN, search for the function name without the extra "UM" attached. Note2: In the header files, normally the function names are remapped, depending upon if UNICODE is defined or not. For example, two versions of the function SetupDiGetDeviceInterfaceDetail() exist. One for UNICODE, and one for ANSI. If the wrong version of the function is called, things won't work correctly. Therefore, in order to make sure the correct one gets called (based on your compiler settings, which may or may not define "UNICODE"), it is useful to explicity specify the CharSet when doing the DLL import. */ #ifdef UNICODE #define Seeifdef Unicode #else #define Seeifdef Ansi #endif //See the mpusbapi.dll source code (_mpusbapi.cpp) for API related documentation for these functions. //The source code is in the MCHPFSUSB vX.X distributions. [DllImport("MPUSBAPI.dll" , EntryPoint="_MPUSBGetDLLVersion")] extern "C" DWORD MPUSBGetDLLVersion(void); [DllImport("MPUSBAPI.dll" , EntryPoint="_MPUSBGetDeviceCount")] extern "C" DWORD MPUSBGetDeviceCount(PCHAR pVID_PID); [DllImport("MPUSBAPI.dll" , EntryPoint="_MPUSBOpen")] extern "C" HANDLE MPUSBOpen(DWORD instance, // Input PCHAR pVID_PID, // Input PCHAR pEP, // Input DWORD dwDir, // Input DWORD dwReserved);// Input [DllImport("MPUSBAPI.dll" , EntryPoint="_MPUSBClose")] extern "C" BOOL MPUSBClose(HANDLE handle); //Input [DllImport("MPUSBAPI.dll" , EntryPoint="_MPUSBRead")] extern "C" DWORD MPUSBRead(HANDLE handle, // Input PVOID pData, // Output DWORD dwLen, // Input PDWORD pLength, // Output DWORD dwMilliseconds);// Input [DllImport("MPUSBAPI.dll" , EntryPoint="_MPUSBWrite")] extern "C" DWORD MPUSBWrite(HANDLE handle, // Input PVOID pData, // Output DWORD dwLen, // Input PDWORD pLength, // Output DWORD dwMilliseconds);// Input [DllImport("MPUSBAPI.dll" , EntryPoint="_MPUSBReadInt")] extern "C" DWORD MPUSBReadInt(HANDLE handle, // Input PVOID pData, // Output DWORD dwLen, // Input PDWORD pLength, // Output DWORD dwMilliseconds);// Input //Need this function for receiving all of the WM_DEVICECHANGE messages. See MSDN documentation for //description of what this function does/how to use it. Note: name is remapped "RegisterDeviceNotificationUM" to //avoid possible build error conflicts. [DllImport("user32.dll" , CharSet = CharSet::Seeifdef, EntryPoint="RegisterDeviceNotification")] extern "C" HDEVNOTIFY WINAPI RegisterDeviceNotificationUM( HANDLE hRecipient, LPVOID NotificationFilter, DWORD Flags); //----------------Global variables used in this application-------------------------------- HANDLE EP1INHandle = INVALID_HANDLE_VALUE; HANDLE EP1OUTHandle = INVALID_HANDLE_VALUE; unsigned int ADCValue = 0; BOOL AttachedState = FALSE; //Need to keep track of the USB device attachment status for proper plug and play operation. //-------------------------------------------------------END CUT AND PASTE BLOCK------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------------------------- public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); this->listenInterface = gcnew IPAddress(this->addr); this->tcpServer = gcnew TcpListener(listenInterface, (int)listenport); //------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------BEGIN CUT AND PASTE BLOCK----------------------------------------------------------------------------------- //Additional constructor code //Globally Unique Identifier (GUID). Windows uses GUIDs to identify things. GUID InterfaceClassGuid = {0xa5dcbf10, 0x6530, 0x11d2, 0x90, 0x1F, 0x00, 0xC0, 0x4F, 0xB9, 0x51, 0xED}; //Globally Unique Identifier (GUID) for USB peripheral devices //Register for WM_DEVICECHANGE notifications: DEV_BROADCAST_DEVICEINTERFACE MyDeviceBroadcastHeader;// = new DEV_BROADCAST_HDR; MyDeviceBroadcastHeader.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; MyDeviceBroadcastHeader.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE); MyDeviceBroadcastHeader.dbcc_reserved = 0; //Reserved says not to use... MyDeviceBroadcastHeader.dbcc_classguid = InterfaceClassGuid; RegisterDeviceNotificationUM((HANDLE)this->Handle, &MyDeviceBroadcastHeader, DEVICE_NOTIFY_WINDOW_HANDLE); //Now perform an initial start up check of the device state (attached or not attached), since we would not have //received a WM_DEVICECHANGE notification. if(MPUSBGetDeviceCount(DeviceVID_PID)) //Check and make sure at least one device with matching VID/PID is attached { EP1OUTHandle = MPUSBOpen(0, DeviceVID_PID, "\\MCHP_EP1", MP_WRITE, 0); EP1INHandle = MPUSBOpen(0, DeviceVID_PID, "\\MCHP_EP1", MP_READ, 0); AttachedState = TRUE; StatusBox_txtbx->Text = "Device Found: AttachedState = TRUE"; label2->Enabled = true; //Make the label no longer greyed out } else //Device must not be connected (or not programmed with correct firmware) { StatusBox_txtbx->Text = "Device Not Detected: Verify Connection/Correct Firmware"; AttachedState = FALSE; label2->Enabled = false; //Make the label greyed out } ReadWriteThread->RunWorkerAsync(); //Recommend performing USB read/write operations in a separate thread. Otherwise, //the Read/Write operations are effectively blocking functions and can lock up the //user interface if the I/O operations take a long time to complete. ServerBackgroundWorker->RunWorkerAsync(); //-------------------------------------------------------END CUT AND PASTE BLOCK------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------------------------- } protected: /// /// Clean up any resources being used. /// ~Form1() { //------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------BEGIN CUT AND PASTE BLOCK----------------------------------------------------------------------------------- //Make sure to close any open handles, before exiting the application if (EP1OUTHandle != INVALID_HANDLE_VALUE) MPUSBClose (EP1OUTHandle); if (EP1INHandle != INVALID_HANDLE_VALUE) MPUSBClose (EP1INHandle); //-------------------------------------------------------END CUT AND PASTE BLOCK------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------------------------- if (components) { delete components; } } //static private array^ adresse = {198,162,50,128}; static private array^ addr = {198,162,50,108}; static private IPAddress^ listenInterface = nullptr;//IPAddress::Any; static private unsigned short listenport = 7890; static private int buffersize = 16; static private TcpListener^ tcpServer = nullptr; static private TcpClient^ tcpClient = nullptr; static private NetworkStream^ tcpStream = nullptr; static private array^ sendBuffer = gcnew array(buffersize); static private array^ receiveBuffer = gcnew array(buffersize); static private array^ byteCount; static private int bytesToRead = 0, nextReadCount, rc; private: System::Windows::Forms::ProgressBar^ progressBar1; protected: private: System::ComponentModel::BackgroundWorker^ ReadWriteThread; private: System::Windows::Forms::Timer^ timer1; private: System::Windows::Forms::TextBox^ StatusBox_txtbx; private: System::Windows::Forms::Label^ label1; private: System::Windows::Forms::Label^ label2; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::Button^ button2; private: System::Windows::Forms::Button^ button3; private: System::Windows::Forms::Button^ button4; private: System::Windows::Forms::Button^ button5; private: System::ComponentModel::BackgroundWorker^ ServerBackgroundWorker; private: System::Windows::Forms::Label^ label3; private: System::ComponentModel::IContainer^ components; private: /// /// Required designer variable. /// #pragma region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->progressBar1 = (gcnew System::Windows::Forms::ProgressBar()); this->ReadWriteThread = (gcnew System::ComponentModel::BackgroundWorker()); this->timer1 = (gcnew System::Windows::Forms::Timer(this->components)); this->StatusBox_txtbx = (gcnew System::Windows::Forms::TextBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->label2 = (gcnew System::Windows::Forms::Label()); this->button1 = (gcnew System::Windows::Forms::Button()); this->button2 = (gcnew System::Windows::Forms::Button()); this->button3 = (gcnew System::Windows::Forms::Button()); this->button4 = (gcnew System::Windows::Forms::Button()); this->button5 = (gcnew System::Windows::Forms::Button()); this->ServerBackgroundWorker = (gcnew System::ComponentModel::BackgroundWorker()); this->label3 = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // progressBar1 // this->progressBar1->BackColor = System::Drawing::Color::White; this->progressBar1->ForeColor = System::Drawing::Color::Red; this->progressBar1->Location = System::Drawing::Point(13, 122); this->progressBar1->Maximum = 1024; this->progressBar1->Name = L"progressBar1"; this->progressBar1->Size = System::Drawing::Size(403, 23); this->progressBar1->Step = 1; this->progressBar1->Style = System::Windows::Forms::ProgressBarStyle::Continuous; this->progressBar1->TabIndex = 9; // // ReadWriteThread // this->ReadWriteThread->WorkerReportsProgress = true; this->ReadWriteThread->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::ReadWriteThread_DoWork); // // timer1 // this->timer1->Enabled = true; this->timer1->Interval = 8; this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick); // // StatusBox_txtbx // this->StatusBox_txtbx->Location = System::Drawing::Point(13, 13); this->StatusBox_txtbx->Multiline = true; this->StatusBox_txtbx->Name = L"StatusBox_txtbx"; this->StatusBox_txtbx->Size = System::Drawing::Size(298, 20); this->StatusBox_txtbx->TabIndex = 11; // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(317, 16); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(58, 13); this->label1->TabIndex = 12; this->label1->Text = L"Status Box"; // // label2 // this->label2->AutoSize = true; this->label2->Enabled = false; this->label2->Location = System::Drawing::Point(123, 106); this->label2->Name = L"label2"; this->label2->Size = System::Drawing::Size(103, 13); this->label2->TabIndex = 13; this->label2->Text = L"//useful progess bar"; // // button1 // this->button1->Location = System::Drawing::Point(13, 58); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(75, 23); this->button1->TabIndex = 14; this->button1->Text = L"Relais 1"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // button2 // this->button2->Location = System::Drawing::Point(95, 57); this->button2->Name = L"button2"; this->button2->Size = System::Drawing::Size(75, 23); this->button2->TabIndex = 15; this->button2->Text = L"Relais 2"; this->button2->UseVisualStyleBackColor = true; this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click); // // button3 // this->button3->Location = System::Drawing::Point(177, 57); this->button3->Name = L"button3"; this->button3->Size = System::Drawing::Size(75, 23); this->button3->TabIndex = 16; this->button3->Text = L"Relais 3"; this->button3->UseVisualStyleBackColor = true; this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click); // // button4 // this->button4->Location = System::Drawing::Point(259, 56); this->button4->Name = L"button4"; this->button4->Size = System::Drawing::Size(75, 23); this->button4->TabIndex = 17; this->button4->Text = L"Relais 4"; this->button4->UseVisualStyleBackColor = true; this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click); // // button5 // this->button5->Location = System::Drawing::Point(341, 55); this->button5->Name = L"button5"; this->button5->Size = System::Drawing::Size(75, 23); this->button5->TabIndex = 18; this->button5->Text = L"Relais 5"; this->button5->UseVisualStyleBackColor = true; this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click); // // ServerBackgroundWorker // this->ServerBackgroundWorker->WorkerReportsProgress = true; this->ServerBackgroundWorker->DoWork += gcnew System::ComponentModel::DoWorkEventHandler(this, &Form1::ServerBackgroundWorker_DoWork); // // label3 // this->label3->AutoSize = true; this->label3->Location = System::Drawing::Point(13, 40); this->label3->Name = L"label3"; this->label3->Size = System::Drawing::Size(96, 13); this->label3->TabIndex = 19; this->label3->Text = L"MessageReceived"; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(432, 175); this->Controls->Add(this->label3); this->Controls->Add(this->button5); this->Controls->Add(this->button4); this->Controls->Add(this->button3); this->Controls->Add(this->button2); this->Controls->Add(this->button1); this->Controls->Add(this->label2); this->Controls->Add(this->label1); this->Controls->Add(this->StatusBox_txtbx); this->Controls->Add(this->progressBar1); this->Name = L"Form1"; this->Text = L"Contrôleur : carte de relais, USB"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion //------------------------------------------------------------------------------------------------------------------------------------------------------------------- //-------------------------------------------------------BEGIN CUT AND PASTE BLOCK----------------------------------------------------------------------------------- private: System::Void ReadWriteThread_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) { //This thread does the actual USB read/write operations (but only when AttachedState == TRUE). //Since this is a separate thread, this code below executes asynchronously from the reset of the //code in this application. /*unsigned char Buffer [64]; DWORD ActualLength;*/ /*while(true) { Buffer[0] = 0x37; //READ_POT command (see the firmware source code), gets 10-bit ADC Value if(AttachedState == TRUE) //Do not try and send USB traffic unless the device is actually attached and enumerated/configured. { if(MPUSBWrite(EP1OUTHandle, Buffer, 1, &ActualLength, 1000)) //Send the command now over USB { if(MPUSBRead(EP1INHandle, Buffer, 3, &ActualLength, 1000)) //Receive the answer from the device firmware through USB { ADCValue = (Buffer[2] << 8) + Buffer[1]; } } } else { ADCValue = 0; }*/ //Sleep(4); //Add a small delay. Otherwise, this while(true) loop executes at warp speed and can cause //tangible CPU utilization increase, with no particular benefit to the application. //} } private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { //When the timer goes off on the main form, update the user inferface with the new data obtained from the thread. if(ADCValue > (unsigned int)progressBar1->Maximum) { ADCValue = progressBar1->Maximum; } progressBar1->Value = ADCValue; } protected: virtual void WndProc( Message% m ) override{ //This is a callback function that gets called when a Windows message is received by the form. // Listen for Windows messages. We will receive various different types of messages, but the ones we really want to use are the WM_DEVICECHANGE messages. if(m.Msg == WM_DEVICECHANGE) { if(((int)m.WParam == DBT_DEVICEARRIVAL) || ((int)m.WParam == DBT_DEVICEREMOVEPENDING) || ((int)m.WParam == DBT_DEVICEREMOVECOMPLETE) || ((int)m.WParam == DBT_CONFIGCHANGED) ) { //WM_DEVICECHANGE messages by themselves are quite generic, and can be caused by a number of different //sources, not just your USB hardware device. Therefore, must check to find out if any changes relavant //to your device (with known VID/PID) took place before doing any kind of opening or closing of endpoints. //(the message could have been totally unrelated to your application/USB device) if(MPUSBGetDeviceCount(DeviceVID_PID)) //Check and make sure at least one device with matching VID/PID is attached { if(AttachedState == FALSE) { EP1OUTHandle = MPUSBOpen(0, DeviceVID_PID, "\\MCHP_EP1", MP_WRITE, 0); EP1INHandle = MPUSBOpen(0, DeviceVID_PID, "\\MCHP_EP1", MP_READ, 0); AttachedState = TRUE; StatusBox_txtbx->Text = "Device Found: AttachedState = TRUE"; label2->Enabled = true; //Make the label no longer greyed out } //else AttachedState == TRUE, in this case, do not try to re-open already open and functioning endpoints. Doing //so will break them and make them no longer work. } else //No devices attached, WM_DEVICECHANGE message must have been caused by your USB device falling off the bus (IE: user unplugged/powered down) { if(MPUSBClose(EP1OUTHandle)) EP1OUTHandle = INVALID_HANDLE_VALUE; if(MPUSBClose(EP1INHandle)) EP1INHandle = INVALID_HANDLE_VALUE; AttachedState = FALSE; StatusBox_txtbx->Text = "Device Not Detected: Verify Connection/Correct Firmware"; label2->Enabled = false; //Make the label greyed out } } } Form::WndProc( m ); } //-------------------------------------------------------END CUT AND PASTE BLOCK------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------------------------------------------------------------------------------- private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { unsigned char Buffer [1]; DWORD ActualLength;//DWORD Buffer[0] = 0x38; //READ_POT command (see the firmware source code), gets 10-bit ADC Value if(AttachedState == TRUE) //Do not try and send USB traffic unless the device is actually attached and enumerated/configured. { if(MPUSBWrite(EP1OUTHandle, Buffer, 1, &ActualLength, 1000)) //Send the command now over USB { //if(MPUSBRead(EP1INHandle, Buffer, 3, &ActualLength, 1000)) //Receive the answer from the device firmware through USB //{ //ADCValue = (Buffer[2] << 8) + Buffer[1]; //} } } else { //ADCValue = 0; } Sleep(4); } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { unsigned char Buffer [1]; DWORD ActualLength; Buffer[0] = 0x39; //READ_POT command (see the firmware source code), gets 10-bit ADC Value if(AttachedState == TRUE) //Do not try and send USB traffic unless the device is actually attached and enumerated/configured. { if(MPUSBWrite(EP1OUTHandle, Buffer, 1, &ActualLength, 1000)) //Send the command now over USB { //if(MPUSBRead(EP1INHandle, Buffer, 3, &ActualLength, 1000)) //Receive the answer from the device firmware through USB //{ //ADCValue = (Buffer[2] << 8) + Buffer[1]; //} } } else { //ADCValue = 0; } Sleep(4); } private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) { unsigned char Buffer [1]; DWORD ActualLength; Buffer[0] = 0x40; //READ_POT command (see the firmware source code), gets 10-bit ADC Value if(AttachedState == TRUE) //Do not try and send USB traffic unless the device is actually attached and enumerated/configured. { if(MPUSBWrite(EP1OUTHandle, Buffer, 1, &ActualLength, 1000)) //Send the command now over USB { //if(MPUSBRead(EP1INHandle, Buffer, 3, &ActualLength, 1000)) //Receive the answer from the device firmware through USB //{ //ADCValue = (Buffer[2] << 8) + Buffer[1]; //} } } else { //ADCValue = 0; } Sleep(4); } private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) { unsigned char Buffer [1]; DWORD ActualLength; Buffer[0] = 0x41; //READ_POT command (see the firmware source code), gets 10-bit ADC Value if(AttachedState == TRUE) //Do not try and send USB traffic unless the device is actually attached and enumerated/configured. { if(MPUSBWrite(EP1OUTHandle, Buffer, 1, &ActualLength, 1000)) //Send the command now over USB { //if(MPUSBRead(EP1INHandle, Buffer, 3, &ActualLength, 1000)) //Receive the answer from the device firmware through USB //{ //ADCValue = (Buffer[2] << 8) + Buffer[1]; //} } } else { //ADCValue = 0; } Sleep(4); } private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) { unsigned char Buffer [1]; DWORD ActualLength; Buffer[0] = 0x42; //READ_POT command (see the firmware source code), gets 10-bit ADC Value if(AttachedState == TRUE) //Do not try and send USB traffic unless the device is actually attached and enumerated/configured. { if(MPUSBWrite(EP1OUTHandle, Buffer, 1, &ActualLength, 1000)) //Send the command now over USB { //if(MPUSBRead(EP1INHandle, Buffer, 3, &ActualLength, 1000)) //Receive the answer from the device firmware through USB //{ //ADCValue = (Buffer[2] << 8) + Buffer[1]; //} } } else { //ADCValue = 0; } Sleep(4); } private: System::Void ServerBackgroundWorker_DoWork(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) { /*static ServerGame * server = new ServerGame(); while(true) { server->update(); }*/ //this->tcpServer = gcnew TcpListener(listenInterface, (int)listenport); tcpServer->Start(); tcpClient = tcpServer->AcceptTcpClient(); tcpStream = tcpClient->GetStream(); byteCount = BitConverter::GetBytes(bytesToRead); tcpStream->Read(byteCount, 0, byteCount->Length); bytesToRead = BitConverter::ToInt32(byteCount, 0); while(bytesToRead > 0 ){ if(bytesToRead < receiveBuffer->Length){ nextReadCount = bytesToRead; }else{ nextReadCount = receiveBuffer->Length; } rc = tcpStream->Read(receiveBuffer, 0, nextReadCount); bytesToRead -= rc; } String^ readText = System::Text::Encoding::UTF8->GetString(receiveBuffer, 0, rc); //Stuff to do with the received command this->label3->Text = readText; unsigned char Buffer [1]; DWORD ActualLength; Buffer[0] = NULL; if(readText->Equals("a") || readText == "a"/*readText == "0x38"*/){ Buffer[0] = 0x38; } if(readText->Equals("b") || readText == "b"/*readText == "0x39"*/){ Buffer[0] = 0x39; } if(readText->Equals("c") || readText == "c"/*readText == "0x40"*/){ Buffer[0] = 0x40; } if(readText->Equals("d") || readText == "d"/*readText == "0x41"*/){ Buffer[0] = 0x41; } if(readText->Equals("e") || readText == "e"/*readText == "0x42"*/){ Buffer[0] = 0x42; } if(Buffer[0] != NULL){ if(AttachedState == TRUE) //Do not try and send USB traffic unless the device is actually attached and enumerated/configured. { if(MPUSBWrite(EP1OUTHandle, Buffer, 1, &ActualLength, 1000)) //Send the command now over USB { //if(MPUSBRead(EP1INHandle, Buffer, 3, &ActualLength, 1000)) //Receive the answer from the device firmware through USB //{ //ADCValue = (Buffer[2] << 8) + Buffer[1]; //} } } else { //ADCValue = 0; } } Sleep(4); //closing client tcpStream->Close(); tcpStream = nullptr; tcpClient->Close(); tcpClient = nullptr; tcpServer->Stop(); } private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { //this->tcpServer = gcnew TcpListener(listenInterface, (int)listenport); /*tcpServer->Start(); tcpClient = tcpServer->AcceptTcpClient(); tcpStream = tcpClient->GetStream(); byteCount = BitConverter::GetBytes(bytesToRead); tcpStream->Read(byteCount, 0, byteCount->Length); bytesToRead = BitConverter::ToInt32(byteCount, 0); while(bytesToRead > 0 ){ if(bytesToRead < receiveBuffer->Length){ nextReadCount = bytesToRead; }else{ nextReadCount = receiveBuffer->Length; } rc = tcpStream->Read(receiveBuffer, 0, nextReadCount); bytesToRead -= rc; } String^ readText = System::Text::Encoding::ASCII->GetString(receiveBuffer, 0, rc); //Stuff to do with the received command //closing client tcpStream->Close(); tcpStream = nullptr; tcpClient->Close(); tcpClient = nullptr; //tcpServer->Stop();*/ } }; }