Solved

Receiving Data Via Telnet Connection

Posted on 2010-11-25
4
589 Views
Last Modified: 2012-06-27
This is something New For me...
I have a controller that have 8 sockets.In each socket you can connect an alarm sensor..
the controler connects via ethernet to pc..and finally with a telnet connection there is a communication between controller and pc.
I 've read several articles about telnet and how to connect with vb.net ...
i am a little bit confused so i am starting saying these words:

1.the Controllers ip address is set to 192.168.0.190
2.in the command prompt i ping the ip so i am sure that there is a connection
3.in the command prompt  c:>telnet and after that i write:
o 192.168.0.190 10001 and i am seiing continiously every 1 second the communication ..it gives me 8 bytes 11111111
and if a sensor is connected in thecontroller in this socket it gives me a 0 (like 11111011).

questions:
1.how can i display these bytes as they are , in multiline textbox..(as 0 or 1)
2.do i have to make a connection when my application starts or just to get the connction
3.how can i translate a single bit which is reffering  to a single socket of the controller and check its value

i think these are my 3 very fisrt questions...
a.i  am using vs 2008 (vb.net)
b.i know the difference between win XP an win 7 about telnet
c.in the laptop i am asking this question i do not a vb editor to send you the code as far as this point so sorry about that.
d.if something from the above is wrong please fill free to point me..
thank you very much in advance.



0
Comment
  • 2
  • 2
4 Comments
 
LVL 11

Accepted Solution

by:
bansidhar earned 300 total points
ID: 34215432

>> 3.in the command prompt  c:>telnet and after that i write:
>> o 192.168.0.190 10001 and i am seiing continiously every 1 second the communication ..it gives me 8 bytes 11111111

>>1.how can i display these bytes as they are , in multiline textbox..(as 0 or 1)
if you are seeing 11111111 on the console you are getting a string of 8 bytes not 8bits so you don't need any byte to bit conversion. Just split the string or even the index is fine like
str=replyFromDevice()
str(0) is  My fist sensor str(1) is second and so on

>> 2.do i have to make a connection when my application starts or just to get the connction
You need to connect to the device from your application

>>3.how can i translate a single bit which is reffering  to a single socket of the controller and check its value
You will not need it unless you are seeing junk characters instead of 1 and 0 in your telnet screen.
0
 
LVL 18

Author Comment

by:John (Yiannis) Toutountzoglou
ID: 34231427
Thank you very much For Your Reply I understand you points and ....this is my Code...
It Runs ..it gives the "string" But once only ..
How can i receive continously the string from the device every second like the console..

'Namespaces here

   Private Sub SendCommands(ByVal PIPAddress As String, ByVal PPort As String)
        Dim MyIPAddress As IPAddress
        Dim ep As IPEndPoint
        Dim tnSocket As Socket
        Dim RecvString As String = String.Empty
        Dim NumBytes As Integer = 0

        MyIPAddress = IPAddress.Parse(PIPAddress.Trim)
        ep = New IPEndPoint(remoteIPAddress, CType(PPort.Trim, Integer))
        tnSocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        Dim SendBytes As [Byte]() = Encoding.ASCII.GetBytes(Command() & vbCrLf)
        Dim RecvBytes(255) As [Byte]

        Try
            tnSocket.Connect(ep)
        Catch oEX As SocketException
            MessageBox.Show(oEX.Message & " " & oEX.ErrorCode)
            Exit Sub
        End Try

        Try

            If tnSocket.Connected Then
                MessageBox.Show("connected")
                tnSocket.Send(SendBytes, SendBytes.Length, SocketFlags.None)
            End If
            Do
                NumBytes = tnSocket.Receive(RecvBytes, RecvBytes.Length, 0)
                RecvString = RecvString + Encoding.ASCII.GetString(RecvBytes, 0, NumBytes)
                txtRecv.Text = RecvString

            Loop While NumBytes = 256
            ' Disconnect
            tnSocket.Disconnect(False)

        Catch oEX As Exception
            ' Error cleanup etc needed
        End Try

        ' Cleanup
        MyIPAddress = Nothing
        ep = Nothing
        tnSocket = Nothing
        RecvString = Nothing

    End Sub

Open in new window

0
 
LVL 18

Author Comment

by:John (Yiannis) Toutountzoglou
ID: 34232164
ok....I found my previouses post answer....BackgroundWorker ...thank you very much
0
 
LVL 11

Expert Comment

by:bansidhar
ID: 34247348
great you found the answer for your second question. Some small items you may require to know.

1. call the Socket.Shutdown before calling Disconnect. This finishes what it was doing before disconnecting (You may not require it here).
2. Try reusing the same socket.
 tnSocket.Disconnect(True)
I write a lot of socket communication apps and found Windows makes a mess without socket reuse (don't know why)

check MSDN
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.disconnect.aspx

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studios 1 76
bound data table problem 2 33
Open a link in vb.net 2 16
run a stored procedure from vb.net 1 27
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question