Search code examples
c#parsingfile-format

How can I parse text from a *.PG type file?


I have some configuration (backup) files of a Siemens S7-300 PLC with the extension *.PG. Some other files have the extension *.DBF and can be read with the OLEDB Connection. But it is not possible to read the *.PG files.

I googled a lot but could not find much info about pg files. It contains also some text informations about the PLC modules, and these I want to parse. Just for testing I tried to use the https://filext.com/online-file-viewer.html online file viewer and I could read the file's text content correctly as like a text file. That means the file should be opened. But how can I do it programatically with C#?

I have found following code for dbf file that is not suitable for my case.

public override bool OpenFile(string fileName, string subFileName = "")
{
   OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(fileName) + ";Extended Properties=dBASE IV;User ID=;Password=;");
   try
   {
       if (con.State == ConnectionState.Closed) { con.Open(); }
       OleDbDataAdapter da = new OleDbDataAdapter("select * from " + Path.GetFileName(fileName), con);
       DataSet ds = new DataSet();
       da.Fill(ds);
       con.Close();
       int i = ds.Tables[0].Rows.Count;
       return true;
   }
   catch
   {
    return false;
   }             
}

Here the complete files content as HEX

07 00 0b 00 d0 07 02 03 f0 06 00 00 42 03 00 00
00 00 00 00 60 03 00 00 00 00 03 e5 a2 88 39 e7
00 00 00 00 12 2d 80 00 00 00 53 54 45 50 20 37
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20 20 04 00 4a e9 31 72 4a 10 00 00 00 00 0e 0c
c4 c0 00 01 03 00 02 02 fe 00 00 00 01 20 00 16
00 2e 00 4e 00 f4 00 f6 00 02 01 7c 02 5a 00 0e
00 f8 01 00 01 06 01 0a 00 00 01 6a 01 4c 02 06
01 2c 00 0b 00 96 00 01 00 00 65 df 0a 7e 01 00
00 01 00 75 00 18 07 ff 00 00 00 00 01 2c 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
20 20 20 20 20 20 20 20 20 20 20 20 20 20 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 0b 00 25 00 96 00 00 00 00 3e 36 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 80
f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 02 00 02 00 08 04 17 00 00 00 05 00 06
00 02 00 01 00 04 00 05 00 60 00 00 00 ff 00 00
00 00 00 02 00 00 00 00 ff 01 00 00 02 01 00 00
00 0c 00 0c 00 0c 00 00 00 00 00 ff 00 00 00 6c
00 00 04 fe 00 00 00 00 00 00 00 00 00 00 01 00
02 02 02 02 02 02 00 0f 04 71 00 0b 00 25 00 96
00 00 e6 94 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 10 ff 00 00 00 ff ff
01 00 00 00 00 00 00 00 00 de 08 03 00 1c 00 1c
00 2c 00 54 00 5a 00 92 00 d2 00 d4 00 0a 00 00
00 00 00 da 00 00 00 c0 a0 00 40 40 f0 00 00 01
00 00 00 00 00 00 00 28 38 02 02 0b be ce 00 c0
00 08 00 00 00 00 40 00 6b 00 40 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 05
10 10 20 ff 00 38 03 04 1f fd 00 ff 21 ff 28 01
00 00 00 00 01 ff 21 ff 28 01 01 00 00 00 01 01
00 01 02 ff 21 ff 28 01 01 00 00 01 01 01 80 00
03 ff 21 ff 28 01 00 01 80 00 01 01 00 4a 3e 3d
3c 3b 3a 39 38 37 35 00 00 be ce 00 03 e8 40 01
02 40 02 08 00 00 00 00 00 01 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 06
00 00 02 00 00 02 00 ea 48 04 00 1c 00 1c 00 2c
00 56 00 5c 00 9e 00 de 00 e0 00 0a 00 00 00 00
00 e6 00 00 00 cc a0 00 80 40 f0 00 00 01 00 00
00 00 00 00 00 29 38 02 02 0b 80 e0 00 c0 61 00
11 21 00 00 00 00 00 00 01 00 00 00 00 00 00 00
00 20 01 00 38 09 00 38 09 00 20 08 00 ff 00 06
00 10 10 20 00 42 04 05 1f fe 00 ff 21 ff 28 01
00 00 1f fc 01 ff 21 ff 28 01 00 00 00 02 02 ff
21 ff 28 01 01 00 00 02 01 01 00 03 03 ff 21 ff
28 01 01 00 00 03 01 01 80 01 04 ff 21 ff 28 01
00 01 80 01 01 01 00 4a 3e 3d 3c 3b 3a 39 38 37
35 00 00 80 e0 00 00 64 80 01 02 80 02 08 00 00
00 00 00 05 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 02 00 06 00 00 02 00 00 02
00 15 0c 45 41 4e ff ff ff ff 02 51 14 00 00 00
00 be 09 00 6f 00 a9 b2 01 00 4d 00 00 00 00 26
06 00 00 00 00 00 00 00 00 00 00 0c 00 00 00 0c
00 00 00 00 00 00 00 00 00 00 00 00 48 e8 01 00
00 00 00 00 00 00 00 00 02 02 02 02 02 01 00 71
da 02 00 6c 00 68 00 08 00 ed 00 94 da 01 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 2c 01 0c 00 0c
00 19 00 08 00 68 08 00 00 42 01 08 00 69 08 00
00 42 00 08 00 6a 08 00 00 42 01 0b 00 71 08 00
00 4c 00 00 00 00 0b 00 73 08 00 00 4c 00 00 00
00 0b 00 74 08 00 00 4c 00 00 00 00 08 00 05 10
00 00 41 00 08 00 06 10 00 00 41 00 0c 45 4e 00
ff ff ff ff 02 51 14 00 00 00 00 34 50 52 4f 46
49 42 55 53 28 31 29 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 01 00 01 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 0c 56 01 00
00 00 00 00 00 00 00 00 0c 43 00 00 00 00 02 02
04 10 24 00 1c 4f 41 00 16 00 00 00 9b 10 14 00
16 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 16 02 00 08 00 61 b1 01 00 42 ff 08 00
62 b1 01 00 42 ff 0e 37 00 00 16 01 00 00 1a 12
24 00 04 00 0c 4d 00 00 16 01 04 00 d8 11 24 00
0c 4d 00 00 16 01 05 00 19 14 14 00 0c 45 41 00
16 01 05 00 19 14 14 00 00 00 00 11 01 00 0b 00
b6 b2 01 00 4c 00 00 00 00 0c 4d 00 00 16 01 06
00 19 14 14 00 0c 45 41 00 16 01 06 00 19 14 14
00 00 00 00 11 01 00 0b 00 b6 b2 01 00 4c 00 00
00 00 0c 4d 00 00 16 01 07 00 90 1f 24 00 0c 45
41 00 16 01 07 00 90 1f 24 00 00 00 00 11 01 00
0b 00 b6 b2 01 00 4c 00 00 00 00 40 4e 00 00 16
02 00 00 ac 10 14 00 03 00 42 33 31 32 42 45 43
45 2e 47 53 45 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 ff ff ff ff 0c 45 41 00 16
02 00 00 ac 10 14 00 00 00 00 11 01 00 0b 00 73
b2 01 00 4c 00 00 00 00 40 44 00 00 16 02 04 00
b3 10 14 00 03 00 42 33 31 32 42 45 43 45 2e 47
53 45 00 38 20 42 69 74 20 64 69 67 69 74 61 6c
20 69 6e 70 75 74 73 00 00 00 00 00 00 00 00 00
00 00 00 00 ff ff ff ff 0c 45 41 00 16 02 04 00
b3 10 14 00 00 00 00 11 01 00 0b 00 04 b3 01 00
4c 6f 00 00 00 40 44 00 00 16 02 05 00 b3 10 14
00 03 00 42 33 31 32 42 45 43 45 2e 47 53 45 00
38 20 42 69 74 20 64 69 67 69 74 61 6c 20 69 6e
70 75 74 73 00 00 00 00 00 00 00 00 00 00 00 00
00 ff ff ff ff 0c 45 41 00 16 02 05 00 b3 10 14
00 00 00 00 11 01 00 0b 00 04 b3 01 00 4c 6f 00
00 00 40 44 00 00 16 02 06 00 b3 10 14 00 03 00
42 33 31 32 42 45 43 45 2e 47 53 45 00 38 20 42
69 74 20 64 69 67 69 74 61 6c 20 6f 75 74 70 75
74 73 00 00 00 00 00 00 00 00 00 00 00 00 ff ff
ff ff 0c 45 41 00 16 02 06 00 b3 10 14 00 00 00
00 11 01 00 0b 00 04 b3 01 00 4c 80 00 00 00 ff

Solution

  • This file contains binary data. Each byte has been assigned a meaning by the author of the file, but you can't tell what those meanings are without documentation.

    So e.g. byte 1 will mean one thing, byte 2 will mean a different thing, and so on.

    There's likely no easy way for you to reverse-engineer the content of that file, or extract anything meaningful.