none
Serial Port Data save problem in database RRS feed

  • Question

  • i am using visual studio 2015 and create Utility received data from serial port . create string DataIn variable  to store data received from serial port . data received successfully form serial port and save data in database through update  button . there is no issue in this case .

    -------------------------------------------------------------------------------------------------

    // Received data from Serial Port 
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                DataIN += serialPort1.ReadExisting();
    }
    // Save data in database through button       
    private void button3_Click(object sender, EventArgs e)
                    DatabaseData = DataIN;
                    connection.Open();
                    OleDbCommand command = new OleDbCommand();
                    command.Connection = connection;
                    command.CommandText = "insert into tblData(MYdata) values('" + DatabaseData + "')";
                    command.ExecuteNonQuery();
                    DataIN = null;
                    DatabaseData = "";
                    connection.Close();
                    MessageBox.Show(" Record save successfully");

    Data Save in Database :-

    1.D1U1802200000000000001000000S009000570101640048500851202880033800265003140015000536000280001400048004420013100098002530

    data save successfully  That's good but i want when serial port send data data automatically save in database for this purpose i am change the code and debug .data automatically save in database but not in single record they save in multi record . i am using following code ----------------

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                DataIN += serialPort1.ReadExisting();            
                DatabaseData = DataIN;
                connection.Open();
                OleDbCommand command = new OleDbCommand();
                command.Connection = connection;
                command.CommandText = "insert into tblData(MYdata) values('" + DatabaseData + "')";
                command.ExecuteNonQuery();
                DataIN = null;
                DatabaseData = "";
                connection.Close();
            }


    data divide and multi pal record and safe in following 

    1.D1U18022
    2.00000000000001000000S009000
    3.5701016400485008512028800338002650031
    4. 40015000536000280001400048004420013100098002530

    Kindly advise how to solved this issue . i am beginner in C#

    Sunday, February 25, 2018 6:46 AM

Answers

  • Thanks for your reply . you are right there is problem in end signal .

    • Marked as answer by saqsaqPK Tuesday, February 27, 2018 4:07 PM
    Tuesday, February 27, 2018 4:07 PM

All replies

  • First of all: What is you use-case?

    Just storing this kind of data in database makes not that much sense. You normally handle/interpret that data first.

    For automatically storing your data, you just use the data recieved event. For storing you data in a single row (not recored) you need a primary key in your table. Does such key exists?

    Sunday, February 25, 2018 11:50 AM
  • yes i use Primary Key .

    data proper save when i use .

    private void button3_Click(object sender, EventArgs e)


    Sunday, February 25, 2018 3:54 PM
  • The problem is that the data isn't all received at once (which is why you have the code DataIN += ...). You would need to know when the data you're receiving has reached the end. If the data being received has some kind of "end character", then maybe something like this might work:

    private string EndString = "ETX"; // or whatever it might be
    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        DataIN += serialPort1.ReadExisting();   
        if (DataIN.Contains(EndString))
        {
            DatabaseData = DataIN;
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "insert into tblData(MYdata) values('" + DatabaseData + "')";
            command.ExecuteNonQuery();
            DataIN = "";
            DatabaseData = "";
            connection.Close();
        }
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Sunday, February 25, 2018 6:04 PM
    Moderator
  • Thanks for your reply during Debug following error show .

    on  if (DataIN.Contains(EndString))

    error show ArgumentNullException Was Unhandled

    i observe  during debug process data is not pass from DataIn To  DatabaseData is Null

    DatabaseData = DataIN;

    Kindly Advise us 



    • Edited by saqsaqPK Monday, February 26, 2018 4:05 PM
    Monday, February 26, 2018 3:30 PM
  • Hello saqsaqPK,

    Your issue is related to how to read all buffers. And the serial port is a stream device, it can continuously receive data.

    You need to check the data contents to find out the end signal when all of it is received. This depends on the sender. The end signal could be a string, you have already known length of the data in advance, etc.

    Best regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 27, 2018 7:20 AM
    Moderator
  • Thanks for your reply . you are right there is problem in end signal .

    • Marked as answer by saqsaqPK Tuesday, February 27, 2018 4:07 PM
    Tuesday, February 27, 2018 4:07 PM