M
M
Maxim2016-10-23 13:58:06
Arduino
Maxim, 2016-10-23 13:58:06

Serial not opening on arduino?

In arduino IDE, everything works, but in my program it doesn't. The Tx and Rx LEDs do not flash.
Serial Connection:
Structure

//Структура для передачи данных в поток
        public struct COMInfo
        {
            public readonly string Port;
            public readonly int Baud;
            public COMInfo(string port, int baud){ this.Port = port; this.Baud = baud; }
        }

Opening a stream and the stream itself.
public void Create()
        {

            Thread Creator = new Thread(Conect); //Поток для создания Serial
            var TimeController = new System.Timers.Timer(2500.00); // Таймер для защиты от долгого создания
            //Событие, которое закрывает поток
            TimeController.Elapsed +=  delegate {if (Creator.IsAlive) Creator.IsBackground = false; };

            Creator.Start(Info);

            //Ожидание
            while (Creator.IsAlive) { }
            TimeController.Close();
                        
            if (Serial != null)
            {
                //Подписка на событие приёма данных
                Serial.DataReceived += Serial_DataReceived;

                Serial.WriteTimeout = 500;
                Serial.ReadTimeout = 500;
            }
        }


//Создание экземпляра, открытия Serial
        private void Conect(object info)
        { 
            //Получение данных о порте
            COMInfo cominfo = (COMInfo)info;
            int speed = cominfo.Baud;
            string port = cominfo.Port;
            lock (this)
            {
                try
                {
                    Serial = new SerialPort(port, speed);
                    Serial.Open();
                    Thread.Sleep(1000); //Ожидание, нужное для подключения к Serial
                    Serial.Write("Оpen");
                }
                catch(Exception e)
                {
                    //Вызов события при возникновении ошибки
                    if (CreatedExceptionEvent != null) CreatedExceptionEvent(this, e);
                    throw e;
                }
            }
        }

Implementation of the MVC pattern
Reading:
SerialReader class (This is where the instance was created)
//Эта строчка есть в потоке
//Serial.DataReceived += Serial_DataReceived;

private void Serial_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            Thread.Sleep(500);
            if (InputData != null) InputData(this, Serial.ReadExisting());
        }

presenter
private void Reader_InputData(object sender, string e)
        {
            MainForm.AddText(e);
        }

mainform
public void AddText(string text) => tbSerial.Text = text;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alejandro Esquire, 2016-10-23
@A1ejandro

What kind of arduino would write ....

P
papkinv, 2016-10-23
@papkinv

Why not try a simple connection option from C# first? Without any multithreading.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question