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

📄 commport.c

📁 Advantech Driver Demo: Com Port 通过com来控制输出
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 ***********************************************************************
 *  Program     : COMMPORT.C                                           *
 *  Description : Demo program for COM port functions                  *
 *  Revision    : 1.00                                                 *
 *  Date        : 9/5/1996                    Advantech Co., Ltd.      *
 ***********************************************************************
 *
 * FUNCTIONS:
 *
 *   InitApplication() - Initializes window data and registers window class
 *
 *   InitInstance() - Saves instance handle and creates main window
 *
 *   MainWndProc() - Process main window messages
 *
 *   ConfigDlgProc() - Process the dialog box messages for configuration
 *
 *   CommandDlgProc() - Process the dialog box messages for read/write
 *
 *   AddControlString() - initialize combobox
 *
 *   SetHardwareSettings() - initialize dialog box
 */

#include <windows.h>
#include <stdlib.h>
#include <dos.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "..\..\..\include\driver.h"
#include "resource.h"

// ------------------------------------------------------------------------
// CONSTANT DEFINITION
// ------------------------------------------------------------------------
#define     TIMEOUT     100

// ------------------------------------------------------------------------
// GLOBAL VARIABLES
// ------------------------------------------------------------------------
HANDLE hInst;                           // current instance
HWND   hMainWnd;                        // main window handle

char      szErrMsg[80];                 // use for MESSAGEBOX function
LRESULT   ErrCde;                       // return error code
LONG      DeviceHandle;                 // device Handle
DEVCONFIG_COM ComCfg;                   // communication port configuration
char      TerminateChar = 0x0d;         // terminate character
HANDLE    hComm;                        // handle of COM port

FARPROC   lpfnConfigDlgProc;            // config. dialog procedure
FARPROC   lpfnCommandDlgProc;           // read/write dialog procedure

char      szBuffer[40];
char     *SpeedString[] =
    {"300", "600", "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", NULL};
char     *DataBitsString[] =
    {"8", "7", "6", "5", "4", NULL};
char     *ParityString[] =
    {"NONE", "EVEN", "ODD", "MARK", NULL};
char     *StopBitString[] =
    {"1", "1.5", "2", NULL};
char     *TxModeString[] =
    {"RS-232", "RTS Control", NULL};

// ------------------------------------------------------------------------
// FUNCTION DECLARATION
// ------------------------------------------------------------------------
BOOL InitApplication(HANDLE hInstance);
BOOL InitInstance(HANDLE hInstance, int nCmdShow);
long FTYPE MainWndProc(HWND, UINT, WPARAM, LPARAM);
int  FTYPE WinMain(HANDLE hInstance, HANDLE hPrevInstance,
     LPSTR lpCmdLine, int nCmdShow);
BOOL FTYPE ConfigDlgProc(HWND, unsigned, WPARAM, LPARAM);
BOOL FTYPE CommandDlgProc(HWND, unsigned, WPARAM, LPARAM);
WORD CvtHex(char *str);
PRIVATE int Power(int,int);
PRIVATE void AddControlString(HWND);
PRIVATE void SetHardwareSettings(HWND,DEVCONFIG_COM FAR *);
PRIVATE int StringToDec(char *,int);

//--------------------------------------------------------------------------
// FUNCTION     : WinMain
//
// PURPOSE      : calls initialization function, processes message loop
//--------------------------------------------------------------------------
int FTYPE WinMain
(
    HANDLE hInstance,                 // current instance
    HANDLE hPrevInstance,             // previous instance
    LPSTR lpCmdLine,                  // command line
    int nCmdShow                      // show-window type (open/icon)
)
{
    MSG msg;                               /* message                      */

    if (!hPrevInstance)                /* Other instances of app running?  */
      if (!InitApplication(hInstance)) /* Initialize shared things         */
          return (FALSE);              /* Exits if unable to initialize    */

    // Perform initializations that apply to a specific instance
    if (!InitInstance(hInstance, nCmdShow))
        return (FALSE);

    // Acquire and dispatch messages until a WM_QUIT message is received.
    while (GetMessage(&msg,      /* message structure                      */
        (HWND)NULL,              /* handle of window receiving the message */
        (UINT)NULL,              /* lowest message to examine              */
        (UINT)NULL))             /* highest message to examine             */
    {
        TranslateMessage(&msg);  /* Translates virtual key codes           */
        DispatchMessage(&msg);   /* Dispatches message to window           */
    }
    return (msg.wParam);         /* Returns the value from PostQuitMessage */
}

//--------------------------------------------------------------------------
// FUNCTION     : InitApplication
//
// PURPOSE      : Initializes window data and registers window class
//--------------------------------------------------------------------------
BOOL InitApplication
(
    HANDLE      hInstance                    // current instance
)
{
    WNDCLASS  wc;

    // Fill in window class structure with parameters that describe the
    // main window.
    wc.style = CS_HREDRAW | CS_VREDRAW; /* Class style(s).                 */
    wc.lpfnWndProc = (WNDPROC)MainWndProc;  /* window process procedure    */
    wc.cbClsExtra = 0;                  /* No per-class extra data.        */
    wc.cbWndExtra = 0;                  /* No per-window extra data.       */
    wc.hInstance = hInstance;           /* Application that owns the class.*/
    wc.hIcon = LoadIcon(hInstance, "IDI_ICON1");
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName =  "MyMenu";   /* Name of menu resource in .RC file.  */
    wc.lpszClassName = "MyClass";  /* Name used in call to CreateWindow. */

    // Register the window class and return success/failure code.
    return (RegisterClass(&wc));

}

//--------------------------------------------------------------------------
// FUNCTION     : InitInstance
//
// PURPOSE      : saves instance handle and creates main window
//--------------------------------------------------------------------------
BOOL InitInstance
(
    HANDLE      hInstance,          // Current instance identifier.
    int         nCmdShow            // Param for first ShowWindow() call.
)
{

    // Save the instance handle in static variable, which will be used in
    // many subsequence calls from this application to Windows.
    hInst = hInstance;

    // Create a main window for this application instance.
    hMainWnd = CreateWindow(
        "MyClass",                  /* See RegisterClass() call.           */
        "Advantech Driver Demo : COM Port" ,          /* Window title bar  */
        WS_OVERLAPPEDWINDOW,        /* Window style.                       */
        CW_USEDEFAULT,              /* Default horizontal position.        */
        CW_USEDEFAULT,              /* Default vertical position.          */
        CW_USEDEFAULT,              /* Default width.                      */
        CW_USEDEFAULT,              /* Default height.                     */
        NULL,                       /* Overlapped windows have no parent.  */
        NULL,                       /* Use the window class menu.          */
        hInstance,                  /* This instance owns this window.     */
        NULL                        /* Pointer not needed.                 */
        );

    // If window could not be created, return "failure"
    if (!hMainWnd)
        return (FALSE);

    // Make the window visible; update its client area; and return "success"

    ShowWindow(hMainWnd, nCmdShow);     // Show the window
    UpdateWindow(hMainWnd);             // Sends WM_PAINT message
    return (TRUE);              // Returns the value from PostQuitMessage
}


//--------------------------------------------------------------------------
// FUNCTION     : MainWndProc
//
// PURPOSE      : the main window function, which
//                does the message processing, in response to
//
//                WM_CREATE     - create window
//                WM_COMMAND    - application menu
//                WM_DESTROY    - destroy window
//
// PARAMETERS   : hWnd (IN) - window handle
//                messag (IN) - message ID
//                wParam (IN) - related to msg
//                lParam (IN) - related to msg
//--------------------------------------------------------------------------
long FTYPE MainWndProc
(
    HWND        hWnd,                          // window handle
    unsigned    message,                       // type of message
    WPARAM      wParam,                        // additional information
    LPARAM      lParam                         // additional information
)
{
    static  HANDLE  hInstance ;

    switch (message)
    {
    case WM_CREATE:

        hInstance = ((LPCREATESTRUCT) lParam)->hInstance;

        lpfnConfigDlgProc = MakeProcInstance (ConfigDlgProc, hInstance);

        lpfnCommandDlgProc = MakeProcInstance (CommandDlgProc, hInstance);

        // initialize default settings
        ComCfg.usCommPort = 2;
        ComCfg.dwBaudRate = 9600;        // baud rate 9600
        ComCfg.usParity = 0;             // none parity check
        ComCfg.usDataBits = 8;           // 8 data bits
        ComCfg.usStopBits = 0;           // 1 stop bit
        ComCfg.usTxMode = 0;             // RS-232 mode
        ComCfg.usPortAddress = 0x3f8;    // default port address

        return 0;

    case WM_COMMAND:     /* message: from application menu */

        switch (LOWORD(wParam))
        {
        case IDM_CONFIG :

            DialogBox (hInstance, "CONFIG", hWnd, lpfnConfigDlgProc) ;
            return 0;

        case IDM_COMMAND :

            DialogBox (hInstance, "COMMAND", hWnd, lpfnCommandDlgProc) ;
            return 0;
        }

        return 0;

    case WM_DESTROY:

        PostQuitMessage(0);
        break;

    default:
        return (DefWindowProc(hWnd, message, wParam, lParam));
    }

    return ((LONG)NULL);
}


