I started with a simple sketch to send data back to PC, also allow PC to control the LEDs on xiao.
When running the sketch, if I use teraterm/putty, i can receive data from xiao and control the LED.
but when i use c#, i can control the LED, but i can never receive anything from xiao. Any suggestion?
Thanks for any pointer!
[xiao sketch]
const int LedPin = 13;
int ledState = 0;
void setup()
{
pinMode(LedPin, OUTPUT);
SerialUSB.begin(9600);
}
void loop()
{
char receiveVal;
SerialUSB.print('1');
delay(200);
if(SerialUSB.available() > 0)
{
receiveVal = SerialUSB.read();
if(receiveVal == '1')
ledState = 1;
else
ledState = 0;
}
digitalWrite(LedPin, ledState);
}
[VS2017 C# console program]
using System;
using System.IO.Ports;
namespace ConsoleApp1
{
class Program
{
static SerialPort _serialPort;
static char [] message = new char [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;
}
}
}
}
}
}