0

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

  • how is the device sending the data? in a constant stream? or do you have to request each value separately from the device? why did you decide to read it byte for byte? – Mong Zhu Jul 29 '21 at 13:52
  • Use `ReadExisting` instead. The following may be helpful: https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.readexisting?view=netframework-4.8 and https://stackoverflow.com/questions/65957066/serial-to-usb-cable-from-a-scale-to-pc-some-values-are-just-question-marks/65971845#65971845 – Tu deschizi eu inchid Jul 29 '21 at 14:00
  • @MongZhu The device responds to specific commands. So for instance, for the spectral curve data I want, if I initiate the reading of a spectral curve and then send 'S0,10' that will send back the Reflectance/Transmittance over 10 wavelengths. The byte-for-byte reading was in the terminal to begin with, I would like to read more. – AndrewB9801 Jul 29 '21 at 14:03
  • The first thing you should do is look for the optical monitor communication protocol specification document and add the reference link for that document to the question, or add the optical monitor vendor and model number to ask how to obtain those communication specifications. If you do not understand the specifications of devices and communication protocols, you will continue to suffer from unforeseen phenomena. – kunif Jul 29 '21 at 14:57

0 Answers0