Solved

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

Posted on 2006-06-29
5
346 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 95
Installed softare without GUID 6 77
When i run adoquery my application freezes 26 167
draw a Christmas tree by using a nested loop? 26 62
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

823 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