⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commdsp.cpp

📁 for asuncyhronous pwm motor control
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "stdafx.h"
#include "CommDSP.h"
#include <windows.h>
#include <commctrl.h>

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE			g_hInst;			// current instance
HWND				g_hWndMenuBar;		// menu bar handle

wchar_t PrincipaleSS[200];
HANDLE hComm;
bool Connected;
bool VitessePositionFlag;

// Forward declarations of functions included in this code module:
ATOM			MyRegisterClass(HINSTANCE, LPTSTR);
BOOL			InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK Config(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK PiloteReferences(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
void UpdatePrincipaleSS();
void EnvoieSemiWord(HWND hDlg, __int8 code, double data);

//--------------------------------------------------------------------
/* portNm =COM1,COM2 etc which ever COM port your phone is connected */
bool OpenPort( HWND hWnd, wchar_t *portNm )  
{    
	if( portNm != NULL ) // check to if port exists
    {
/* hComm is handle that makes a buffer file pointin to your
   COM port for read and write of port data */
	hComm = CreateFile( portNm, GENERIC_READ | GENERIC_WRITE, 
                        0, 
                        0, 
                        OPEN_EXISTING,
                        NULL,
                        0);
    if (hComm == INVALID_HANDLE_VALUE)
        {
        //error occured alert user about error 
					MessageBox( hWnd, portNm, _T("Port Open Error"), MB_OK);
        return false;
        }
    else  return true; // port opened successfully alert user
    }
else
    {
      // specified port missing alert user
          return false;
    }
}
//--------------------------------------------------------------------
BOOL WriteABuffer(char * lpBuf, DWORD dwToWrite)
{
OVERLAPPED osWrite = {0};
DWORD dwWritten;
BOOL fRes;
	// Issue write.
	if (!WriteFile(hComm, lpBuf, dwToWrite, &dwWritten, NULL)) 
	{
		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;
 return fRes;
}
//--------------------------------------------------------------------
void ReadPort()
{
DWORD dwRead;        
char  chRead[250];  
// remember to adjust 250 according to the total data read
char *count = NULL;
/* flush the old values and fill with some arbitary symbol 
   thats most unlikely to appear in your data to know
   the end point after reading */
	memset(chRead,'\x91',250);  
	Sleep( 5L);
	ReadFile(hComm, chRead, 250, &dwRead, NULL); 
/* now chRead contains data . you can manipulate
   it accoring to your needs now. */
}
//--------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPTSTR    lpCmdLine,
                   int       nCmdShow)
{
	MSG msg;

	// Perform application initialization:
	if (!InitInstance(hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	HACCEL hAccelTable;
	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_COMMDSP));

	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}

//-------------------------------------------------------------------------
void UpdatePrincipaleSS()
{

}
//-------------------------------------------------------------------------
//  FUNCTION: MyRegisterClass()
//  PURPOSE: Registers the window class.
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
	WNDCLASS wc;

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = WndProc;
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = hInstance;
	wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_CommDSP));
	wc.hCursor       = 0;
	wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = szWindowClass;

	return RegisterClass(&wc);
}
//-------------------------------------------------------------------------
//   FUNCTION: InitInstance(HINSTANCE, int)
//   PURPOSE: Saves instance handle and creates main window
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
    HWND hWnd;
    TCHAR szTitle[MAX_LOADSTRING];		// title bar text
    TCHAR szWindowClass[MAX_LOADSTRING];	// main window class name

    g_hInst = hInstance; // Store instance handle in our global variable

    // SHInitExtraControls should be called once during your application's initialization to initialize any
    // of the device specific controls such as CAPEDIT and SIPPREF.
    SHInitExtraControls();

    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 
    LoadString(hInstance, IDC_COMMDSP, szWindowClass, MAX_LOADSTRING);

    //If it is already running, then focus on the window, and exit
    hWnd = FindWindow(szWindowClass, szTitle);	
    if (hWnd) 
    {
        // set focus to foremost child window
        // The "| 0x00000001" is used to bring any owned windows to the foreground and
        // activate them.
        SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
        return 0;
    } 

    if (!MyRegisterClass(hInstance, szWindowClass))
    {
    	return FALSE;
    }

    hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

    if (!hWnd)
    {
        return FALSE;
    }

    // When the main window is created using CW_USEDEFAULT the height of the menubar (if one
    // is created is not taken into account). So we resize the window after creating it
    // if a menubar is present
    if (g_hWndMenuBar)
    {
        RECT rc;
        RECT rcMenuBar;

        GetWindowRect(hWnd, &rc);
        GetWindowRect(g_hWndMenuBar, &rcMenuBar);
        rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
		
        MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

		return TRUE;
}
//-------------------------------------------------------------------------
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//  PURPOSE:  Processes messages for the main window.
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
RECT rc = { 4, 10, 100, 100};
static SHACTIVATEINFO s_sai;
char OutSS[200];
char OutSSadd[200];
wchar_t LOutSS[200];

    switch (message) 
    {
				case WM_COMMAND:
            wmId    = LOWORD(wParam); 
            wmEvent = HIWORD(wParam); 
            // Parse the menu selections:
            switch (wmId)
            {
               case IDM_HELP_ABOUT:
	                    DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
                    break;
               case IDM_CONFIG:
//	                    DialogBox(g_hInst, (LPCTSTR)IDD_CONFIGBOX, hWnd, Config);
                    //--------------------------------------------------------------------
								 if(OpenPort(hWnd, _T("COM7:")))  // opens COM7 port sur le ppc
																									// et je recois sur com7 sur le dongle BT 520
																									// emulateur com1 redirect sur com 1 mais ne marche pas vert BT :-( comm 44
											{
											DCB dcb;
											COMMTIMEOUTS timeouts;
												dcb.DCBlength = sizeof(dcb);
												// first read existing DCB
												GetCommState(hComm, &dcb);
												// diddle the DCB here. See the Library docs on "DCB"

												dcb.fOutxCtsFlow = false; 
												// Disable CTS monitoring
												dcb.fOutxDsrFlow = false;    // Disable DSR monitoring
												dcb.fDtrControl = DTR_CONTROL_DISABLE;    // Disable DTR monitoring
												dcb.fOutX = false;        // Disable XON/XOFF for transmission
												dcb.fInX = false;        // Disable XON/XOFF for receiving
												dcb.fRtsControl = RTS_CONTROL_DISABLE;    // Disable RTS (Ready To Send)
												// send modified DCB back to the port
												SetCommState(hComm, &dcb);

												timeouts.ReadIntervalTimeout = 20; 
												timeouts.ReadTotalTimeoutMultiplier = 10;
												timeouts.ReadTotalTimeoutConstant = 100;
												timeouts.WriteTotalTimeoutMultiplier = 10;
												timeouts.WriteTotalTimeoutConstant = 100;
												Connected=true;
												if (!SetCommTimeouts(hComm, &timeouts))
													{
													// error with time outs .alert user and exit
													}
											}										
										break;
               case IDM_CONTROL_SEND:
								 DialogBox(g_hInst, (LPCTSTR)IDD_PILOTEREFERENCES, hWnd, PiloteReferences);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -