首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

linux 2.6下编译usb驱动和arm板进行数据通信(8)

linux 2.6下编译usb驱动和arm板进行数据通信(8)

BOOL OpenUsbPort (long int DelayTime)
{
    char Message[100];
    unsigned long int Seconds, count=100;
    BOOL DeviceFound = FALSE,MessageEnable = TRUE;
    struct termios options, old_options;
    int file;

    file = open(DeviceName, O_RDWR);
    if (file < 0)
    {
        printf("ERROR: Open driver failed:%d \n", errno);
        return FALSE;
    }



    usb_fd = file;
    usb_dbg("%s end, usb_fd %d\n", __func__, usb_fd);
    return TRUE;
}

BOOL WriteUsb (WTPCOMMAND WtpCmd,DWORD dwCmdSize)
{
    char Message[100];
    int result;
    DWORD IOCTL_CMD, dwActualBytes = 0;
    BOOL IsOk;

    if (usb_fd == 0)
    {
        sprintf (Message,"  Error: OpenUsbPort must be called prior to calling WriteUsb!");
        usb_dbg("%s %d USB COMMUNICATIONS ERROR\n", __func__, __LINE__);
        return FALSE;
    }

    result = write( usb_fd, &WtpCmd, dwCmdSize);
    if (result < 0)
    {
        sprintf (Message,"  Error: WriteUsb failed with error %d", result);
        usb_dbg("%s %d USB COMMUNICATIONS ERROR\n", __func__, __LINE__);
    }

    if (dbg_level)
    {
        sprintf (Message,"IOCTL code: %X",IOCTL_CMD);
        usb_dbg("%s %d USB write %d bytes\n", __func__, __LINE__, result);
    }

    return TRUE;
}

BOOL ReadUsb(WTPSTATUS *WtpStatus, DWORD dwSize, PDWORD pBytesRead)
{
    int result;
   
    usb_dbg("%s %d USB read %d bytes\n", __func__, __LINE__, dwSize);

    result = read(usb_fd, WtpStatus, dwSize);
    if (result <=  0)
    {
#if ___output_log__
        printf("%s return %d\n", __func__, result);
#endif
        return FALSE;
    }
   
    dbg_usbStatus(WtpStatus);
        
    *pBytesRead = result;
    return TRUE;
        
}

BOOL ReadUsbCharBuffer (char *WtpMessage,DWORD dwSize,PDWORD pBytesRead)
{
    int result;

    if (usb_fd < 0)
    {
        printf ("  Error: OpenUsbPort must be called prior to calling ReadUsb!");
        return FALSE;
    }
    result = read(usb_fd, WtpMessage, dwSize);
    if (result < 0 )
    {
        printf ("  Error: ReadFile failed with error\n");
        return FALSE;
    }
   
    *pBytesRead = result;
    if (*pBytesRead != dwSize) return FALSE;
   
    return TRUE;
}

void CloseUsbPort ()
{
    if (usb_fd != 0) close (usb_fd);
}
继承事业,薪火相传
返回列表