标题:读写显存编辑错误问题
只看楼主
nmgbtzyf
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2020-12-1
结帖率:0
 问题点数:0 回复次数:4 
读写显存编辑错误问题
这是链接https://zhidao.baidu.com/question/1451479255163995780.html?device=mobile&ssid=0&from=1012852q&uid=0&pu=usm@2,sz@1320_1004,ta@iphone_2_10.0_11_12.2&bd_page_type=1&baiduid=FC7582F63E35AAB9C4D9A3BE3827BE04&tj=www_zhidao_normal_1_0_10_p1
我用vs2010编辑错误
哪位师傅帮个  给个调通的实例
搜索更多相关主题的帖子: mobile 错误 编辑 from 读写 
2022-01-19 13:16
nmgbtzyf
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2020-12-1
得分:0 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
 
#include "libusb.h"
 
#define VID 0x8888
#define PID 0x0088
 
#define edp2in 0x82
#define edp2out 0x02
 
int main(void)
{
    libusb_device **devs, *dev;
    int ret, i;
    ssize_t cnt;
    usb_pro_t usb_pro;
    struct libusb_device_handle *handle = NULL;
    libusb_context *ctx = NULL;
 
    ret = libusb_init(&ctx);
    if (ret < 0)
        return -1;
 
    libusb_set_debug(ctx, 3);
 
    cnt = libusb_get_device_list(NULL, &devs);
    if (cnt < 0) {
        printf("no usb dev on bus\r\n");
        return  -1;
    }
 
    i = 0;
    while((dev = devs[i++]) != NULL) {
 
        ret = libusb_get_device_descriptor(dev,&desc);
        if (ret < 0) {
            printf("failed to get device descriptor");
            goto error;
        }
 
        if ((desc.idVendor == VID) && (desc.idProduct == PID)) {
            printf("bLength: 0x%04x\r\n", desc.bLength);
            printf("bDescriptorType: 0x%04x\r\n"
hts Reserved



stm32 usb通信上位机

30


peixiuhui
关注
STM32 USB 上位机程序实现 转载
2017-08-08 12:17:43
 1点赞
 
peixiuhui  

码龄14年

关注
libusb 介绍
libusb是开源的C库,使用该库是的用户可以在应用程序中直接访问 USB 设备,无需为 USB 设备编写内核驱动。libusb支持多个平台 (Linux, window, iOS),所以可以很方便地将应用程序移植到其他平台。

linux libusb 安装
从网上下载libusb的源码,下载地址:http://www., 下载后编译安装。

# tar jxvf libusb-1.0.20.tar.bz2
 
# cd libusb-1.0.20
# ./configure
# make
# sudo make install
1
2
3
4
5
6
1
2
3
4
5
6
ubuntu下可以通过以下命令快速安装。

sudo apt-get isntall libusb*
1
1
安装后,libusb的头文件被安装在/usr/local/include/libusb-1.0 ,链接库被安装在/usr/loacal/lib目录下。

usb bulk 传输例程
这个例程演示如何使用 libusb 库,编写 USB bulk xfer 上位机demo,可以正常接收和发送数据。注意,修改程序中的 VID 和 PID 的值和你 device 板子上所定义的一致,传输数据块的大小不要超过 device 定义的最大传输长度。

 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
 
#include "libusb.h"
 
#define VID 0x8888
#define PID 0x0088
 
#define edp2in 0x82
#define edp2out 0x02
 
int main(void)
{
    libusb_device **devs, *dev;
    int ret, i;
    ssize_t cnt;
    usb_pro_t usb_pro;
    struct libusb_device_handle *handle = NULL;
    libusb_context *ctx = NULL;
 
    ret = libusb_init(&ctx);
    if (ret < 0)
        return -1;
 
    libusb_set_debug(ctx, 3);
 
    cnt = libusb_get_device_list(NULL, &devs);
    if (cnt < 0) {
        printf("no usb dev on bus\r\n");
        return  -1;
    }
 
    i = 0;
    while((dev = devs[i++]) != NULL) {
 
        ret = libusb_get_device_descriptor(dev,&desc);
        if (ret < 0) {
            printf("failed to get device descriptor");
            goto error;
        }
 
        if ((desc.idVendor == VID) && (desc.idProduct == PID)) {
            printf("bLength: 0x%04x\r\n", desc.bLength);
            printf("bDescriptorType: 0x%04x\r\n", desc.bDescriptorType);
            printf("bcdUSB: 0x%04x\r\n", desc.bcdUSB);
            printf("bDeviceClass: 0x%04x\r\n", desc.bDeviceClass);
            printf("bDeviceSubClass: 0x%04x\r\n", desc.bDeviceSubClass);
            printf("bDeviceProtocol: 0x%04x\r\n", desc.bDeviceProtocol);
            printf("bMaxPacketSize0: 0x%04x\r\n", desc.bMaxPacketSize0);
            printf("vendor id: 0x%04x\r\n", desc.idVendor);
            printf("product id: 0x%04x\r\n", desc.idProduct);
            printf("bcdDevice: 0x%04x\r\n", desc.bcdDevice);
            printf("iManufacturer: 0x%04x\r\n", desc.iManufacturer);
            printf("iProduct: 0x%04x\r\n", desc.iProduct);
            printf("iSerialNumber: 0x%04x\r\n", desc.iSerialNumber);
            printf("bNumConfigurations: 0x%04x\r\n", desc.bNumConfigurations);
 
        }
 
    }
 
    handle = libusb_open_device_with_vid_pid(ctx, VID, PID);
 
    if (handle == NULL) {
        printf("cant't open device\r\n");
        goto error;
    } else {
        printf("open device\r\n");
    }
 
    libusb_free_device_list(devs, 1);
 
    if (libusb_kernel_driver_active(handle, 0) ==1) {
        printf("kernel driver active, detach it \r\n");
 
        if (libusb_detach_kernel_driver(handle, 0) == 0) {
            printf("detached kernel driver\r\n");
        }
        else {
            goto error;
        }
    }
 
    ret = libusb_claim_interface(handle, 0);
    if (ret < 0) {
        printf("can't claim interface\r\n");
        goto error;
    } else {
        printf("claimed interface\r\n");
    }
 
    char data[64];
    int actual_len = 0;
    int didi = 1000;
    for (int i = 0; i< 1000; i++) {
        memset(data, 0, sizeof(data));
        /*  receive data from device  */
        /*
        ret = libusb_bulk_transfer(handle, edp2in, data, 64, &actual_len, 0);
 
        if (actual_len = 0) {
            printf("received nothing\r\n");
        } else {
            printf("bulk transfer: %s\r\n", data);
        }
 
        usleep(200000);
        */
 
        char *str = "am host";
 
        sprintf(data, "am host %d\r\n", i);
 
        ret = libusb_bulk_transfer(handle, edp2out, data, strlen(data), &actual_len, 0);
 
        if (actual_len != 0) {
            printf("send data: %s\r\n", data);
        }
 
        usleep(200000);
    }
 
    libusb_close(handle);
 
error:
 
    printf("free device list\r\n");
    libusb_free_device_list(devs, 1);
 
    libusb_exit(NULL);
 
    return 0;
2022-01-23 05:06
nmgbtzyf
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2020-12-1
得分:0 
#include <stdio.h>
#include <string.h>
#include <iostream>
#include "conio.h"
#include "lusb0_usb.h"
#pragma comment(lib, "libusb.lib")
 
#define m_dev_VID    0x1483        /* Vendor ID of the m_dev         */
#define m_dev_PID    0x5751        /* Product ID of the m_dev        */
#define EP1_OUT_SIZE    63          //可根据设备修改大小
#define EP1_IN_SIZE    63
 
int main(int argc, char *argv[])
{  
        struct usb_device * m_dev = NULL;
        struct usb_dev_handle *m_dev_handle;
        char str[64];
    memset(str,0,sizeof(str));
 
        usb_init();
        usb_find_busses();
        usb_find_devices();
        
    struct usb_bus *bus;
        for(bus = usb_get_busses(); bus; bus = bus->next)
        {
                struct usb_device *dev;
                for(dev = bus->devices; dev; dev = dev->next)
                {
                        if(dev->descriptor.idVendor == m_dev_VID && dev->descriptor.idProduct == m_dev_PID)
                        m_dev=dev;
                }
      
2022-01-23 16:12
nmgbtzyf
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2020-12-1
得分:0 
T, All Rights Reserved



usb

30

使用libusb-win32库, 批量(bulk)传输方式, 编写上位机软件 原创
2017-03-24 22:21:46
 7点赞
 
Zhuang_Zimo  

码龄5年

关注
关键词: libusb, bulk批量传输, 上位机软件.

公司的一个项目中负责USB驱动SDK设计, 做一个USB上位机软件, 要求使用的传输方式是bulk传输. 以前从没接触过这方面的东西, 可以说完全是个外行, 到现在为止搞了将近一个月, 总算出来了那么一点成果. 在这里, 总结分享一下.

 

~>USB上位机程序的编写

      1.下载libusb-win32的项目源码(libusb-win32-bin-1.2.6.0), 里面包含.lib库和.h头文件, 以及inf-wizard.exe和testlibusb-win.exe.

在PC端插上USB设备后, 运行inf-wizard.exe, 即可自动生成.inf 驱动文件, 不用自己编写驱动, 就可完成驱动的安装.
运行testlibusb-win.exe, 即可查看设备的信息, 包括: VID/PID,以及各种描述符, 端点地址Endpoint Address, 最大包的大小MaxPacketSize.
程序代码中加入 : #include "lusb0_usb.h"       以及      #pragma comment(lib, "libusb.lib")          //即可调用libusb的函数
函数的调用过程.
          1) 调用 void usb_init(void); 进行初始化

          2) 调用usb_find_busses、usb_find_devices和usb_get_busses这三个函数,获得已找到的USB总线序列;然后通过链表遍历所有的USB设备,根据已知的要打开USB设备的ID(VID/PID),找到相应的USB设备.

          3) 调用usb_open函数打开该USB设备

          4) usb_set_configuration(m_dev_handle, 1)   //设置配置

        intint usb_claim_interface(usb_dev_handle *dev, int interface);   

         注册与操作系统通信的接口,这个函数必须被调用,因为只有注册接口,才能做相应的操作。

         

          5)与USB设备进行通信。使用函数

        int usb_bulk_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);//批量写入

        int usb_bulk_read(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout);//批量读取

           从USB设备读取(接收)数据或向其写入(发送)数据。

 

          6) int usb_release_interface(usb_dev_handle *dev, int interface);

          注销被usb_claim_interface函数调用后的接口,释放资源,和usb_claim_interface对应使用。

          8)  int usb_close(usb_dev_handle *dev);

          与usb_open相对应,关闭设备,是必须调用的, 返回0成功,<0 失败。

 

 

