ARM USB 通信(转)

ARM USB 通信

采用ZLG的动态链接库,动态装载。

ARM是Context-M3-1343。

在C++ Builder 6 中开发的上位机通信软件。

USB通信代码如下:

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//#include "zyUSBDev.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
    unsigned char sendbuf[1024],recbuf[1024];
    int i;

    unsigned char Inbyte;
    for (i = 0; i < sizeof(sendbuf); i++)
    {
        sendbuf[i] = Inbyte;                     //16进制字节放进发送缓冲区4096字节
        recbuf[i] = 0;                             //清空接收缓冲区
    }

    /*******************  下面开始使用动态库 ***********************/
    int ret;
    AnsiString m_Disp = " china";
    unsigned char cmd[2],ack;
    cmd[0] = sizeof(sendbuf) / 256;                 //需要发送的字节数量的长度的高 8 位
    cmd[1] = sizeof(sendbuf) % 256;                 //                          低 8 位

    HINSTANCE handle;
    FARPROC lpFarProc;
    int __stdcall (*pFun)(int,unsigned char*,int,int);//change

    handle = LoadLibrary("zyUSBDev.dll");
    lpFarProc = GetProcAddress(handle, "zyUSB_ReadData");
    pFun = (int(__stdcall*)(int,unsigned char*,int,int))lpFarProc;  //__cdecl

    //第五步: 用逻辑端点2, 接收来自 USB 设备的大量数据
    ret = pFun(2, recbuf, sizeof(sendbuf), 1000);
    if (ret != sizeof(sendbuf))
    {
        if (ret <= 0)
        {
            m_Disp = "逻辑端点 2 接收数据错误: 未接收到有效数据。";
        }
        else if (ret > 0)
        {
            m_Disp = "逻辑端点 2 接收数据错误: 接收到 部分数据。";
        }

        // MessageBox("逻辑端点 2 接收数据错误");
        return;
    }

    //显示收到的字节
    m_Disp = "";
    int temp = 0;
    temp = recbuf[0] + recbuf[1] * 256;
    m_Disp= IntToStr(temp) + " ";
    for (i = 0; i < sizeof(sendbuf); i++)
        m_Disp = m_Disp + recbuf[i] + " ";

    Memo1->Text = m_Disp;
    FreeLibrary(handle);
}
//---------------------------------------------------------------------------
原文地址:https://www.cnblogs.com/LittleTiger/p/11962669.html