S7NetPlus / s7netplus

S7.NET+ -- A .NET library to connect to Siemens Step7 devices
MIT License
1.33k stars 588 forks source link

Writing a DInt to DB. I got stuck on this matter. #139

Closed Mallowan closed 6 years ago

Mallowan commented 6 years ago

Hello, Trying to write a DInt, followed provided code samples and Documentation. I've got an exception as shown below. 2018-06-28 23_56_59-s7net_01 debugging - microsoft visual studio administrator and some info from TIA DB's graph. 2018-06-28 23_59_06-siemens - c__users_ngc_user_documents_automation_s7net_older_ibs_s7net_older_i Could anyone give me some advices? Thanks in advance!

Mallowan

mycroes commented 6 years ago

Please provide the StackTrace of the Exception so we can trace the origin.

Mallowan commented 6 years ago

deleted

Mallowan commented 6 years ago

I think this is my mistake as was with Class reading couple of months ago. But where this is? I have no clue.

rapha-dev commented 6 years ago

Hi @Mallowan, you have a syntax error in your variable. DB8.DBD2.0 is not valid, you may use DB8.DBD2 for DINT.

Found it in the Wiki?

Mallowan commented 6 years ago

Hi @rapha-dev, long time, no see! ) DB8.DBD2.0 this is normal absolute addressing for Step7 and for TIA Portal, if it is turned on to absolute addressing in its settings. Perhaps I did it reflexively. There's a lot of work for a single brain. The heap of hardware, two apps, different prog. languages and so one... )

Mallowan P.S. just checked your version. I've got one more exception.

rapha-dev commented 6 years ago

I'm pretty sure it is a syntax error in Step7, unsure about TIA. Can you post exception details please?

Mallowan commented 6 years ago

This is from second exception. that I've got after DB's address correction. Please note that I use TIA v15. Reading from (e.g. DB9) is going excellently.

rapha-dev commented 6 years ago

Yeah, dealing with multiple projects at the same time can be exhausting.

The first (essential) part of your exception detail is missing, it should be written before at S7net_01.Par_Mixer.Par_Mixer_FormClosed....

Mallowan commented 6 years ago

espesially considering that some part of brain is busy with FIFA World Cup games. ) I will look at exception one more time.

It seems I missed nothing. There's nothing before

rapha-dev commented 6 years ago

Right, you posted the StackTrace, important part here is NullreferenceException. StackTrace shows where it was thrown, in your case in the FormClosed method.

Same code works when you replace DB8 with DB9?

Mallowan commented 6 years ago

it seems, I've found where the issue is. Just created a button_event handler in MainForm and sent to DB8 a value. That went well. DB8 had got a value. However, writing doesn't work from events that are situated in SubForm. ( There's no syntax errors/ fails there, though. Except an exception that occurs while app running, of course. So I have no clue what difference between MainForm and SubForm in this term. Could you simulate that at your environment? ОК, I've got what did you mean... here is previous peaсe..

System.NullReferenceException was unhandled HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=S7net_01 StackTrace:

So question is 'what does miss in SubForm that doesn't cause a syntax error?'

rapha-dev commented 6 years ago

Looks like the plc instance is not accessible then. It would produce a NullReferenceException as well.

Mallowan commented 6 years ago

Could you simulate that at your environment in two Forms? How to do plc instance acessible in SubForm? Please note that there's no any syntax errors there and IntelliTrace shows all plc.properties and so on.

rapha-dev commented 6 years ago

That is something common, when you create an instance of a class (or anything else) and it isn't static you cannot access the instance from outside.

You should pass the plc instance (or a wrapper if you have programmed such) to the Form or instanciate a new plc in the SubForm (which might be not the right way to do so).

Another option is a static wrapper to use from any context (Form). The static wrapper class cann handle all requests to a single plc instance.

Mallowan commented 6 years ago

It's hard to understand... when there's no syntax error and nothing working at the same moment...

It would be nice to look at a sample for the second Form. Both variants. )

rapha-dev commented 6 years ago

Maybe this helps to understand why you get that NullReferenceException, will try to get an example.

@Mallowan Made an example how to pass a Plc instance to another Form.

mycroes commented 6 years ago