附上最简单的win32控制台程序的代码:(配合Bus Hound运行起来更直观).

 

 

#include <stdio.h>
#include <string.h>
#include <iostream>
#include "conio.h"
#include "lusb0_usb.h"
#pragma comment(lib, "libusb.lib")
 
#define m_dev_VID    0x1483        /* Vendor ID of the m_dev         */
#define m_dev_PID    0x5751        /* Product ID of the m_dev        */
#define EP1_OUT_SIZE    63          //可根据设备修改大小
#define EP1_IN_SIZE    63
 
int main(int argc, char *argv[])
{  
        struct usb_device * m_dev = NULL;
        struct usb_dev_handle *m_dev_handle;
        char str[64];
    memset(str,0,sizeof(str));
 
        usb_init();
        usb_find_busses();
        usb_find_devices();
        
    struct usb_bus *bus;
        for(bus = usb_get_busses(); bus; bus = bus->next)
        {
                struct usb_device *dev;
                for(dev = bus->devices; dev; dev = dev->next)
                {
                        if(dev->descriptor.idVendor == m_dev_VID && dev->descriptor.idProduct == m_dev_PID)
                        m_dev=dev;
                }
        }
        if(!m_dev)
        {
                printf("m_dev not found\n");
                return 1;
        }
  
        m_dev_handle = usb_open(m_dev);
        if(!m_dev_handle)
        {
            printf("Could not open m_dev\n");
            return 1;
        }
  
        printf("设备打开成功!\n");
        if(usb_set_configuration(m_dev_handle, 1) < 0)
        {
                printf("Could not set configuration\n");
                usb_close(m_dev_handle);
                return 1;
        }
 
        if(usb_claim_interface(m_dev_handle, 0) < 0) //claim_interface 0 注册与操作系统通信的接口 0
        {
                printf("Could not claim interface\n");
                usb_close(m_dev_handle);
                return 1;
        }
 
 
    //-----获取端点的地址-----------------------------
    int ep = m_dev->config->interface->altsetting->endpoint->bEndpointAddress;
    int EP_IN = 0;
    int EP_OUT = 0;
    if (ep > 0x0f)
    {
        EP_IN = ep;
        EP_OUT = ep - 0x80;
    }
    else
    {
        EP_OUT = ep;
        EP_IN = ep + 0x80;
    }
        
    printf("EP_IN: 0x%02x , EP_OUT: 0x%02x \n", EP_IN, EP_OUT);
    //------------------------------------------------------------
        
    char WriteTestData[2048] = {0};
    char ReadTestData[2048] = {0};
    for(int i = 0; i< 2048; i++)
    {
        WriteTestData[i] = i;
    }
        
        //端点1写入数据
        int ret;
    ret = usb_bulk_write(m_dev_handle, EP_OUT, WriteTestData, EP1_OUT_SIZE, 500);
    if(ret != EP1_OUT_SIZE)
    {
        printf("端点1写入数据失败! %d\n", ret);
        return 1;
    }
    else
    {
        printf("端点1写入数据成功!\n");
    }
 
    //端点1读取数据
    ret = usb_bulk_read(m_dev_handle, EP_IN, ReadTestData, EP1_IN_SIZE, 500);
    if(ret != EP1_IN_SIZE)
    {
        printf("端点1读取数据失败! %d\n", ret);
        return 1;
    }
    else
    {
        printf("端点1读取数据成功!\n");
        for (int i = 0; i < EP1_IN_SIZE; i++)
        {
            printf("%02X ", ReadTestData[i]);
            if(((i + 1) % 16) == 0)
            {
                printf("\n");
            }
        }
        printf("\n");
    }
 
 
    /**************************************************************************/
 
        usb_release_interface(m_dev_handle, 0);        //注销接口,释放资源,和usb_claim_interface搭配使用。
        usb_close(m_dev_handle);
        printf("\n设备关闭\n");
 
        return 0;
}
 
2022-01-23 16:13
nmgbtzyf
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2020-12-1
得分:0 
directdraw直接读写显存的方法
#include "stdafx.h"
#include <ddraw.h>
#include <iostream>
#include <afxwin.h>//òòÎaòaóÃμ½CStringËùòÔòa°üo¬Õa¸öí·Îļt
#include "winsock2.h "
using namespace std;

LPDIRECTDRAW lpDD;

LPDIRECTDRAWSURFACE lpDDSPrimary;

LPDIRECTDRAWSURFACE lpDDSBack;

int main(int argc, char* argv[])
{
DDSURFACEDESC t;
HWND desktop;
desktop = GetDesktopWindow();
HDC hDC = ::GetDC(desktop);

DDSURFACEDESC ddsd;
DDSCAPS ddscaps;
HRESULT ddrval;

BYTE *Bitmap;

ddrval = DirectDrawCreate( NULL, &lpDD, NULL );
if( ddrval != DD_OK )
{
printf("error/n");
}
ddrval = lpDD->SetCooperativeLevel(desktop, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );
if( ddrval != DD_OK )
{
lpDD->Release();
printf("error/n");
}
ddrval = lpDD->SetDisplayMode( 1280, 800, 32);
if( ddrval != DD_OK )
{
lpDD->Release();
printf("error/n");
}
memset( &ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
ddrval = lpDD->CreateSurface( &ddsd, &lpDDSPrimary, NULL );
if( ddrval != DD_OK )
{
lpDD->Release();
printf("error/n");
}
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddrval = lpDDSPrimary->GetAttachedSurface(&ddscaps, &lpDDSBack);
if( ddrval != DD_OK )
{
lpDDSPrimary->Release();
lpDD->Release();
printf("error/n");
}
memset(&ddsd, 0, sizeof(ddsd) );
ddsd.dwSize = sizeof( ddsd );
ddrval = lpDDSPrimary->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
Bitmap = (BYTE*)ddsd.lpSurface;
while(1){
int pos;
for (int y=0;y<480; y++)
{
pos=y*ddsd.lPitch;
for (int x=0; x<640; x++)
{
Bitmap[pos] =25; //R
pos++;
Bitmap[pos] =25; //G
pos++;
Bitmap[pos] =5; //B
pos+=2;
}
}
lpDDSPrimary->Unlock(&ddsd);//unlock
}
//printf("%d/n", ddsd.dwWidth);
return 0;
}

这是俺调试成功的代码,通过查阅大量不成体系的资料,最后终于可以随心所欲的往显存里面写东西了,当然现在这个代码的含义我也不清楚,但达到目的就可以了,我的笔记本式1280*800,32位色,执行此代码会在480*640的区域出现一块颜色,话不多说,有兴趣可以试试哦!

[此贴子已经被作者于2022-10-16 21:05编辑过]

2022-10-16 21:03



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-508187-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.072936 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved