【CH554式改评测】CH554和libusb(下)
<div class='showpostmsg'>接【CH554式改评测】CH554和libusb(上)
https://bbs.eeworld.com.cn/forum.php?mod=viewthread&tid=641783&fromuid=305293
(出处: 电子工程世界-论坛)
上集吹了一下怎么修改windows驱动,和单片鸡代码的乱改
接下来请出UI钜献
Q......T......
要配套libusb使用的文件有两个
libusb.a和lusb0_usb.h
库文件和头文件
说好的开源呢
记得把lusb0_usb.h包含上
上集里面提到的VID、PID和endPoint必须对得上
不对不通,妥妥滴
面向对象真好用
上点初学的代码
bool MainWindow::openUSB(struct usb_device *dev)
{
devOpenFlg = false;
devHandle = usb_open(dev);
if(!devHandle)
{
qDebug() << "error opening device: ";
qDebug() << usb_strerror();
return false;
}
else
{
QString svid = QString::number(dev->descriptor.idVendor, 16);
QString spid = QString::number(dev->descriptor.idProduct, 16);
qDebug() << "success: device " << svid << " : "<<spid << " opened";
devOpenFlg = true;
}
if (usb_set_configuration(devHandle, MY_CONFIG) < 0)
{
qDebug() << "error setting config #" << MY_CONFIG << " : " << usb_strerror();
usb_close(devHandle);
return false;
}
else
{
qDebug() << "success: set configuration #" << MY_CONFIG;
}
if (usb_claim_interface(devHandle, 0) < 0)
{
qDebug() << "error claiming interface #" << MY_INTF;
qDebug() << usb_strerror();
usb_close(devHandle);
return false;
}
else
{
qDebug() <<"success: claim_interface #" << MY_INTF;
}
return true;
}
bool MainWindow::closeUSB()
{
if(devHandle)
{
devOpenFlg = false;
// UsbListener::quit();
int ret = usb_close(devHandle); // Exit Thread
if(ret == 0)
{
qDebug() << "Close USB Device success, ret = " << ret << "\n";
return true;
}
else
{
qDebug() << "Close USB Device failed, ret = " << ret << "\n";
return false;
}
}
else {}
return false;
}
bool MainWindow::sendData(QByteArray &data)
{
if(true == devOpenFlg)
{
int ret = 0;
char *tmp = data.data();
ret = usb_bulk_write(devHandle, EP_OUT, tmp, data.length(), 5000);
if (ret < 0)
{
qDebug() << "error writing:";
qDebug() << usb_strerror() << ret;
return false;
}
else
{
qDebug() << "success: bulk write " << ret <<" bytes";
return true;
}
}
else{}
return false;
}
struct usb_device * MainWindow::my_usb_get_device_list(const unsigned short idVendor,const unsigned short idProduct)
{
struct usb_bus *bus;
int bus_cnts,dev_cnts;
bus_cnts = usb_find_busses(); /* find all busses */
dev_cnts = usb_find_devices(); /* find all connected devices */
if(dev_cnts == 0)
{ui->statusBar->showMessage(tr("未发现设备"));}
else{}
if(bus_cnts == 0)
{ui->statusBar->showMessage(tr("未发现总线"));}
else{}
for (bus = usb_get_busses(); bus; bus = bus->next)
{
for (dev = bus->devices; dev; dev = dev->next)
{
/* for specified device
if (dev->descriptor.idVendor == MY_VID
&& dev->descriptor.idProduct == MY_PID)
{
return usb_open(dev);
}
*/
if((dev->descriptor.idVendor == idVendor) && (dev->descriptor.idProduct == idProduct))
{
QString svid = QString::number(dev->descriptor.idVendor, 16);
QString spid = QString::number(dev->descriptor.idProduct, 16);
qDebug("VID:PID = %x:%x\n", dev->descriptor.idVendor, dev->descriptor.idProduct);
ui->statusBar->showMessage(tr("设备已插入,VID:PID=%1:%2\n").arg(svid).arg(spid));
return dev;
}
else{}
}
}
return NULL;
}
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG *msg = reinterpret_cast<MSG *>(message);
eventType.count();
*result = *result;
int msgType = msg->message;
if(msgType == WM_DEVICECHANGE)
{
switch(msg->wParam)
{
case DBT_DEVICEARRIVAL:
/* break; */
case DBT_DEVICEREMOVECOMPLETE:
/* break; */
case DBT_DEVNODES_CHANGED:
{
dev = my_usb_get_device_list(MY_VID, MY_PID);
if(NULL != dev)
{
QString svid = QString::number(dev->descriptor.idVendor, 16);
QString spid = QString::number(dev->descriptor.idProduct, 16);
ui->statusBar->showMessage(tr("设备已插入,VID:PID=%1:%2\n").arg(svid).arg(spid));
usb_init(); /* initialize the library */
usb_set_debug(250);
openUSB(my_usb_get_device_list(MY_VID,MY_PID));
}
else
{
ui->statusBar->showMessage(tr("设备已移除"));
}
}
break;
default:
break;
}
}
return false;
}
void MainWindow::on_sendButton_clicked()
{
QByteArray data;
QString tmp;
tmp = ui->input_box->text();
data = tmp.toUtf8();
sendData(data);
}
void MainWindow::on_scanButton_clicked()
{
}
void MainWindow::on_reopenButton_clicked()
{
}
void MainWindow::timerEvent(QTimerEvent *e)
{
int ret;
char tmp;
if(e->timerId() == read_timer)
{
ret = usb_bulk_read(devHandle, EP_IN, tmp, sizeof(tmp), 10);
if(ret > 0)
{
qDebug() << "success: bulk read " << ret << " bytes";
// QString strTmp;
// QString strOut;
// for(int i = 0; i < ret; i++)
// {
// strTmp.sprintf("%02X ", (unsigned char)tmp);
// strOut.append(strTmp);
// }
// qDebug() << strOut;
QByteArray rcv_buf(tmp, ret);// Receive Data Proccess Function
qDebug() << rcv_buf;
ui->textBrowser->insertPlainText(rcv_buf);
ui->textBrowser->insertPlainText("\r");
rcv_buf.clear();
}
else{}
}
}
void MainWindow::timerUpdate()
{
}
可以实时监测CH554 USB_DEVICE热插拔功能
HID设备数据IN采用最2的方式
10ms查询一次,大神们来耻笑楼主吧
不要说什么线程,撸起键盘就是码
可以发送接收数据
不知道为毛,单片鸡侧会多打印2个字节数据出来
回头再debug一下
热插拔测试一下
先到这....
此内容由EEWORLD论坛网友ljj3166原创,如需转载或用于商业用途需征得作者同意并注明出处
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> :handshake 你好,我是个小白,感谢你付出这么多,我想问下,你那个上位机是用什么软件呢? 你好,那个上位机用的什么软件呢?
页:
[1]