Solved

Receiving Data Via Telnet Connection

Posted on 2010-11-25
4
586 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

772 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