Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2006-06-29
5
Medium Priority
?
350 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
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…
Starting up a Project

664 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