Sunday, December 20, 2015

Eddie Obeys but won't Give Good Feedback

Eddie is now moving from the  dashboard. Sending commands appeared to be easy, but receiving data is another story. I have tried to read sensors and update form controls and I had only partial success.

I got finally all working by reading serial data byte by byte. Trying to read receive data line by line or the whole buffer didn't work for me. It can be a driver issue or I haven't get the full idea of threading.

Now I cannot clear the receiving buffer. That means I cannot update form fields properly without tricks. All worked very well with VB5&6 in good old times.

I am using now timer to send commands like with VB5.

Anyway, here is the code:

private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
          
            byte[] buff = new byte[sp.BytesToRead];
               
                int result = sp.Read(buff, 0, sp.BytesToRead);
                /*
                foreach (byte b in buff)
                  
                */
                foreach (byte b in buff)
                {
                RecASCII += b.ToString();
                }
            //string aa = sp.ReadLine();
            //string aaa = sp.ReadExisting();
            Thread.Sleep(100);
            sp.DiscardInBuffer(); // clear buffer
            RecHEX = ASCIITOHex(RecASCII);
          
            this.Invoke((MethodInvoker)delegate
            {
                RightPing.Text = RecHEX;
                LeftPing.Text = RecHEX;
            });

            }

No comments:

Post a Comment