T
T
Timofey Isakov2016-05-04 16:34:30
Mono
Timofey Isakov, 2016-05-04 16:34:30

MonoDevelop Serial Port?

Hello, I am writing in C # in MonoDevelop, you need to open the port, and put all the data received from the port into the textbox, everything worked out without problems in the console application, but when I tried to create a graphical shell, I ran into a problem, the application form does not load, the program seems to be listening port and can not do anything else, the code is at least in principle, but I have been suffering with it for a long time, I am not particularly strong in programming. Thanks for the help.

using System;
using Gtk;
using System.IO.Ports;
using System.Threading;

public partial class MainWindow: Gtk.Window
{   
   public MainWindow (): base (Gtk.WindowType.Toplevel)
   {
      Build ();
      GetValues ();
   }
   public void GetValues()
   {
      SerialPort sp = new SerialPort ("/dev/ttyAMA0", 9600, Parity.None, 8, StopBits.One);
      sp.Open ();
      if (sp.IsOpen) 
      {
                 while(true)        //I thing this is a problem!
         {
            queue.Buffer.Text += Convert.ToChar (sp.ReadChar ());
                     }
      }

   }

   protected void OnDeleteEvent (object sender, DeleteEventArgs a)
   {
      Application.Quit ();
      a.RetVal = true;
   }

}

If done
if (sp.IsOpen) 
      {  queue.Buffer.Text += Convert.ToChar (sp.ReadChar ());
            
               }

That counts only 1 character, ReadLine doesn't work, ReadExisting works in a console application. Maybe some kind of backgroundworker that will poll the port and if there is data, then add it? I don't know anymore.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
amf1k, 2016-05-04
@istimbi

Most likely the flow with the interface is blocked. It is necessary to move the method that listens to the port into a separate thread

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question