安富莱USB上位机教程
参考安富莱的视频,不过这里 调用是libusb最新的库
可以参考上一个文章:
QT调用最新的libusb库
https://editor.csdn.net/md/?articleId=142733711
调试结果:
资源地址:
上位机:https://download.csdn.net/download/a15236617777/89856863
对比参照:
这个是安富莱的QT工程用的库,应该不是最新的。
https://download.csdn.net/download/a15236617777/89856864
部分代码:
int OpenUSB(void)
{
int ret;
// 初始化 libusb
ret = libusb_init(&m_dev_ctx);
if (ret < 0) {
std::cerr << "Failed to initialize libusb" << std::endl;
return ret;
}
// 打开 USB 设备,替换为你的设备 VID 和 PID
m_dev_handle = libusb_open_device_with_vid_pid(m_dev_ctx, m_dev_VENDOR, m_dev_PRODUCT);
if (!m_dev_handle) {
std::cerr << "Cannot open device" << std::endl;
libusb_exit(m_dev_ctx);
return -1;
}
// 申请接口(假设接口 0)
ret = libusb_claim_interface(m_dev_handle, 1);
if (ret < 0) {
std::cerr << "Cannot claim interface" << std::endl;
libusb_close(m_dev_handle);
libusb_exit(m_dev_ctx);
return ret;
}
return ret=0;
}
int ReadUsbMsg(void)
{
int ret;
libusb_device *newdevice=libusb_get_device(m_dev_handle);
libusb_device_descriptor desc;
// 获取设备描述符
if (libusb_get_device_descriptor(newdevice, &desc) != 0) {
std::cerr << "Failed to get device descriptor" << std::endl;
return -1;
}else
{
// 获取厂家字符串
if (libusb_get_string_descriptor_ascii(m_dev_handle, desc.iManufacturer, usb_msg_manufacturer, sizeof(usb_msg_manufacturer)) > 0) {
std::cout << "Manufacturer: " << usb_msg_manufacturer << std::endl;
}
// 获取产品字符串
if (libusb_get_string_descriptor_ascii(m_dev_handle, desc.iProduct, usb_msg_product, sizeof(usb_msg_product)) > 0) {
std::cout << "Product: " << usb_msg_product << std::endl;
}
// 获取序列号字符串
if (libusb_get_string_descriptor_ascii(m_dev_handle, desc.iSerialNumber, usb_msg_serial, sizeof(usb_msg_serial)) > 0) {
std::cout << "Serial Number: " << usb_msg_serial << std::endl;
}
}
return ret=1;
}
int WriteBulkData() {
int transferred; // 实际传输字节数
int ret;
// 发送数据
ret = libusb_bulk_transfer(m_dev_handle, 2, Send_CmdData, sizeof(Send_CmdData), &transferred, 1000);
if (ret < 0)
{
std::cerr <<libusb_error_name(ret)<< std::endl;
return ret;
} else
{
std::cout<<"Sent CMD: 0X"<<std::setw(2) << std::setfill('0') << std::hex <<(int)Send_CmdData[0]<<"("<<transferred<<"bytes)\n"<<std::endl;
}
return 0;
}
void display_read_data(unsigned char *buffer, int transferred) {
std::cout << "Read " << transferred << " bytes:" << std::endl;
for (int i = 0; i < transferred; i++) {
if (i % 50 == 0) { // 每16个字节换行
if (i > 0) {
std::cout << std::dec << std::endl; // 切换回十进制
}
std::cout << "Line " << (i / 50) << ": ";
}
std::cout << std::setw(2) << std::setfill('0') << std::hex << (int)buffer[i] << " ";
}
std::cout << std::dec << std::endl; // 切换回十进制
}
int ReadBulkData() {
//unsigned char buffer[2048]; // 缓冲区大小根据需求调整
int transferred; // 实际传输字节数
int ret;
// 执行 bulk 读取 RecTraData
ret = libusb_bulk_transfer(m_dev_handle, 0x82, Rece_DATA, sizeof(Rece_DATA), &transferred, 1000);
//ret = libusb_bulk_transfer(m_dev_handle, 0x82, buffer, sizeof(buffer), &transferred, 1000);
if(ret<0)
{
std::cerr << "Bulk read failed: " << libusb_error_name(ret) << std::endl;
return ret;
}
else
{
display_read_data(Rece_DATA,transferred);
}
return transferred;
}