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

📄 rs232u.cpp

📁 Texas Instruments的TUSB3410转串口电路测试应用程序的源码。
💻 CPP
字号:
/* Copyright (C) Jungo 2003 - 2004 */

#include <windows.h>
#include <commctrl.h>
#include <stdlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#include "rs232u.h"
#include "resource.h"
#include "rs232u_lib.h"

HINSTANCE hGInstance = NULL;
BOOL ReadThreadEnd = TRUE;
BOOL ReadThreadActive = FALSE;
WDL_HANDLE handle;
 
/****************************************************************
* Main Windows Application Routine
*****************************************************************/

int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nShowCmd)
{
    INITCOMMONCONTROLSEX ccex;

    hGInstance = hInstance; // store global copy of instance handle

    ccex.dwSize = sizeof(ccex);
    ccex.dwICC = ICC_PROGRESS_CLASS;

    // Initialize the common controls used in this app
    if (!InitCommonControlsEx(&ccex))
    {
        MessageBox(NULL, "Failed to Initialize Application", "Failure!",
            MB_ICONSTOP);
        return EXIT_FAILURE;
    }

    if (DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL,
        (DLGPROC)DlgProc)==-1)
    {
        MessageBox(NULL, "Failed to create main dialog box.", "Failure!",
            MB_ICONSTOP);
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

BOOL CALLBACK DlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static HWND hListBox;
    static HWND hSpeedList;
    WDL_STATUS status;
    char sMsg[200];
    char buf[512];

    switch(uMsg)
    {
    case WM_INITDIALOG:
    {
        WDL_CALLBACK_OPS ops;
        int i;

        hSpeedList = GetDlgItem(hwndDlg, IDC_SPEED_COMBO); 
        for (i = 0; i < 6; i++) 
            SendMessage(hSpeedList, CB_INSERTSTRING, i, (LPARAM) szBaudRate[i]); 
        SendMessage(hSpeedList, CB_SETCURSEL, 0, 0);
            
        hListBox = GetDlgItem(hwndDlg, IDC_MSGBOX);

        SendMessage(hListBox, LB_INSERTSTRING, 0, (LPARAM) "USB HID Monitor");

        status = WDL_Init(&handle, VID, PID, (void *)hwndDlg, "");
        if (status)
        {
            sprintf(sMsg, "WDL_Init() failed with status 0x%x - %s", status,
                WDL_Stat2Str(status));
            SendMessage(hListBox, LB_INSERTSTRING, 0, (LPARAM)sMsg);
            break;
        }
        ZeroMemory(&ops, sizeof(ops));
        ops.cbConnect = OnConnect;
        ops.cbDisconnect = OnDisconnect;
        status = WDL_SetNotificationCallback(handle, &ops);
        if (status)
        {
            sprintf(sMsg, "WDL_SetNotificationCallback() failed with "
                "status 0x%x - %s", status, WDL_Stat2Str(status));
            SendMessage(hListBox, LB_INSERTSTRING, 0, (LPARAM)sMsg);
            break;
        }

        if (WDL_IsAttached(handle)==WDL_SUCCESS)
        {
            SendMessage(hListBox, LB_INSERTSTRING, 0,
                (LPARAM)"USB Device Located!");
            OnConnect(handle, (void *)hwndDlg);
        }
        break;
    }

    case WM_COMMAND:
    {
        if (LOWORD(wParam) == IDC_SEND_BUTTON)
        {
            int bytes = GetDlgItemText(hwndDlg, IDC_MSGBOX_OUT, buf, sizeof(buf));
            status = RS232U_Write(handle, buf, bytes);
            if (status)
            {
                sprintf(sMsg, "RS232U_Write() failed with status 0x%x - %s ",
                    status, WDL_Stat2Str(status));
                MessageBox(NULL, sMsg, "Failed to write the text", MB_ICONSTOP);
            }
        }
        else if (LOWORD(wParam) == IDC_SPEED_COMBO && 
            HIWORD(wParam) == CBN_SELCHANGE)
        {
            int i;
            
           // int bytes = GetDlgItemText(hwndDlg, IDC_SPEED_COMBO, buf, sizeof(buf));

            /*
            for (i = 0; i < 6; i++)
                if (!strcmp(buf, szBaudRate[i]))
                    break;
                    */
            i = SendMessage((HWND)lParam, CB_GETCURSEL, 0, 0);
                    
            WDL_STATUS status = RS232U_SetSpeed(handle, (BYTE)i);
            if (status)
            {
                sprintf(sMsg, "RS232U_SetSpeed() failed with status 0x%x - %s ",
                    status, WDL_Stat2Str(status));
                MessageBox(NULL, sMsg, "Failed to set the speed", MB_ICONSTOP);
            }
        }
        else if (LOWORD(wParam) == IDC_HW_CHECK && HIWORD(wParam) == BN_CLICKED)
        {
            WPARAM iVal = SendMessage((HWND)lParam, BM_GETCHECK, 0, 0);     

            WDL_STATUS status = RS232U_SetHWFlowControl(handle, (iVal == BST_CHECKED));
            if (status)
            {
                sprintf(sMsg, "RS232U_SetHWFlowControl() failed with status 0x%x - %s ",
                    status, WDL_Stat2Str(status));
                MessageBox(NULL, sMsg, "Failed to set the hardware flow control", MB_ICONSTOP);

                WPARAM iOppositeVal = (iVal == BST_CHECKED) ? BST_UNCHECKED : BST_CHECKED;
                SendMessage((HWND)lParam, BM_SETCHECK, iOppositeVal, 0);
            }
        }
        break;
    }

    case WM_CLOSE:
        ReadThreadEnd = TRUE;

        if (ReadThreadActive)
        {
            PostMessage(hwndDlg, WM_CLOSE, 0, 0);
            break;
        }
        WDL_Close(handle);

        EndDialog(hwndDlg, 0);
        break;

    default:
        break;
    }
    return FALSE;
}

void ReadThread(void *context)
{
    HWND hListBox;
    size_t num_read;
    unsigned char msg[MAX_REPORT_LEN-2+1]; // +1 = null-terminated

    ReadThreadActive = TRUE;

    hListBox = GetDlgItem((HWND)context, IDC_MSGBOX);

    SendMessage(hListBox, LB_INSERTSTRING, 0, (LPARAM)"Read thread started");

    while (!ReadThreadEnd)
    {
        if (RS232U_Read(handle, msg, &num_read)==WDL_SUCCESS)
        {
            msg[num_read] = '\0';
            SendMessage(hListBox, LB_INSERTSTRING, 0, (LPARAM)msg);
        }
    }

    SendMessage(hListBox, LB_INSERTSTRING, 0, (LPARAM)"Read thread ended");

    ReadThreadActive = FALSE;
}

// Connect callback
void __cdecl OnConnect(WDL_HANDLE handle, void *context)
{
    char sMsg[200];
    WPARAM wNewCheckedVal = BST_CHECKED;

    HWND hListBox = GetDlgItem((HWND)context, IDC_MSGBOX);
    SendMessage(hListBox, LB_INSERTSTRING, 0, (LPARAM)"Connected!");

    WDL_STATUS status = RS232U_SetSpeed(handle, 0);
    if (status)
    {
        sprintf(sMsg, "RS232U_SetSpeed() failed with status 0x%x - %s ",
            status, WDL_Stat2Str(status));
        MessageBox(NULL, sMsg, "Failed to set the speed", MB_ICONSTOP);
    }

    HWND hHWFlowCtrlCheckBox = GetDlgItem((HWND)context, IDC_HW_CHECK);

    status = RS232U_SetHWFlowControl(handle, TRUE);
    if (status)
    {
        sprintf(sMsg, "RS232U_SetHWFlowControl() failed with status 0x%x - %s ",
            status, WDL_Stat2Str(status));
        MessageBox(NULL, sMsg, "Failed to set the hardware flow control", MB_ICONSTOP);
        wNewCheckedVal = BST_UNCHECKED;
    }

    SendMessage(hHWFlowCtrlCheckBox, BM_SETCHECK, wNewCheckedVal, 0);

    ReadThreadEnd = FALSE;
    _beginthread(ReadThread, 0, context);
}

// Disconnect callback
void __cdecl OnDisconnect(WDL_HANDLE handle, void *context)
{
    HWND hListBox = GetDlgItem((HWND)context, IDC_MSGBOX);

    SendMessage(hListBox, LB_INSERTSTRING, 0, (LPARAM)"Disconnected!");
    ReadThreadEnd = TRUE;
}

⌨️ 快捷键说明

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