Hello. I have a project where I use Raspberry pi pico. I code it on Arduino IDE. Actually the problem is i cant communicate via serial port between C# and raspberry.
The code on raspberry is this ( I create something simple to test)
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
Serial.println ("0,0,0,0,0,0,0");
delay (500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
As you see it writes a string to serial port every 0.5 sec.
on the C# side code is this (it is also simple created by chatgpt and modified by me)
using System;
using System.IO.Ports;
class SerialPortReader
{
static void Main(string[] args)
{
SerialPort port = new SerialPort("COM7", 9600); // Replace "COM1" with the appropriate port name and "9600" with the baud rate
port.DataReceived += Port_DataReceived;
try
{
port.Open();
Console.WriteLine("Serial port opened. Listening for data... Press any key to exit.");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("Error opening the serial port: " + ex.Message);
}
finally
{
port.Close();
}
}
private static void Port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort port = (SerialPort)sender;
if (port.BytesToRead > 0)
{
string data = port.ReadLine().Trim();
Console.WriteLine(data);
}
}
}
the problem is they can not communicate.
but there is a problem when I open serial port on arduino I see on the data which is "0,0,0,0,0,0,0" has written to serial port by raspberry pi as normal.
then I created com port pair ( com 20 and com21)
I added com 20 to c# and added com 21 to arduino serial port. I write manually "0,0,0,0,0,0,0" data to serial port and c# code read data.
so in a conclusion the raspberry and c# code cant communicate. but i can communicate both of them separately.
Sorry but I did my best to explain. if there is something you didn't understand let me explain again.
Thank you