I am currently working on my own library "myUSBSerial.h" which inherits the class "USBSerial" from Portenta's standard library "PluggableUSBSerial.h".
After creating and writing the library, I have created two COM ports. (There was only one until now. See image).
Of these, COM12 does not return a response when connected, and is confusing, so I want to delete it and have only one COM port.
However, this is not going well.
Please give me some advice.
myUSBSerial.cpp
#include "stdint.h"
#include "myUSBSerial.h"
#include "USB/USBCDC.h"
using namespace arduino;
MyUSBSerial::MyUSBSerial(USBPhy *phy, uint16_t vendor_id, uint16_t product_id, uint16_t product_release) :
USBSerial::USBSerial(phy, vendor_id, product_id, product_release) {
//_SerialUSB.~USBSerial();
//USBSerial _SerialUSB(false);
}
void MyUSBSerial::line_coding_changed(int baud, int bits, int parity, int stop)
{
USBSerial::line_coding_changed(baud, bits, parity, stop);
}
void MyUSBSerial::beginSt(unsigned long baud) {
begin(baud);
}
myUSBSerial.h
#ifndef MYUSBSERIAL_H
#define MYUSBSERIAL_H
#include "Arduino.h"
#include "stdint.h"
#include "USB/PluggableUSBSerial.h"
#include "USB/USBCDC.h"
#include "usb_phy_api.h"
#include "mbed.h"
namespace arduino {
class MyUSBSerial : public ::USBSerial {
public:
MyUSBSerial(USBPhy *phy, uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001);
virtual ~MyUSBSerial(){}
void line_coding_changed(int baud, int bits, int parity, int stop) override;
void beginSt(unsigned long baud);
void beginSt(unsigned long baud, uint16_t config);
uint32_t counter() {
return 334;
}
};
}
#endif
sketch.ino
#include "myUSBSerial.h"
MyUSBSerial mySerial(get_usb_phy());
const long BAUD_RATE = 115200L;
void setup() {
mySerial.beginSt(BAUD_RATE);
delay(1000);
}
void loop() {
String request = "";
bool isCmd = false; // Flag: delimiter
bool isCR = false; // Flag: <CR>
char thisChar = 0; // received character
while(mySerial.available() >0) {
thisChar = mySerial.read();
request += thisChar;
if(thisChar == '\n' && isCR == true) {
isCmd = true;
break;
}
if(thisChar == '\r')
isCR = true;
else
isCR = false;
}
if(isCmd) {
mySerial.print("Recieve: ");
mySerial.print(request);
}
}