-
Networkstream issue
Hi all,
first of all, i want you to know that i'm a complete newbie to VB .NET. I just find it fascinating what the possibilities are, that's why im trying to learn it a little bit.
Now, for the explanation of my problem. I'm trying to create a piece of software that connects to a PLC (Siemens S7-300) through TCP and then fetches data every 2 seconds.
The TCP connection is not a problem, that works fine. I also can send and read data to/from the PLC. The problem now is that i want to get/send the data every 2 seconds. (for visualization purposes).
At the moment my read actions are attached to a button.
The code is like:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Algemeen.ReadTCP()
TextBox2.Text = Algemeen.myReadBuffer(0).ToString("x2")
End Sub
And the code in ReadTCP is
Shared Sub ReadTCP()
If TCPClient.Connected = True Then
Algemeen.Stream = TCPClient.GetStream()
If Algemeen.Stream.CanRead = True Then
Algemeen.Stream.BeginRead(Algemeen.myReadBuffer, 0, 1024, New AsyncCallback(AddressOf ReadCallback), Algemeen.Stream)
End If
End If
End Sub
I've already experimented with stream.dataavailable but had no succes so far.
Is there anyone here who could help me on the right way? I'm not asking for a solution, because i want to learn it the hard way. But if someone could give me directions where i need to begin
to accomplish this, it would be wonderful!!!
Tnx in advance
Nico
-
Re: Networkstream issue
Well it strikes me as a bit of an odd thing to attempt but all regular repeating actions start with a Timer!
-
Re: Networkstream issue
Hi dunfiddlin,
I've already tried some things with a timer too, (should have typed this in the first post ;-)).
It doesnt seem to work (perhaps im doing it wrong). It tries to read the networkstream even when im not connected yet. Although it is programmed like:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer.Enabled = True
Timer.Interval = 20000
AddHandler Timer.Tick, AddressOf test2
End Sub
Private Shared Sub test2()
If Algemeen.Stream.DataAvailable = True & Algemeen.Stream.CanRead = True Then
Algemeen.ReadTCP()
Form3.TextBox2.Text = Algemeen.myReadBuffer(0).ToString("x2")
End If
End Sub
But it tries to read the data even when i'm not connected to the TCP-Server.
Any ideas what im doing wrong?
Tnx for the fast response!
Gr,
Nico
-
Re: Networkstream issue
EDIT ABove post
[HIGLIGHT]
Private Shared Sub test2()
If Algemeen.TCPClient.Connected = True Then
If Algemeen.Stream.DataAvailable = True & Algemeen.Stream.CanRead = True Then
Algemeen.ReadTCP()
Form3.TextBox2.Text = Algemeen.myReadBuffer(0).ToString("x2")
End If
End If
End Sub
[/HIGHLIGHT]
And when im connected to the tcp-server i get the following
NullReferenceException was unhandled
Object reference not set to an instance of an object.
Gr,
Nico
-
Re: Networkstream issue
Don't start/enable the Timer until the connection is established. There are a lot of things in the code you posted which don't appear to have declarations so I can't really determine which might be responsible for the Null exception.
-
Re: Networkstream issue
Hi,
The problem was idd that i tried to acces the networkstream before it was declared (stupid newbie error i guess :p)
I made some changes and now it works like a charm!!
It's kinda hard to learn when you dont have a base to build on. I searched for evening school but the courses they give are so basic.
Do you know a good site with some tutorials of VB .Net?
The next thing i'm going to try is a simple bitmap change when a bit out of the plc changes.
If i don't find it out myself, you will see a thread popping up here :)
Tnx for the help.
Gr
Nico
-
Re: Networkstream issue
There are several good posts about network communication in the code banks.
I do see a lot of people starting with Networking, but there is a lot that goes into networking and it really shouldn't be a starting point if you are just getting into programming.