I'm using my Arduino UNO board to read and plot points on a radar display. I'm receiving serial data to my PC that looks like this:
44.16 ; 48
44.47 ; 49
44.57 ; 50
44.88 ; 51
158.01 ; 52
44.88 ; 53
The first number is the distance, the second number is the degree marker that the servo is rotated to.
I want to take this data and create an array that will plot and refresh the measurement of every degree as the servo sweeps from 1-180 degrees and back from 180 degrees to 1 degree.
Here is my code:
using System;
using System.IO.Ports;
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;
namespace SonarRadarProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var comPorts = new string[10]; //You really shouldn't have many more than 10 ports open at a time.... Right?
comPorts = SerialPort.GetPortNames();
foreach (var comPort in comPorts)
listBoxComPorts.Items.Add(comPort);
listBoxBaudRate.Items.Add(9600);
}
private void buttonConnect_Click(object sender, EventArgs e)
{
#region User Interface
/*-------------------------------------------------------*
* --------------USER INTERFACE SECTION------------------*
*-------------------------------------------------------*/
var userPortChoice = listBoxComPorts.SelectedItem;
var userBaudChoice = listBoxBaudRate.SelectedItem;
if ((userBaudChoice == null) || (userPortChoice == null))
{
MessageBox.Show("Please select both a PORT and a BAUD rate.", "Error");
return;
}
SerialPort arduinoPort = new SerialPort()
{
PortName = userPortChoice.ToString(),
BaudRate = Convert.ToInt16(userBaudChoice)
};
arduinoPort.Open();
arduinoPort.NewLine = "\r\n";
#endregion
#region Real Code
/*-------------------------------------------------------*
* --------------THE REAL CODE SECTION-------------------*
*-------------------------------------------------------*/
while (arduinoPort.IsOpen)
{
//
//BEGIN making a string array based on serial data sent via USB from the Arduino UNO
//
}
#endregion
}
}
}
I can make the array, and separate the Degrees from the Distance measurement (using string.split and string.remove). I have no idea to keep this information continuously flowing in from the serial port (with the lines of output being separated, and properly formatted) and updating my variables for this data.
Do I need to introduce a handshake? Perhaps read this data bit by bit and compile it? Read the inBuffer, instead of arduinoPort.readLine()?
TLDR: I want continuously updated (realtime?), properly formatted serial data, that I can separate in to two variables (sonarDist, and sonarDeg).
Do your worst, I'm honestly completely lost. Any advice is welcome.