通过PC和Arduino之间的USB端口发送和接收数据

时间:2019-03-01 08:47:56

标签: arduino libusb-1.0

我想使用libusb库以同步模式将笔记本电脑中的一些数据发送到Arduino,然后我将接收到一些修改后的数据,这是我的C代码的相关部分:

int main()
{
    int res                      = 0;  /* return codes from libusb functions */
    libusb_device_handle* handle = 0;  /* handle for USB device */
    int kernelDriverDetached     = 0;  /* Set to 1 if kernel driver detached */
    int numBytes                 = 0;  /* Actual bytes transferred. */
    libusb_context *ctx          = NULL; //a libusb session
    uint8_t buffer[]="hello world!!!!";  //data to send
    uint8_t buffer1[64]="";
    printf("%s\n", buffer);
    /* Initialise libusb. */
    printf("emission...\n");
    res = libusb_init(&ctx);

    if (res != 0)
    {
        fprintf(stderr, "Error initialising libusb.\n");
        return 1;
    }
    handle = libusb_open_device_with_vid_pid(ctx, VENDOR_ID, PRODUCT_ID);
    if (!handle)
    {
        fprintf(stderr, "Unable to open device.\n");
        return 1;
    }
    /* Check whether a kernel driver is attached to interface #0. If so, we'll
    * need to detach it.
    */

    if (libusb_kernel_driver_active(handle, 0))
    {
        res = libusb_detach_kernel_driver(handle, 0);
        if (res == 0)
        {
            kernelDriverDetached = 1;
        }
        else
        {
            fprintf(stderr, "Error detaching kernel driver.\n");
            return 1;
        }
    }

    /* Claim interface #0. */
    res = libusb_claim_interface(handle, 0);
    if (res != 0)
    {
        fprintf(stderr, "Error claiming interface.\n");
        return 1;
    }
    /* Send the message to endpoint 1 with a 100ms timeout. */

    res = libusb_bulk_transfer(handle, ENDPOINT_OUT, buffer, (int)strlen((char*)buffer), &numBytes, 100);
    if (res == 0)
    {
        printf("%d bytes transmitted successfully.\n", numBytes);
    }
    else
    {
        fprintf(stderr, "Error sending message to device.\n");
    }

    res = libusb_bulk_transfer(handle, ENDPOINT_IN, buffer1, 64, &numBytes, 0);
    if (0 == res)
    {
        printf("received data: %s \n", buffer1);
        printf("length: %d %d\n", (int)strlen((char*)buffer1),numBytes);
    }
    else printf("reception error!\n");

    /* Release interface #0. */
    res = libusb_release_interface(handle, 0);
    if (0 != res)
    {
        fprintf(stderr, "Error releasing interface.\n");
    }

    /* If we detached a kernel driver from interface #0 earlier, we'll now
    * need to attach it again.  */
    if (kernelDriverDetached)
    {
        libusb_attach_kernel_driver(handle, 0);
    }

    /* Shutdown libusb. */
    libusb_exit(ctx);

    return 0;
}

这是Arduino代码的相关部分:

char code[64]="";   
byte i;         
byte j;

void loop()
{
    i = 0;
    j = 0;
    while(Serial.available() && i<64)  // we have some data to receive
    {
        j = 1;
        code[i] = Serial.read();  
        delay(50);
        i++;
    }
    if (i<64){
        code[i]='\0';
    }
    else {
        code [63]='\0';
    }
    if (j==1){
        Serial.write("received by Arduino: ");
        Serial.println(code);
        j=0;
    }

    while(!Serial.available());   
}

在运行时,无法正确接收数据,例如:

  

世界您好!!!发射...成功传输了15个字节。

     

接收到的数据:否:rl

     

由Arduino接收:d

     

长度:32 32

通常我应该收到“ Arduino收到的消息:hello world !!!!”

0 个答案:

没有答案