Komunikacja arduino z komputerem

0

Witam forumowiczów, mam pewien problem. Od pewnego czasu bawię się z arduino i przy próbie komunikacji go z moim programem konsolowym w c#. Czasem wyświetla dwa pierwsze komunikaty ale później znowu pokazuje się błąd taki jak na obrazku. Z góry dziękuje za każdą pomoc.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ArduinoBETAConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort myport = new SerialPort()
            {
                BaudRate = 9600,
                PortName = "COM3",
                ReadTimeout = 100
            };

            myport.Open();

            while (true)
            {
                try
                {
                    Console.WriteLine(myport.ReadLine());
                }
                catch
                {
                    Console.WriteLine("NIE PYKLO");
                }
            }
        }
    }
}

A tutaj kod na arduino (tak wiem prosty :D)

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
}
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);
  Serial.println("ON");
  delay(1000);                       
  digitalWrite(LED_BUILTIN, LOW); 
  Serial.println("OFF");   
  delay(1000);                       
}

Monitor portu Arduino IDE, oraz błędy w visual studio są pokazane na zdjęciach w linku. Sory, że w linku ale coś nie działa mi w stawianie pomimo, że korzystam z chrome.

static.4programmers.net/uploads/attachment/5a/5a0a04e21e0b1.png
static.4programmers.net/uploads/attachment/5a/5a0a04e45ece2.png (Jeśli usunę try, catch)
static.4programmers.net/uploads/attachment/5a/5a0a04e6652b1.png
static.4programmers.net/uploads/attachment/5a/5a0a04e8c7a15.png

0

Nie ustawiasz zbyt niskiego timeout'u w ustawieniach portu szeregowego?
Bo w konsoli masz 100(ms) a w arduino delay 1000(ms).

0

Nic nie pomogło

0

Musi, masz za mały czas, ustaw na 2000 i zobacz co się stanie. Oczywiście w programie na komputerze

0

Po prostu wolniej wypisuje ale dalej nie działa

0

Ale czy nie przypadkiem ReadTimeout w SerialPorcie, nie podaje się w Tickach? Usuń po prostu ReadTimeout, bo domyślnie jest nieskończony, i sprawdź jak to działa. Potem możesz ustawić Timeout (10 000 Tick-ów to milisekunda).

2

Zrób tak
Dla obiektu serialPort dodaj metodę obsługi zdarzenia

serialPort1.DataReceived += SerialPort1_DataReceived;

Definicja metody:

private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = sender as SerialPort;
            if (sp != null)
            {
                string dataReceived = sp.ReadLine();
                Console.WriteLine(dataReceived);                
            }
        }

a tego TimeOut wywal

0

@Bartek P:
Z MSDN

Gets or sets the number of milliseconds before a time-out occurs when a read operation does not finish.

The read time-out value was originally set at 500 milliseconds in the Win32 Communications API. This property allows you to set this value. The time-out can be set to any value greater than zero, or set to InfiniteTimeout, in which case no time-out occurs. InfiniteTimeout is the default.

Co może warto byłoby spróbować - ustawić InifiniteTimeout i mierzyć ile tak naprawdę zajmuje odebranie wiadomości bo być może coś ze strony Arduino jest nie tak.

0

Po usunięciu nie wyswietla się nic no chyba że najpierw odpale monitor portu na arduino i po chwili wylacze nastepnie odpale program c# to wypisuje sie od razu 2/3 razy on lub off i znowu nic a gdy zamkne i odpale jeszcze raz c# to nic sie nie wyswietla do czasu gdy powtorze wlaczenia serial portu arduino.

0

Nie działa no chyba ,że coś źle robię

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ArduinoBETAConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort myport = new SerialPort()
            {
                BaudRate = 9600,
                PortName = "COM3 
            };
            myport.Open();

            myport.DataReceived += SerialPort1_DataReceived;
            void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                SerialPort sp = sender as SerialPort;
                if (sp != null)
                {
                    string dataReceived = sp.ReadLine();
                    Console.WriteLine(dataReceived);
                }
            }
        }
    }
}
1

Masz metodę DataReceived w środku Main.

0

Tak ma być

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ArduinoBETAConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort myport = new SerialPort()
            {
                BaudRate = 9600,
                PortName = "COM3"
            };
            myport.DataReceived += Myport_DataReceived;
            myport.Open();    
        }

        private static void Myport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = sender as SerialPort;
            if (sp != null)
            {
                string dataReceived = sp.ReadLine();
                Console.WriteLine(dataReceived);
            }
        }
    }
}
0

Nie działa, wywalało błąd kiedy nie była statyczna funkcja

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
namespace ArduinoBETAConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort myport = new SerialPort()
            {
                BaudRate = 9600,
                PortName = "COM3"
                
            };


            myport.Open();

            myport.DataReceived += SerialPort1_DataReceived;
        }
        static void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = sender as SerialPort;
            if (sp != null)
            {
                string dataReceived = sp.ReadLine();
                Console.WriteLine(dataReceived);
            }
        }
    }
}
0

Próbowałem twoją Manuel.Artificer

0

Na końcu main daj Console.Read() bo teraz Ci zamknie program od razu

0

Nic nie wyświetla, czeka po prostu na klawisz

0

Sprawdziłem i jest na com3 na innych nie otwiera portu i pokazuje błąd. Pętla i też nic nie zmienia

0

Dobra mam. Cały czas programowałem teraz na okienkach i nigdy przez to się z takim czymś nie spotkałem

Na końcu main zamiast Console.Read() oraz zwykłego while(true); wstaw coś takiego

            while (true)
            {
                Application.DoEvents();
            }

Jak z tym nie ruszy to się poddaję

1 użytkowników online, w tym zalogowanych: 0, gości: 1