//--------------------------------------------------------------------------
// FUNCTION     : ConfigDlgProc
//
// PURPOSE      : the window function for "CONFIG" windows, which
//                does the message processing, in response to
//
//                WM_INITDIALOG     : Setups the dialog box state
//                WM_COMMAND        : Passes control to a command-handling
//                                    function.
//
// PARAMETERS   : hDlg (IN) - window handle
//                msg (IN) - message ID
//                wParam (IN) - related to msg
//                lParam (IN) - related to msg
//
// RETURNS      : TRUE if processing
//                FALSE if not processing
//--------------------------------------------------------------------------
BOOL FTYPE ConfigDlgProc
(
    HWND        hDlg,                          // window handle
    unsigned    msg,                           // type of message
    WPARAM      wParam,                        // additional information
    LPARAM      lParam                         // additional information
)
{
	long dwIndex;

	switch (msg)
	{
	case WM_INITDIALOG:

		// Initialize Dialog box
		AddControlString(hDlg);

		// set data to dialog box
		SetHardwareSettings(hDlg,&ComCfg);

		break;

	case WM_COMMAND:
		switch (LOWORD(wParam))
		{
		case IDOK:
			// get communication port
			if (SendDlgItemMessage( hDlg, IDC_COMPORT, EM_GETMODIFY, 0, 0))
			{
				SendDlgItemMessage( hDlg, IDC_COMPORT, WM_GETTEXT, 20,
                    (LPARAM) ((LPSTR)szBuffer));
				ComCfg.usCommPort = (WORD)atoi(szBuffer);
				SendDlgItemMessage( hDlg, IDC_COMPORT, EM_SETMODIFY, FALSE, 0);
			}

			// get baud rate
			SendDlgItemMessage(hDlg, IDC_BDRATE,
                    WM_GETTEXT, 20, (LPARAM) ((LPSTR)szBuffer));
			ComCfg.dwBaudRate = (DWORD)atol(szBuffer);

			// get parity
			dwIndex = SendDlgItemMessage(hDlg, IDC_PARITY, CB_GETCURSEL, 0, 0);
			if (dwIndex != CB_ERR)
				ComCfg.usParity = (unsigned short)dwIndex;

			// get data bits
			SendDlgItemMessage(hDlg, IDC_DATABITS,
                    WM_GETTEXT, 20, (LPARAM) ((LPSTR)szBuffer));
			ComCfg.usDataBits = (unsigned short)atoi(szBuffer);

			// get stop bits
			dwIndex = SendDlgItemMessage(hDlg, IDC_STOPBITS, CB_GETCURSEL, 0, 0);
			if (dwIndex != CB_ERR)
				ComCfg.usStopBits = (unsigned short)dwIndex;

			// get transmission mode
			dwIndex = SendDlgItemMessage(hDlg, IDC_TXMODE, CB_GETCURSEL, 0, 0);
			if (dwIndex != CB_ERR)
				ComCfg.usTxMode = (unsigned short)dwIndex;

			// get communication port base
			if (SendDlgItemMessage( hDlg, IDC_BASE, EM_GETMODIFY, 0, 0))

⌨️ 快捷键说明

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