Solved

Receiving Data Via Telnet Connection

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

895 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now