Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't read structure. Help!!! #105

Closed
Mallowan opened this issue Mar 29, 2018 · 9 comments
Closed

Can't read structure. Help!!! #105

Mallowan opened this issue Mar 29, 2018 · 9 comments
Labels

Comments

@Mallowan
Copy link

Mallowan commented Mar 29, 2018

Hello.
A reading/writing Bytes and Markers went successfully. That's great!
I've tried to read data from simpliest structure.
I've got an exception. NullReferenceException 'Object reference not set to an instance of an object.'
I followed the steps described in Documentation for the library.
I can't get what's wrong in the code. Please see screens.

struct_reading
structure

thanks in advance

@marcosdvpereira
Copy link
Contributor

Try declare 8 bool properties, see issue #64 and add "get" and "set" to your public vars.

@Mallowan
Copy link
Author

Mallowan commented Apr 11, 2018

Hello there,
Of course I'll try to do it this way. Thanks a lot.
But, as far as I understood "get" and "set" are used in declaration for a class reading. If we follow this documentation, for a structure reading isn't written that there should be used "get" and "set" in structure declaration. Isn't it?
Either Documentation is poorly written or there's no difference between class reading and structure reading.

Special thank for advice about reading of entire byte. Nowhere is written in Documentation too.

Mallowan

@rapha-dev
Copy link
Member

@Mallowan
ReadStruct determines its size by field types (Boolean, Int32...) only.
ReadClass uses its properties (get, set) followed by field type to define final size.
In this case your testStruct should be fine without properties (at least contain 8 bools as @watashimeandeu recommends).

@Mallowan
Copy link
Author

Mallowan commented Apr 11, 2018

Could someone share with working code of reading a structure to textBoxes and reading a structure to a class with further output of values in textBoxes?

Mallowan

@Mallowan
Copy link
Author

Mallowan commented Apr 12, 2018

Of course I saw these tests! If I would have gotten them I wouldn't have written here my questions.
To me this is pretty complicate to get such things on C# language.
I'm just young Step 7 programmer and in area of C# I have small experience. So I need simple examples.
I would be appreciate if someone would provide a code based on real C# project, where is described how to get values from a structure and a class.
P.S. I've found some sample projects in the Web, and have got them perfectly. But these samples cover partially this theme (for instance: reading/writing a boolean variable, particular address in DB, reading/ writing a Real value). That's all. But I need more explanations which were made by this (understandable) way.

hope everyone has got what I meant.

Mallolwan

@rapha-dev
Copy link
Member

Mesta has written about S7.Net in his blog, an example is included, too. It's a good start to get familar with the library.

Communication with Siemens S7 Plc with C# and S7.Net plc driver

@Mallowan
Copy link
Author

Mallowan commented Apr 12, 2018

@rapha-dev,
First of all thank you for replying. I read this blog in the past and studied this sample. And as I wrote above this example from Mesta is pretty poor.
Content of events that was considered in the sample solution called "PLC communication":
-connection;
-disconnection;
-start;
-stop;
-set Word variable;
-set Real variable;
-set DoubleInt variable;
-set DoubleWord variable;

That's all!

There is no single word about reading in the project code;
There's no single word about a structure reading and a class reading in the project code;
In the blog the class reading itself is positioned as a special feature of the library, though!

Others guys (thank them) that have nothing to do with this library development, bothered at least to provide more samples in their sample projects (e.g. writing/reading almost all possible variables, except a structure and a class reading, of course). Otherwise I wouldn't write here.

If I'm not right, please correct me. I will apologize for what I said.

Mallowan

@rapha-dev
Copy link
Member

@Mallowan

For my own i use classes to read from PLC and single statements to write.

A class read looks like this. _plc is a S7.Net.Plc instance.

        /// <summary>
        /// Read Class/Entry from PLC
        /// </summary>
        /// <param name="dsNumber">Entry no</param>
        /// <returns>False on read error</returns>
        public bool ReadFromDb(DB dbClass, int dsNumber)
        {
            if (_plc.IsConnected)
            {
                _plc.ClearLastError();
                _plc.ReadClass(dbClass, dbClass.DBNo, ((dsNumber - 1) * dbClass.StructLen) + dbClass.Offset);        // store result in dbClass-Instance
                if (_plc.LastErrorCode == ErrorCode.NoError)
                {
                    return true;
                }
            }
            return false;
        }

Each DB has its own class.

    /// <summary>
    /// Struct of DB800 entries
    /// PAR_LOG
    /// </summary>
    public class DB800 : DB
    {
        /***************************************************************************************
         * Must not contain other public properties than fillable by PLC maintaining order !!! *
         * aka "public Type propertyname { get; set; }"                                        *
         ***************************************************************************************/

        public DB800()
        {
            // set constraints
            Offset = 50;
            StructLen = 20;
            MaxLen = 2000;
            DBNo = 800;
        }

        #region Properties filled by PLC:ReadClass
        public Int16 Id { get; set; }
        public Int32 Timestamp { get; set; }
        public Double Par_Alt { get; set; }
        public Double Par_Neu { get; set; }
        public Int16 ProgrammNr { get; set; }
        #region 2 Byte = 16 Bools
        public Boolean Bool_Neu { get; set; }
        public Boolean Bool_Alt { get; set; }
        public Boolean Reserve_2 { get; set; }
        public Boolean Reserve_3 { get; set; }
        public Boolean Reserve_4 { get; set; }
        public Boolean Reserve_5 { get; set; }
        public Boolean Reserve_6 { get; set; }
        public Boolean Reserve_7 { get; set; }
        public Boolean Reserve_8 { get; set; }
        public Boolean Reserve_9 { get; set; }
        public Boolean Reserve_10 { get; set; }
        public Boolean Reserve_11 { get; set; }
        public Boolean Reserve_12 { get; set; }
        public Boolean Reserve_13 { get; set; }
        public Boolean Reserve_14 { get; set; }
        public Boolean Reserve_15 { get; set; }
        #endregion  
        public Int16 Par_Id { get; set; }
        #endregion
    }
    public class DB
    {
        /***************************************************************************************
         * Must not contain other public properties than fillable by PLC maintaining order !!! *
         * aka "public Type propertyname { get; set; }"                                        *
         ***************************************************************************************/

        #region Contraints
        /// <summary>
        /// Offset to first entry in Bytes
        /// </summary>
        public int Offset;

        /// <summary>
        /// Length of structure (entry) in Bytes
        /// </summary>
        public int StructLen;

        /// <summary>
        /// Max count of entries
        /// </summary>
        public int MaxLen;

        /// <summary>
        /// DB number
        /// </summary>
        public int DBNo;
        #endregion
    }

My DBs have a main structure followed by identical sections, like DB800 (data logger). Therefor you can set an offset to start reading sections.

The DB800 is defined in Step7 and contains multiple UDTs following this section:
udt

Hope it helps a little.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants