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.