I am new to arduino. I started with a simple sketch to allow PC to control the LEDs and echo the command on xiao arduino model. When running the sketch, if I use teraterm or putty, i can read echo from xiao and control the LED (write) successfully. but when i use visual studio c# to write a simple console program, i can control the LED (write), but i can never receive anything from xiao(write). the same c# console can receive from non-arduino serial devices
I tried it on multiple configuration and result is the same.
Can any one see if this true with any arduino board or show me how to fix the problem?
Thanks for any pointer!
[Arduino Sketch]
const int LedPin = 13;
int ledState = 0;
String s = "";
void setup()
{
pinMode(LedPin, OUTPUT);
SerialUSB.begin(9600);
while (!SerialUSB)
delay(10);
}
void loop()
{
if (SerialUSB.available() > 0)
{
s = SerialUSB.readString();
SerialUSB.println("test=" + s);
if (ledState == 1)
ledState = 0;
else
ledState = 1;
digitalWrite(LedPin, ledState);
}
delay(10);
}
[VS2017 C# console program]
using System;
using System.IO.Ports;
namespace ConsoleApp1
{
class Program
{
static SerialPort _serialPort;
static byte [] message = new byte [6000];
public static void Main()
{
int i;
_serialPort = new SerialPort();
_serialPort.PortName = "COM6";//Set your board COM
_serialPort.BaudRate = 9600;
_serialPort.Open();
while (true)
{
if ((i=_serialPort.BytesToRead)>0)
{
_serialPort.Read(message, 0, i);
Console.WriteLine("Read=" + i.ToString());
}
if (Console.KeyAvailable)
{
string c = Console.ReadLine();
_serialPort.Write(c);
if (c == "x")
{
_serialPort.Close();
return;
}
}
}
}
}
}