I have been using a serial terminal in order to communicate with the serial port of an optical monitor.
The data I receive I'm able to insert into a mySQL table using the mySQL connector NET extension.
The problem that I'm having is that, say the recorded transmittance is 99.45%, the terminal will insert 9, 9, ., 4 and 5 as separate rows.
The data is separated by spaces so I thought that being able to identify these spaces and group the data like that may work but I couldn't figure out how to go about doing just that.
There is a lot of code in the form1.cs for other features on the terminal so here is what I believe to be the relevant code:
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;
using System.IO;
using MySql.Data.MySqlClient;
namespace CP_Serial_Port_Terminal
{
public partial class Form1 : Form
{
string sendWith;
string dataIN;
int dataINLength;
int[] dataInDec;
StreamWriter objStreamWriter;
string pathFile;
bool state_AppendText = true;
MySqlConnection myConnection;
MySqlCommand myCommand;
private void SaveDataToMySqlDatabase()
{
if(saveToMySQLDatabaseToolStripMenuItem.Checked)
{
try
{
myConnection = new MySqlConnection("server=localhost; username=****; password=****; port=3306; database=database01");
myConnection.Open();
myCommand = new MySqlCommand(string.Format("INSERT INTO `table01` (`Wavelength(nm)`,`R/T(%)`) VALUES ('','{0}')", dataIN), myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
RefreshDataGridViewForm2();
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
List<int> dataBuffer = new List<int>();
while (serialPort1.BytesToRead > 0)
{
try
{
dataBuffer.Add(serialPort1.ReadByte());
}
catch(Exception error)
{
MessageBox.Show(error.Message);
}
}
dataINLength = dataBuffer.Count();
dataInDec = new int[dataINLength];
dataInDec = dataBuffer.ToArray();
this.Invoke(new EventHandler(ShowData));
}
I'm relatively new to C# and have been working off of the work done by Catur Pebriandani on YouTube. Here is a link to a download for the full serial terminal - http://www.caturcreativeproject.com/2017/11/daftar-tutorial-pemrograman-visual-c.html look to number 13.
If you have any clarification questions please ask, I wasn't sure how much to include in this question. Thanks