简体   繁体   中英

Writing an array to a file in one row

Over a serial port I read in some data sent from a weighing scale. It's 8 blocks. That looks like

Datum     07.12.2015
Zeit      10:29:43
Artikel   Testxyz

and so on.

I manipulate the data and then write it to an CSV file. My problem is somehow the format of writing the data to the file. Firstly the data was each written on a new line, so like it was reading in. But I want to have all 8 in one row and then the next block of the 8 information in the new line and so on. Now it is in one line but therefor I got

System.String[]System.String[]System.String[]

instead of

07.12.2015 10:29:43 Testxyz

This problem I had before and fix it but then I haven't got it in one row. Now I have it in one row but again this System.String[]. It's frustrating.

Following the code section:

public static void Read()
{
    string pathImportFile = @"C:\Dokumente und Einstellungen\All Users\Desktop\WaageLAU";
    string pathImportFileDate = "Heute";
    string msgIn = "msgIn";
    string msgInValid = "msgInValid";
    string SpaltenBezeichnungen = "Datum\tZeit\tArtikel\tBrutto\tTara\tNetto\tHoch\tNiedrig";
    string first = "first";
    string second = "second";
    string third = "third";
    string fourth = "fourth";
    string fifth = "fifth";
    string sixth = "sixth";
    string seventh = "seventh";
    string eighth = "eighth";

    // seperators
    string[] Datum = new string[] {"Datum"};
    string[] Zeit = new string[] {"Zeit"};
    string[] Artikel = new string[] {"Artikel"};
    string[] Brutto = new string[] {"Brutto"};
    string[] Tara = new string[] {"Tara"};
    string[] Netto = new string[] {"Netto"};
    string[] Hoch = new string[] {"Hoch"};
    string[] Niedrig = new string[] {"Niedrig"};

    while (_continue)
    {   
        try
            {
                msgIn = _serialPort.ReadLine(); 
            }catch (TimeoutException) { }

        if(msgIn != "msgIn" && msgIn != msgInValid)
        {
            msgInValid = msgIn.Replace("\t", "");

            if(msgInValid != "msgInValid")
            {
                if (msgInValid.Contains("Datum"))
                    {           
                        string date = DateTime.Now.ToString("yyyyMMdd");            
                        pathImportFileDate = pathImportFile + " " + date + ".csv";
                        first = msgInValid.Split(Datum, StringSplitOptions.RemoveEmptyEntries).ToString();  
                    }
                else if (msgInValid.Contains("Zeit"))
                    {second = msgInValid.Split(Zeit, StringSplitOptions.RemoveEmptyEntries).ToString();}
                else if (msgInValid.Contains("Artikel"))
                    {third = msgInValid.Split(Artikel, StringSplitOptions.RemoveEmptyEntries).ToString();}
                else if (msgInValid.Contains("Brutto"))
                    {fourth = msgInValid.Split(Brutto, StringSplitOptions.RemoveEmptyEntries).ToString();}
                else if (msgInValid.Contains("Tara"))
                    {fifth = msgInValid.Split(Tara, StringSplitOptions.RemoveEmptyEntries).ToString();}
                else if (msgInValid.Contains("Netto"))
                    {sixth = msgInValid.Split(Netto, StringSplitOptions.RemoveEmptyEntries).ToString();}
                else if (msgInValid.Contains("Hoch"))
                    {seventh = msgInValid.Split(Hoch, StringSplitOptions.RemoveEmptyEntries).ToString();}
                else if (msgInValid.Contains("Niedrig"))
                    {               
                        eighth = msgInValid.Split(Niedrig, StringSplitOptions.RemoveEmptyEntries).ToString();                       

                        String[] line = new string[] {first, second, third, fourth, fifth, sixth, seventh, eighth};

                        if(!File.Exists(pathImportFileDate))
                        {
                            try
                                {
                                    using (System.IO.StreamWriter fileImport = new System.IO.StreamWriter(pathImportFileDate,true))
                                    {
                                        fileImport.WriteLine(SpaltenBezeichnungen);
                                    }       
                                }catch (TimeoutException) { }
                        }

                        try
                            { 
                                using (System.IO.StreamWriter fileImport = new System.IO.StreamWriter(pathImportFileDate,true))
                                {
                                    fileImport.Write(line);
                                }       
                            }catch (TimeoutException) { }
                        Array.Clear(line, 0, line.Length);

                    }
                else
                    {
                        // nichts machen bzw. auf Daten warten
                    }
            }
        }
    }   
}

The error in in any type of the following line:

second = msgInValid.Split(Zeit, StringSplitOptions.RemoveEmptyEntries).ToString();

string.Split(...) returns a string-array and if you call ToString() on a string-array you get the value string[] wich is then stored in second . You have to save the coresponding value at .Split(...)[0] into second, third, etc. .

The correct line should be:

msgInValid.Split(Zeit, StringSplitOptions.RemoveEmptyEntries)[0];
//or
msgInValid.Split(Zeit, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();

FirstOrDefault will not raise any errors if there is no entry present in the array.


Sample:

string first = "Datum     07.12.2015";
var k = first.Split(new string[]{"Datum"}, StringSplitOptions.RemoveEmptyEntries)[0];
Console.WriteLine(k.Trim());

For writing the text correct into a file:

Just replace the code line:

fileImport.Write(line);

with

fileImport.Write(string.Join(",", line));

If you want to write each following data-bucket to a new line use WriteLine .

NOTE: this code will not write anything if the serial port does not send or skips the value for Niedrig .

Why no declare your separators as String only instead of String[] . And then you can replace fileImport.Write(line); with fileImport.WriteLine(String.Join("\\t", line));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM