小编App*_*pie的帖子

C程序从连接到系统的USB设备读取数据

我试图从连接到系统USB端口的USB设备(例如pendrive)获取数据.在这里,我可以打开设备文件并读取一些随机原始数据.但我想获取像minicom/teraterm这样的数据.

请让我知道我可以使用哪些方法和库来成功完成,以及如何完成.

#include <stdio.h> 
#include <stdio.h>   
#include <string.h>  
#include <unistd.h>  
#include <fcntl.h>   
#include <errno.h>   
#include <termios.h> 
#include <signal.h>
#include <sys/time.h>

int main()
{
    short portfd=-1;
    int n,f,len;
    char buf[256],*s;
    alarm(2);
#if defined(O_NDELAY) && defined(F_SETFL)
    portfd = open("/dev/ttyUSB0", O_RDWR|O_NDELAY);
    if (portfd >= 0){
        /* Cancel the O_NDELAY flag. */
        printf("port openend\n");
        n = fcntl(portfd, F_GETFL, 0);
        (void) fcntl(portfd, F_SETFL, n & ~O_NDELAY);
    }
#else
    portfd = open("/dev/ttyUSB0", O_RDWR);
#endif
    if (portfd >= 0) 
    {
        if (len == 0) len = strlen(s); …
Run Code Online (Sandbox Code Playgroud)

c linux

6
推荐指数
1
解决办法
2万
查看次数

标签 统计

c ×1

linux ×1