This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-02-12 23:02:49

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Communicating With Visual Basic(.net) and Scratch

This topic is about communicating between Scratch and VB. Visual basic(.net) is a program developed by Microsoft. It has a relatively easy to use language and can do many things on a Windows computer.

Connecting Scratch with Visual Basic will enable Scratch to be a much more powerful program making it able to access and do all kinds of things.

I have found some code on the Internet which enables VB to communicate through ports and have adapted it to work with Scratch (I only have receiving data from Scratch so far and hope that some of you could help with sending).

To get started first you need Visual Basic.net and Scratch with Mesh activated.
How to activate mesh

Next make a new VB.net project with console application (You can choose Windows Application if you want. You must modify the code slightly).

Past the following code and turn on communications with Scratch.

Code:

Imports System.Net.Sockets
Imports System.Text
Module Module1
    Sub Main()
        For value As Integer = 0 To 50
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            'If host is on local computer use the following otherwise use the IP address Scratch gave you
            tcpClient.Connect("127.0.0.1", 42001)
            Dim networkStream As NetworkStream = tcpClient.GetStream()
            If networkStream.CanWrite And networkStream.CanRead Then
               'Following lines are for sending data which I have disabled. It's here to help people who want to send data to Scratch
                ' Do a simple write.
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("Is anybody there")
                'networkStream.Write(sendBytes, 0, sendBytes.Length)
                ' Read the NetworkStream into a byte buffer.
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                ' Output the data received from the host to the console.

                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                Console.WriteLine(("Host returned: " + returndata))
            Else
                If Not networkStream.CanRead Then
                    Console.WriteLine("cannot not write data to this stream")
                    tcpClient.Close()
                Else
                    If Not networkStream.CanWrite Then
                        Console.WriteLine("cannot read data from this stream")
                        tcpClient.Close()
                    End If
                End If
            End If
           
            Threading.Thread.Sleep(4000)
        Next
    End Sub
End Module

Now with Scratch make something like the following and run both.

 

when green flag clicked
forever
broadcast [start]
wait(4)
change [value] by (1)
end
The VB code and the Scratch code will update every 4 seconds. You can make it faster if you want. This was just done so that you can see the communication between them being successful.

VB will now receive data from Scratch.

If anyone would like to help me send data to Scratch please do.

Last edited by what-the (2012-02-13 03:36:02)


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#2 2012-02-12 23:05:44

CheeseMunchy
Scratcher
Registered: 2008-10-13
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

I just started using VB yesterday.

Interesting.....


6418,

Offline

 

#3 2012-02-13 00:23:54

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

If only I knew visual basic. I bought the whole darn professional of visual studio 2010 recently, and I don't know the simplest language in there.  hmm


I am currently http://blocks.scratchr.org/API.php?user=GP1&action=onlineStatus&type=imagehttp://blocks.scratchr.org/API.php?user=GP1&action=onlineStatus&type=text and I finally got over 1000 posts.

Offline

 

#4 2012-02-13 02:30:56

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

CheeseMunchy wrote:

I just started using VB yesterday.

Interesting.....

Give the above code a try. Tell me what you think. It might take you a few weeks to learn how to use the data from Scratch in VB.


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#5 2012-02-13 02:48:05

slinger
Scratcher
Registered: 2011-06-21
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

Cool, I may make something like this in pascal  tongue


http://s0.bcbits.com/img/buttons/bandcamp_130x27_blue.png

Offline

 

#6 2012-02-13 02:51:22

ProgramCAT
Scratcher
Registered: 2011-12-13
Posts: 500+

Re: Communicating With Visual Basic(.net) and Scratch

I don't know VB.net, just some PHP.
Is it easy?


Programming is an art...
Goodbye, Scratch. I am leaving because of the exams coming up at our school, though I'll check the forums once or twice a week.

Offline

 

#7 2012-02-13 03:34:06

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

ProgramCAT wrote:

I don't know VB.net, just some PHP.
Is it easy?

Quite easy, all the commands are listed as you type.

Last edited by what-the (2012-02-13 03:34:21)


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#8 2012-02-13 07:38:56

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

Nice find, what-the!

I had actually only yesterday tried experimenting with Scratch remote connections and VB, without success. It turns out I tried Ports instead of Sockets. Silly me  tongue

I'll give the code a try, even though it surprises me that .NET doesn't have an event that alerts you when a new message arrives from the socket and that you actually have to check for it yourself  hmm

By the way, for our readers, do remember that Visual Basic is a language, not a program. The program is Visual Studio Express (or Professional if you're as lucky as me  wink

Offline

 

#9 2012-02-13 12:12:42

nickbrickmaster
Scratcher
Registered: 2010-02-02
Posts: 500+

Re: Communicating With Visual Basic(.net) and Scratch

Mind if i use this in Konnected? I will give credit.


Ask me what I'm doing, wait an hour than roll a die, if it's 4-6, I'm playing Skyrim, if it's 1, I'm eating, if it's 2-3 I'm programming.
Steam: nickbrickmaster | RotMG: PwnThemAll | Minecraft: nickbrickmaster | League Of Legends: BaneOfTitans

Offline

 

#10 2012-02-13 21:38:41

what-the
Scratcher
Registered: 2009-10-04
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

nickbrickmaster wrote:

Mind if i use this in Konnected? I will give credit.

Sure but what's Konnected?


http://imageshack.us/m/64/9034/ddfss.pngMy site
Find someone post count. Click posts under username. Find number of pages. Times that by 40 for min and 60 for max and you have a rough estimate of post count.

Offline

 

#11 2012-02-13 23:31:22

GP1
Scratcher
Registered: 2009-07-06
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

LS97 wrote:

Nice find, what-the!

I had actually only yesterday tried experimenting with Scratch remote connections and VB, without success. It turns out I tried Ports instead of Sockets. Silly me  tongue

I'll give the code a try, even though it surprises me that .NET doesn't have an event that alerts you when a new message arrives from the socket and that you actually have to check for it yourself  hmm

By the way, for our readers, do remember that Visual Basic is a language, not a program. The program is Visual Studio Express (or Professional if you're as lucky as me  wink

tongue  I don't even know why I got pro, I usually use just c# and c++. But who cares, I got it for free anyway (with Dreamspark)


I am currently http://blocks.scratchr.org/API.php?user=GP1&action=onlineStatus&type=imagehttp://blocks.scratchr.org/API.php?user=GP1&action=onlineStatus&type=text and I finally got over 1000 posts.

Offline

 

#12 2012-02-14 10:09:32

nickbrickmaster
Scratcher
Registered: 2010-02-02
Posts: 500+

Re: Communicating With Visual Basic(.net) and Scratch

what-the wrote:

nickbrickmaster wrote:

Mind if i use this in Konnected? I will give credit.

Sure but what's Konnected?

It's my mod that makes it easier to "Konnect" with other programs.


Ask me what I'm doing, wait an hour than roll a die, if it's 4-6, I'm playing Skyrim, if it's 1, I'm eating, if it's 2-3 I'm programming.
Steam: nickbrickmaster | RotMG: PwnThemAll | Minecraft: nickbrickmaster | League Of Legends: BaneOfTitans

Offline

 

#13 2012-02-14 11:22:11

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Communicating With Visual Basic(.net) and Scratch

GP1 wrote:

LS97 wrote:

Nice find, what-the!

I had actually only yesterday tried experimenting with Scratch remote connections and VB, without success. It turns out I tried Ports instead of Sockets. Silly me  tongue

I'll give the code a try, even though it surprises me that .NET doesn't have an event that alerts you when a new message arrives from the socket and that you actually have to check for it yourself  hmm

By the way, for our readers, do remember that Visual Basic is a language, not a program. The program is Visual Studio Express (or Professional if you're as lucky as me  wink

tongue  I don't even know why I got pro, I usually use just c# and c++. But who cares, I got it for free anyway (with Dreamspark)

Same! I love Dreamspark, that's how I got literally half my software!

Offline

 

Board footer