Metro M4 Express - Receiving Serial bytes on PC

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
anoonmoose
 
Posts: 1
Joined: Thu Jul 09, 2020 1:11 pm

Metro M4 Express - Receiving Serial bytes on PC

Post by anoonmoose »

I swear I've tried to Google this and search the forums and while I've found some other people that seem like they've had this problem I don't think I can find an answer.

The following code was programmed into my Metro M4 Express using the Arduino IDE, version 1.8.12. In summary, it waits to receive a newline character over the serial port, and echos it back. It also sets the board's LED high as a sign that it is responding.

Code: Select all

bool ledState = false;
String inputString = ""; //buffer for received string
boolean stringComplete = false; //set this tr

void setup() {
  Serial.begin(9600);
  inputString.reserve(32);

  pinMode(13,OUTPUT); //led
  digitalWrite(13,LOW);
}


void loop() {
  
  while (Serial.available()) {
    char inChar = (char)Serial.read();
    inputString += inChar;
    if (inChar == '\n') {
      stringComplete = true;
    }
  }

  if(stringComplete)
  {
    digitalWrite(13,HIGH);
    Serial.print("ECHO: "+inputString);
    delay(500);
    digitalWrite(13,LOW);
    inputString = "";
    stringComplete = false;
  }
}
If I open up the Arduino IDE Serial Monitor, I can type commands and have them echoed back to me. I see the LED on pin 13 go high, and I see the RX and TX LEDs blink briefly.

However, the following C# code, which I would expect to work given a fair amount of experience with C#, .NET, and this class, does not work:

Code: Select all

SerialPort pn;

void Main()
{
	pn = new SerialPort("COM26", 9600);
	pn.Open();
	Thread.Sleep(1000);
	
	pn.WriteLine("VER");
	Thread.Sleep(1000);
	
	var cx = pn.BytesToRead;
	var buff = new byte[cx];
	pn.Read(buff, 0, cx);
	Console.WriteLine($"Read {cx} bytes");

	pn.Close();
}
When I run this code, I see the RX pin blink, and the pin 13 LED also blinks, which means 1) the device is receiving serial bytes and 2) one of those bytes is a newline, so I should receive a response. Instead, I get no bytes in response. Additionally, the TX pin does not blink. Can't be that I'm using the wrong COM port in the code, since the Metro is clearly receiving the string or else the RX and PIN13 LEDs wouldn't go high.

I understand that this may be a .NET issue, which may be outside of the scope of this forum. But I'm migrating from an old Metro 328 to a Metro M4 Express, and this code works just fine on the Metro 328.

Thanks in advance.

Locked
Please be positive and constructive with your questions and comments.

Return to “Metro, Metro Express, and Grand Central Boards”