Sunday, 15 July 2012

c# - Make SerialPort close and stop read data on window closing -


I think that when I open the serial port, when I close my WPF window, there is a problem (I It seems that it still receives some data). What will be the best way to shut it down? So far, I have found a solution, but it is not certain that its work has been done.

My serial port settings:

  serialPort.ReadTimeout = 1000; SerialPort = New SerialPort (); SerialPort.PortName = Application. Present. Properties ["DevPort"]. ToString (); SerialPort.BaudRate = 9600; serial. Port = stop One; SerialPort.DataBits = 8; SerialPort.Parity = Samata.No; serial port. Handshake = Handshake.No; SerialPort.DataReceived + = serial_sp_Data retrieved; SerialPort.Open ();  

window. Stop the method:

  if (serialPort.IsOpen) {this.Closing - = Window_Closing; Thread CloseDown = New Thread (New Thread Start (Serial Port. Disclosure)); CloseDown.Start (); }  

It is difficult to reach that window freeze (usually it is more than 1x / 10 D or greater).

@EDIT:

data_modified method:

  int count = serialPort What will be the best way to close / dispersulate the serial port before the window closes? BytesToRead; Byte [] bytere = new byte [count]; serial port. Read (Bytere, 0, Count); Bytere = Subarre (Bytere, 5, 5); String S2 = Bitakkar Toasting (byte); String [] tempAry = s2.Split ('-'); Byte [] decBytes2 = new byte [tempAry.Length]; For (Int i = 0; I & lt; tempAry.Length; i ++) DCBites 2 [I] = Convert. Toight (Tampier [i], 16); String host; Int port = int.Parse (Application.Current.Properties ["Port"]. ToString ()); Host = Application.Current.Properties ["Host"]. ToString (); Tekst = s2 Replace ("-", ""); String result = start client (host, port, text); String [] _result = result.Split ('_'); Tekst = ""; If (_result [1] .tostring () == "-1") {var thread = new thread (new threadstart (shower)); Thread.SetApartmentState (ApartmentState.STA); Thread.Start (); Thread.Join (); Tekst = ""; Return; } Card = _result [0]; Digit = decimal Purse (_result [1]); Thread 1 = new thread (() => press button (card button); Thread1.SetApartmentState (ApartmentState.STA); Thread1.Start (); Thread1.Join (); ShowError method shows only my custom error window PressButton method B. RaiseEvent (new routing adventure agent (buttonclive event));  

Your problem is normal: there are still some threads when running the application Used to be? The answer is very clear: Ask to close the thread and wait for it to actually end.

However, your specific problem may be related to how the serial port is operated. This is because it takes advantage of an underlying thread, and you should take care to run it as little as possible (which is to avoid any block).

It is a special case when you use the Read method, until all necessary data is read:

Instead, you should use another approach: first See how many bytes have already been received, then read them and no one else. Total stream should be collected by your code:

See an example here, old but still good:


No comments:

Post a Comment