The code below reads Eddie's ping (sonar) sensors and update the form text controls.
Text controls updates happen in the different thread. I had a lot of googling to make this happen. The other option I have check is the Backgroundworker-class.
Things are going slowly now, but I know based on my experience, all will accelerate later.
I have also worked with ASP-study and the VB6 "Charts Channel" with Artinsoft tool. Unfortunately it's free version allows only 10000 lines. So I have to divide the code. The "Charts Channel" is about 23000 lines. Naturally I can remove some code. Artinsoft tool isn't the perfect solution, but it gives some foundation how to do the conversion.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace SimpleEddie
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.PortName = "COM3";
serialPort1.BaudRate = 115200;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
serialPort1.Open();
string msg = "PING \r";
serialPort1.Write(msg);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = serialPort1.ReadExisting();
if (data != "")
{
string data1 = data.Substring(0, 3);
string data2 = data.Substring(4, 3);
this.Invoke((MethodInvoker)delegate {
LeftPing.Text = data1; // runs on UI thread
RightPing.Text = data2;
});
}
}
}
}
No comments:
Post a Comment