• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 608
  • Last Modified:

Receiving Data Via Telnet Connection

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
John (Yiannis) Toutountzoglou
Asked:
John (Yiannis) Toutountzoglou
  • 2
  • 2
1 Solution
 
bansidharCommented:

>> 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
 
John (Yiannis) ToutountzoglouAuthor Commented:
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
 
John (Yiannis) ToutountzoglouAuthor Commented:
ok....I found my previouses post answer....BackgroundWorker ...thank you very much
0
 
bansidharCommented:
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now