c# - Serial port communication display -


I am trying to chat with serial port communication, this chat should be an interface in a WPF project, I have previously I have already created my interface and everything, I am stuck in receiving a response through the serial port. I have already tried to add a datatyseived incident from the serial port but I am scared that I am misusing it I am Because I have not programmed in C # this is a very simple code. What I need to do is get the information from the serial port and display it in a text block because it will appear in a simple chat window.

  InitializeComponent (); _serialPort = New SerialPort (); Foreach (SerialPort.GetPortNames in String S ()) {listaComs.Items.Add (S); }} Private Zero DesereseWandler (Object Sender, Serial Data ReservesEurge E.) {SerialPort sp = (SerialPort) Sender; String indenta = sp.ReadExisting (); Visor.Inlines.Add ("receive the data:"); Visor.Inlines.Add (indata); } Private Zero enviarClick (Object Sender, RoutedEventArgs e) {_serialPort.WriteLine (escribir.Text); Visor.Inlines.Add ("yo:" + escribir.Text + Environment.NewLine); } Private Zero cambiarTexto (Object Sender, MouseButtonEventArgs e) {if (escribir.Text == "Escriba su texto") {escribir.Text = ""; }} Private Zero Initializers Serial () {// Porter Las Prolidated Kotas _serialPort.BaudRate = 9600; _serialPort.Parity = Equality One; _serialPort.StopBits = StopBits.One; _serialPort.DataBits = 8; _serialPort.Handshake = Handshake.None; _serialPort.DataReceived + = New SerialDataReceivedEventHandler (DataReceivedHandler); // Panner los timeouts de escritura and lectura _ serialport. Readtimeout = 500; _serialPort.WriteTimeout = 500; } Private Waste Kunker Com (Object Sender, Routing Avenger E. E) {String Selection = Lista.com. Selected Itam Toasting (); _serialPort.PortName = Selection; InicializarSerial (); _serialPort.Open (); _continue = true; Visor.Text = ""; } Private Zero desconectarCom (Object Sender, RoutedEventArgs e) {_serialPort.Close (); }   

When I run it in .exe file, it crashes with an exception (translated) from invalid operation : < Blockquote>

"The call that this subprocess has realized can not access this object because the property is another subprosse."

Events are removed from the thread, and they have a serial port In the case of the layer), it is not going to be definitely on the UI thread.

However, the UI operation should be on the UI thread. The codes below will fail:

  Serialport SP = (SerialPort) sender; String indenta = sp.ReadExisting (); Visor.Inlines.Add ("receive data:"); visor.Inlines.Add (indata);   

Because you are modifying the UI elements from the wrong thread. The solution to using BeginInvoke () to perform the operation Marshall on the UI thread is:

  SerialPort sp = (SerialPort) sender; String indenta = sp.ReadExisting (); Dispatcher.BeginInvoke (() => {visor.Inlines.Add ("Data Received:"); visor.Inlines.Add (indata);}, Faucet);   

The translation of your exception to PS was not correct; If this was the problem, the exception that you will see (in English):

The cross-thread operation is not valid: access controls from a thread other than the thread it was created on.

(or some very similar) I did not recognize the text provided, and gave my code, it seems that you had to face it like this.

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -