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.
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 ModuleNow with Scratch make something like the following and run both.
when green flag clicked forever broadcast [start] wait(4) change [value] by (1) endThe 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.
Last edited by what-the (2012-02-13 03:36:02)
My site Offline
I just started using VB yesterday.
Interesting.....
Offline
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.
Offline
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.
My site Offline
I don't know VB.net, just some PHP.
Is it easy?
Offline
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)
My site Offline
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
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
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
Offline
Mind if i use this in Konnected? I will give credit.
Offline
nickbrickmaster wrote:
Mind if i use this in Konnected? I will give credit.
Sure but what's Konnected?
My site Offline
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![]()
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![]()
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![]()
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)
Offline
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.
Offline
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![]()
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![]()
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![]()
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