简体   繁体   中英

Serial Port In C#

I am working with a program the reads data from a file and plots it to a graph in real time. My stream is coming from a microcontroller output and I am building an interface to display the data. I am using stream reader in my routine, but I have a problem.

I have decided to use the data from the serial port directly. I get an error when I try to use the variable line that I have just read from the port. I do not know what I am doing wrong.

Thanks.

        int tickStart = 0;
        System.IO.Ports.SerialPort port;
        public string portname;
        public Parity parity;
        public int BaudRate;
        public StopBits stopbits;
        public int databits;
        int count;

        String line;

        public string PortName
        {
            get { return portname; }
            set { portname = value; }
        }

        private void Form1_Load( object sender, EventArgs e )
        {
        //graphing stuff    
            count = 0;
            portname = "COM1";
            parity = Parity.None;
            BaudRate = 9600;
            stopbits = StopBits.Two;
            databits = 8;
            port = new System.IO.Ports.SerialPort(portname);
            port.Parity = parity;
            port.BaudRate = BaudRate;
            port.StopBits = stopbits;
            port.DataBits = databits;
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.Open();
            count = 0;
            }


        void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
          try
            {
             line = port.ReadLine(); 
              count++;
              }
          catch (Exception ex)
             {
              MessageBox.Show(ex.Message);
          }
         }



        private void timer1_Tick( object sender, EventArgs e )
        {
            //graphing stuff
            if (list == null)
                return;

            // Time is measured in seconds
            double time = (Environment.TickCount - tickStart) / 1000.0;

            double value = double.Parse(line);
            list.Add(time, value);
           //graphing stuff

          }

in timer1_Tick, add an additional check for 'line == null', in addition to 'list == null'.

Assuming that your data comes in no more often than the firing rate of your timer, and, that the data received arrives all in one burst.

If I understand correctly, what happens is that the timer ticks before a value has been read from the port for the first time. This means that line == null still, so you get the error.

The best solution would be to not start the timer before at least one line is read, but checking for null would also do the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM