Description
I connect MKR 1010 to a Lidar with a OTG cable .MKR 1010 is USBHost , it can send command to Lidar and receive data from Lidar.
But the problem I meet is the data I receive is loss some bytes. The bellow is my code.
`#include <cdcacm.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <usbhub.h>
#include "pgmstrings.h"
// On SAMD boards where the native USB port is also the serial console, use
// Serial1 for the serial console. This applies to all SAMD boards except for
// Arduino Zero and M0 boards.
#if (USB_VID==0x2341 && defined(ARDUINO_SAMD_ZERO)) || (USB_VID==0x2a03 && defined(ARDUINO_SAM_ZERO))
#define SerialDebug SERIAL_PORT_MONITOR
#else
#define SerialDebug Serial1
#endif
class ACMAsyncOper : public CDCAsyncOper
{
public:
uint8_t OnInit(ACM *pacm);
};
uint8_t ACMAsyncOper::OnInit(ACM *pacm)
{
uint8_t rcode;
// Set DTR = 1 RTS=1
rcode = pacm->SetControlLineState(3);
if (rcode)
{
ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
return rcode;
}
LINE_CODING lc;
lc.dwDTERate = 115200;
lc.bCharFormat = 0;
lc.bParityType = 0;
lc.bDataBits = 8;
rcode = pacm->SetLineCoding(&lc);
if (rcode)
ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
return rcode;
}
uint8_t* hokuyo_data= (uint8_t*) malloc(128);
char ssid[] = "passssss";
char pass[] = "12345678";
int keIndex = 0;
int status = WL_IDLE_STATUS;
const IPAddress serverIP(192, 168, 137, 1);
uint16_t serverPort = 54768;
WiFiClient client;
USBHost UsbH;
USBHub Hub(&UsbH);
ACMAsyncOper AsyncOper;
ACM Acm(&UsbH, &AsyncOper);
void setup()
{
// SerialDebug.begin(19200);
// SerialDebug.println("Start");
if (UsbH.Init())
//SerialDebug.println("USB host failed to initialize");
delay( 200 );
//SerialDebug.println("USB Host init OK");
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
//Serial.print(".");
}
if (client.connect(serverIP, serverPort))
{
client.print("Hello world!");
}
else
{
client.stop();
}
//client.connect(serverIP, serverPort);
//client.print("Hello world!");
}
void loop()
{
UsbH.Task();
if (Acm.isReady()) {
uint8_t rcode;
if (client.available()) {
String line = client.readString();
//client.write(line.c_str());
/* sending to the phone */
rcode = Acm.SndData(line.length(), (uint8_t*) line.c_str());
if (rcode)
ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
} //if(Serial.available()...
//delay(50);
uint32_t epAddr = Acm.GetEpAddress();
static uint8_t buf[1024];
uint16_t rcvd = 1024;
rcode = Acm.RcvData(&rcvd, buf);
if (rcode && rcode != USB_ERRORFLOW)
ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
if (rcvd) { //more than zero bytes received
for (uint16_t i = 0; i < rcvd; i++) {
client.write((char) buf[i]); //printing on the screen
}
}
}
}`