I am trying to use my Arduino to control some motors through the USB virtual serial port. It seems to work flawlessly through the Arduino port monitor, through TeraTerm also seems to work well, but I am having issues communicating from my C# software that we use for other motor controllers (we are trying to emulate the interface). The C# code is rock-solid, and is not suspect - we have used it for years now - the only caveat is the serial data must be terminated with LF. When we send data from the C# code to the Arduino, the Rx light flashes as it sees data come in, but I don’t get any data coming into the C# side. I have repeated this effort using the Microsoft SerialChat example: https://stackoverflow.com/questions/905 … in-c-sharp, and see the same results. Both of these programs work as expected if I use an FTDI serial converter with pins 2/3 tied together - everything I send shows on the receive side within the C# code.
I have also tried to detect if the serial port is closing on the Arduino side, and it seemed like this might be part of the issue, but I seem to not be able to end/start the connection if I detect the port closing. I am not sure if this is a red herring or not.
I am really wanting to use the USB virtual com port, but I am having trouble getting past this. I have reduced my code to the following example:
int RXLED = 17; // The RX LED has a defined Arduino pin
void setup()
{
pinMode(RXLED, OUTPUT); // Set RX LED as an output
// set up serial port for output
Serial.begin(115200);
//while (!Serial)
//{
//}
Serial.setTimeout(100);
//Serial.println("START");
}
// https://stackoverflow.com/questions/9072320/split-string-into-string-array
String Split(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {0, -1};
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++)
{
if(data.charAt(i)==separator || i==maxIndex)
{
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : "";
}
void loop()
{
//or, perhaps we get serial commands
//check to see if we have data waiting on the serial port
if (Serial.available() > 0)
{
Serial.println("1\n");
//String lcCmd = Serial.readStringUntil('\n');
String lcCmd = Serial.readString();
lcCmd.trim();
lcCmd.toLowerCase();
Serial.println(lcCmd);
Serial.println("2\n");
//todo/fix - we need to split our string
if (lcCmd.equals("err?") == true)
{
Serial.print("0 - (error 0 - \"No error\")\n");
}
else if (lcCmd.equals("ver?") == true)
{
Serial.print("FW_DSP: V1.2.3\n");
}
Serial.print("Cmd: " + lcCmd + "\n");
}
if (Serial == false)
{
// Serial.end();
Serial.begin(115200);
Serial.setTimeout(100);
Serial.flush();
//digitalWrite(MOT0_ENA, 0);
}
else
{
//digitalWrite(MOT0_ENA, 1);
}
Serial.println("0\n");
//Serial.flush();
//delayMicroseconds(50);
digitalWrite(RXLED, LOW); // set the RX LED ON
TXLED0; //TX LED is not tied to a normally controlled pin so a macro is needed, turn LED OFF
delay(1000); // wait for a second
digitalWrite(RXLED, HIGH); // set the RX LED OFF
TXLED1; //TX LED macro to turn LED ON
delay(1000); // wait for a second
}
I am a bit lost for which direction to go. My instincts tell me to ditch the USB port and just wire up the RX/TX to my FTDI converter to see how this changes things. I am guessing I have a timing issue, but I thought I would reach out for advice first. I have done quite a bit of searching online, but I am overwhelmed with results that don’t seem to fit my issue.