-1

I have built application forms that use a serial port C#.

I want to save the last serial port number used and COM data in the .ini file when I close the executable. So, I can use the same data for the next use of the application

        private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {

            _Ser.PortName = cBoxPort.Text;
            _Ser.BaudRate = Convert.ToInt32(cBoxBaud.Text);
            _Ser.DataBits = Convert.ToInt32(cBoxDatabits.Text);
            _Ser.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cBoxStopBits.Text);
            _Ser.Parity = (Parity)Enum.Parse(typeof(Parity), cBoxParitybits.Text);
            this.Close();
            string[] data = { cBoxPort.Text, cBoxDatabits.Text, cBoxStopBits.Text, cBoxParitybits.Text };
        }

        catch (Exception err)
        {

            MessageBox.Show(err.Message, ("Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        MessageBox.Show("Configuration has been saved","Status");
    }
Sara
  • 3
  • 3
  • You can just store the values in a text file. See [File.WriteAllText](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=netframework-4.8), [File.WriteAllLines](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealllines?view=netframework-4.8), [File.ReadAllText](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readalltext?view=netframework-4.8), [File.ReadAllLines](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.readlines?view=netframework-4.8). – Tu deschizi eu inchid Oct 20 '22 at 15:13
  • If you prefer an INI file, the following may be useful: [Reading/writing an INI file](https://stackoverflow.com/questions/217902/reading-writing-an-ini-file). Alternatively, as others have stated, you may store the data in a JSON or XML file. For XML, the following may be helpful: https://stackoverflow.com/a/73640395/10024425 and https://stackoverflow.com/a/72589790/10024425. – Tu deschizi eu inchid Oct 20 '22 at 15:18

2 Answers2

0

I think the best way is for you to serialize the object with the data you want to save and to retrieve it, just deserialize the object.

Here is an example of how to do it with XML file:How to serialize/deserialize simple classes to XML and back

A simpler way is with JSON, here is an example applied to your code.

You will need the reference Newtonsoft.Json;

_Ser.PortName = cBoxPort.Text;
        _Ser.BaudRate = Convert.ToInt32(cBoxBaud.Text);
        _Ser.DataBits = Convert.ToInt32(cBoxDatabits.Text);
        _Ser.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cBoxStopBits.Text);
        _Ser.Parity = (Parity)Enum.Parse(typeof(Parity), cBoxParitybits.Text);

Serializing the object to a .json file

string dataFile = @"c:\DataFile.json";
        string jsonString = JsonConvert.SerializeObject(_Ser);
        File.WriteAllText(dataFile, jsonString);

Retrieving data from a .json file

string recoverdata =  File.ReadAllText(dataFile);
        _Ser = JsonConvert.DeserializeObject<[put here the type of your object "_ser"] >(recoverdata);
  • I don't got it, i work on C# , and i new on it – Sara Oct 20 '22 at 11:51
  • @Sara, I updated with the option via json. It's the same principle, but easier to implement. – Anderson Constantino Oct 20 '22 at 12:36
  • THANK YOU, but what i will put here i don't get it <[put here the type of your object "_ser"] > – Sara Oct 23 '22 at 08:42
  • string[] data = { cBoxPort.Text, cBoxDatabits.Text, cBoxStopBits.Text, cBoxParitybits.Text }; – Sara Oct 23 '22 at 08:42
  • @Sara, The best way is to work with object, but performing the string array will also work. Would be like this: **string[] data = { cBoxPort.Text, cBoxDatabits.Text, cBoxStopBits.Text, cBoxParitybits.Text }; string dataFile = @"c:\DataFile.json"; string jsonString = JsonConvert.SerializeObject(data); File.WriteAllText(dataFile, jsonString);** So to retrieve: **string recoverdata = File.ReadAllText(dataFile); data = JsonConvert.DeserializeObject(recoverdata);** – Anderson Constantino Oct 24 '22 at 11:28
0

you need to generate a json and then save the port you last committed on that json. When the program opens, you should read the last recorded port from the json and open the port again.