It is necessary to organize reading from com - port. I can not understand why the data does not come, i.e. The DataReceived event does not fire.

 private void AcceptSettings_Click(object sender, EventArgs e) { try { serialPort.PortName = "COM1"; serialPort.BaudRate = 19200; serialPort.DataBits = 8; serialPort.Parity = Parity.None; serialPort.StopBits = StopBits.One; serialPort.DataReceived += new SerialDataReceivedEventHandler(serialPort_DataReceived); serialPort.Open(); } catch (Exception ex) { MessageBox.Show(ex.Message); serialPort.Close(); } } private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { this.BeginInvoke(new EventHandler(DoUpdate)); } private void DoUpdate(object s, EventArgs e) { StreamWriter fs=null; try { fs = new StreamWriter("log.txt", true, Encoding.Default); str = serialPort.ReadExisting(); fs.WriteLine(str); MessageBox.Show(str); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { fs.Close(); } } 

    1 answer 1

    Does it not work at all, or do you only read the first few bytes? If the latter, then try to read just in the next thread, and not respond to the event. The method is a bit clumsy, but for ComPort the only one is correct. This is my IMHO of course, but it partially follows from the documentation and the way of working. DataReceived is triggered when a byte appears, and there is no guarantee that it will be thrown on every byte.

    If it does not come at all, then perhaps this is not the com port =) Is something sent to the terminal from this port?

    • Yes, using the utilities I read from this port! (I close the port in the utility) - romandrovich
    • We must begin with a simple data acquisition. Try to open the port and do ReadLine (), without threads and events. If there is no translation of strings in the data, then read the number of bytes you are waiting for. - IronVbif