Tagging this as invalid because it doesn't seem related to S7NetPlus in any way. I'll leave it open for now because I guess you're almost there, but I'd rather not have totally unrelated issues here.

rapha-dev commented 6 years ago

Sure, better to discuss in another repo (see above).

Mallowan commented 6 years ago

@mycroes, Hi there, Put yourself in my shoes and try to write on (e.g. Stackoverflow) like: 'I've got an exception while running HMI based on S7netPlus library and don't know whether this exep comes from used library itself or I made some mistakes in C# code because I'm newbie, or it's double issue at all.' Like it really has been. I guess most of site members will make big eyes on this request. Mallowan.

@rapha-dev , example, this is great stuff, THANK YOU A LOT. Finally wrote to DB from SubForm. )

if (MainFormsPlc != null) { var value = MainFormsPlc.Read("DB9.DBD2"); } tell me please for what do you check if MainFormsPlc is not equal 'null'? I'm asking because I have in my code following: private Plc plc = null; And it's working.

Thanks in advance!

rapha-dev commented 6 years ago

It is an example for reading in a SubForm. var value can be anything else, it just should demonstrate how to use the passed Plc instance without creating a new instance in every Form.

Do you mind if we discuss in my example repo as it really doesn't belong here? https://github.com/rapha-dev/examples/issues

mycroes commented 6 years ago

Dear @Mallowan (sincerely with best intents, no sarcasm),

Put yourself in my shoes [...]

I did, that's why I didn't close the issue. I've labeled it so that if someone is searching for actual issues to fix (like me, since I'm trying to get S7NetPlus into better shape) they can skip issues that are not really related to S7NetPlus. There is no offense meant by labeling the issue 'invalid' and I think you should be very thankful for @rapha-dev for the time he spent on helping you. Also, you should try to avoid taking things personally (whenever possible of course) when people offer criticism that can help you improve your code (or code related skills). What's the chance you actually considered putting yourself in my shoes?

[...] (e.g. Stackoverflow) like [...]

StackOverflow has some clear guidelines. A very tiny excerpt:

Include just enough code to allow others to reproduce the problem. For help with this, read How to create a Minimal, Complete, and Verifiable example.

If you would've taken such advice, you would've concluded that you didn't hit an issue in S7NetPlus. We're not here to support beginning programmers. That doesn't mean we don't want to help you, but you can probably get way better support in a place that is meant to support beginning programmers. One such place that I can recommend is codingame.com. It is in no way related to S7NetPlus, but it offers a nice way to learn coding (although it's not a tool to learn coding per se, it offers coding challenges of different levels of complexity).

Now back to your issue (because I don't mind to help you, I just want to keep the issues clean): maybe you should try to shrink your problem domain. You're having multiple forms, you're trying to share stuff and my guess is you're new to programming and not just new to C# (again, no offense meant, just guessing / stating). Try to cut stuff into pieces. Create a sample application that reads from PLC but doesn't involve any UI (so create a command line program). Create a sample program that does involve a UI and tackles a different part of your project. If I want to test something I often create a small project which I name [feature]Sandbox, here's my current list:

Besides sandboxes I also use the C# Interactive window (in Visual Studio) a lot, just Google to see how you can make use of it.

Now even more closely related to your issue. I'd be happy to take a look at your code, but S7NetPlus is not the place where it should end up. If you can create your own repository I can help you there. When you have a repository I can easily fetch your code to reproduce any issues and I can provide Pull Requests to fix them (if you so desire).

Last but not least, S7NetPlus is missing a sample program. A project like S7NetPlus should be a community effort (although it seems it has mostly been a single maintainer at a time). A community is not just made up of (good) developers. A community needs users and as a project S7NetPlus could use contributions from those users as well. So maybe you could actually provide a sample implementation that we can include in the repository for future reference for new users. I'd be happy to assist in creating such a sample, if you don't feel like contributing it I will probably end up doing it myself anyway just as well...

To summarize, I hope I have given you some pointers that can assist you in your learning challenge. If you want help with your project, I'll be happy to help but I do expect that you create a repository for your program in that case. Last but not least I welcome you to contribute to the S7NetPlus project.

mycroes commented 6 years ago

Closing this, feel free to open a new issue if you find any problems. Also, I advice you to use the newly released S7NetPlus 0.2.0, many things have been fixed there.