Solved

Read ASCII stream data directly from port, using VB.net.

Posted on 2006-06-29
5
347 Views
Last Modified: 2012-05-05
Hi,

I am able to receive Data stream using Hyper Terminal and save to a log.txt file. To set up connection in my Hyper Terminal, all I need is the Host Address(IP#) and Port Number.

We want to read in the data stream from the port directly, whithout reading the log, using VB.net Windows program.

Question now is how to read in data, any idea?

Thanks,
Sheldon
0
Comment
Question by:XiaodongYan
  • 3
  • 2
5 Comments
 
LVL 15

Expert Comment

by:bpmurray
ID: 17014054
Something like this?

        Dim mySocket As Socket
        Dim myBuffer(1024) As Byte
        Dim BytesRead As Integer

        mySocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        mySocket.Connect("127.0.0.1", 80)

        BytesRead = mySocket.Receive(myBuffer, 1024, SocketFlags.None)

        mySocket.Shutdown(Net.Sockets.SocketShutdown.Both)
        mySocket.Close()
0
 

Author Comment

by:XiaodongYan
ID: 17026886
Tried and it worked! Thank you!

Now, since I have no control on the sender part, which from antenna receiver. I have to receive continue stream of characters.

How do I void missing bytes?

Thanks,
Sheldon
0
 
LVL 15

Expert Comment

by:bpmurray
ID: 17026903
What do you mean? Do you mean somehow flag that some data is missing or do you mean to somehow clean up the stream when some bytes are missing?

I think either of these situations is pretty much impossible - it depends on the data stream and the heuristics you can apply to the data.
0
 

Author Comment

by:XiaodongYan
ID: 17027814
Data in "myBuffer" needs to be saved into a log file.

I mean the possible gap between point 1 to point #2 in the following code.

For n = 0 To 500
      str = ""
      i = 0
      BytesRead = mySocket.Receive(myBuffer, 1024, SocketFlags.None)
' point #1
      Do While myBuffer(i) > 0
           str = str & Chr(myBuffer(i))
           i = i + 1
      Loop
      FilePut(FileNumber, str)
' point #2
Next n


Thanks,
Sheldon
0
 
LVL 15

Accepted Solution

by:
bpmurray earned 250 total points
ID: 17027993
You can do this more efficiently:

      BytesRead = mySocket.Receive(myBuffer, 1024, SocketFlags.None)
'
      FilePut(FileNumber, str)
        Dim mySocket As Socket
        Dim myBuffer(1024) As Byte
        Dim BytesRead As Integer
        Dim myFile as FileStream

        mySocket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
        mySocket.Connect("127.0.0.1", 80)
        myFile = File.Open("antenna.log", FileAccess.Create)
        Do While whatever_might_stop_this
             BytesRead = mySocket.Receive(myBuffer, 1024, SocketFlags.None)
             myFile.Write(myBuffer, 0, Bytesread)
        Loop

        mySocket.Shutdown(Net.Sockets.SocketShutdown.Both)
        mySocket.Close()
        myFile.Close()
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

809 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