There is a device (scales), which at Com-port throws a value (text line) at a speed of 10 times per second. I need to write an application that allows you to listen to the Com-port and display this line on the screen.
At startup, the program should immediately read the data from the port. It seems nothing complicated. I deduce value in simple Label. If I launch the application, then everything is OK, if I need to stop the port, and then start, I’ve encountered that Label does not have time to draw, or vice versa is updating too quickly. Did this:

public void Service_Start() { try { serial.PortName = "COM1"; serial.BaudRate = 9600; serial.Handshake = System.IO.Ports.Handshake.None; serial.Parity = Parity.None; serial.DataBits = 8; serial.StopBits = StopBits.One; serial.ReadTimeout = 200; serial.Open(); serial.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(Recieve); } catch (Exception) { MessageBox.Show("Не удается найти Com Port. Пожалуйста проверьте настройки подключения"); } } private delegate void UpTDelegate(string text); private void Recieve(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { recieved_data = serial.ReadExisting(); Dispatcher.Invoke(DispatcherPriority.Send, new UpTDelegate(WriteData), recieved_data); } private void WriteData(string text) { lbl_receive.Content = "Вес: " + text; } 

lbl_receive - this is just a Label in which you need to display infu constantly

Can you tell?

  • one
    Can you format the code? - tym32167
  • 2
    Maybe it's worth this serial.ReadTimeout = 200; parameter play? This is just a delay in reading. Yes, and if you find disconnecting / enabling the port, did you remember to catch these moments in order to unsubscribe / subscribe to / from DataReceived ? - Bulson
  • c readtimeout was played, did not help. But you were right ... I forgot to unsubscribe from the event when the port is disconnected. Thank you very much - Denis Ivanov

0