0

I am sending the English alphabets with one second delay from an Arduino to my laptop:

void loop() 
{
    delay(1000);
    Serial.write('N'); 

}

I am trying to catch and print the alphabets from a C++ program using libusb. Here is the relevant part of the code:

while(1)
{
        r = libusb_bulk_transfer(dev_handle, 131, data, 1, &actual, 0); 
        if(r == 0 && actual == 1) //we read 1 byte successfully
                cout<<"Read Successful! Data recieved is "<<data[0]<<endl;
        else
                cout<<"Read Error! Return value: "<<r
                    <<"Actual  bytes recieved = "<<actual<<endl;

}

This is the output I get:

Read Successful! Data recieved is A
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is B
Read Successful! Data recieved is C
Read Successful! Data recieved is D
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is E
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is F
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is G
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is H
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is I
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is J
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is K
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is L
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is M
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is N
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is O
Read Successful! Data recieved is P
Read Error! Return value: 0Actual  bytes recieved = 0
Read Successful! Data recieved is Q
Read Error! Return value: 0Actual  bytes recieved = 0

I observe that each second Arduino sends an alphabet and my program catches it without fail. But almost all the time, a read error is also printed on the screen along with the captured object. What is causing this, and how can I stop it?

I've found the source code of Serial.write() here, but I can't make any sense out of it.

daltonfury42
  • 3,103
  • 2
  • 30
  • 47
  • 1
    The Arduino and your PC are not in sync. Not an expert at this, but is there a function in libusb which waits for data. That would most likely fix your problem? – Levi Jun 03 '15 at 21:21
  • You read each second but write each 2 sec. Or what is `delay(2000)`? – qPCR4vir Jun 03 '15 at 21:30
  • @qPCR4vir my mistake, I changed it to 1 second after copy pasting the code snippet here. Thanks for pointing it out, I've update my answer. – daltonfury42 Jun 03 '15 at 21:34
  • I am not an Arduino tech guy but the code posted here, doesn't match the outpus (unless there's some tricky aspect that i missed) – CristiFati Jun 03 '15 at 22:01
  • @CristiFati, it is the output, and the tricky aspect might be that each second multiple lines are printed. – daltonfury42 Jun 04 '15 at 05:46
  • Why use libusb? The Arduino, when plugged in, should expose itself as a regular ol' serial port. – Steve Jun 04 '15 at 20:38
  • why not libusb? or I will have to do in termios. Libusb has error checking, and other safety fetures. – daltonfury42 Jun 04 '15 at 22:18

0 Answers0