Page 1 of 1

Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 22nd, 2013, 11:22 am
by Pidea
Having looked at the various community solutions to control FreeStyler none of them meet my needs so I'm in the process of writing my own app which I intend to release to the community when it's finished.

The problem I'm having is that I'm using Visual Basic 2010 and the sample code for talking to FS is for VB6. As a result there's a couple of problems as VB 2010 doesn't support the VarPtr function. I've found a work around but it's still throwing an error.

Here's my version of the VB code from the PDF in the documentation folder with comments to show where I've made changes:

Code: Select all

Public Class frm_Showtime

    ' Declare function to talk to FreeStyler
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    ' VB6 called this a type
    Private Structure COPYDATASTRUCT
        Public dwData As Long
        Public cbData As Long
        Public lpData As Long
    End Structure

    Private ThWnd As Long
    Private Const WM_COPYDATA = &H4A
    Private Const WM_USER = &H400
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    Sub SendData(ByVal Functioncode As Byte, ByVal Length As Long, ByVal arrValues() As Byte)
        Dim cdCopyData As COPYDATASTRUCT
        Dim DMXvalues() As Byte
        Dim teller As Byte
        Dim n As Integer
        Dim i As Integer
        cdCopyData.dwData = Functioncode
        cdCopyData.cbData = Length
        ' This line is the problem: cdCopyData is a structure of longs, bytes are fed into VarPtr
        ' which no longer exists in VB2010 so I've used a function I found on the internet as a
        ' substitute but it returns an integer
        cdCopyData.lpData = VarPtr(arrValues(0))

        ThWnd = FindWindow(vbNullString, "FS")
        If ThWnd > 0 Then
            i = SendMessage(ThWnd, WM_COPYDATA, Me.ThWnd, cdCopyData)
        End If
    End Sub

    Public Function VarPtr(ByVal o As Object) As Integer
        Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)
        Dim ret As Integer = GC.AddrOfPinnedObject.ToInt32
        GC.Free()
        Return ret
    End Function
End Class
The error is here:

Code: Select all

i = SendMessage(ThWnd, WM_COPYDATA, Me.ThWnd, cdCopyData)
in the SendData function. VB is complaining that:
Value of type 'WindowsApplication1.frm_SHowtime.COPYDATASTRUCT' cannot be converted to 'Long'
Now I'm a PHP programmer who last dabbled with VB back when version 1 came out (I interfaced it to a FORTRAN library - shows my age !).

The problem is that the variable cdCopyData is defined as containing longs and sendMmessage expects this but cdCopyData.lpData contains an integer which comes from the incoming byte values.

This is all Martian to me and I've spent hours searching the internet to try and find a way of converting the output from the VarPtr function to a Long but nothing works. The Clng function doesn't fix the problem when it's applied as here:
Public Function VarPtr(ByVal o As Object) As Long
Dim GC As System.Runtime.InteropServices.GCHandle = System.Runtime.InteropServices.GCHandle.Alloc(o, System.Runtime.InteropServices.GCHandleType.Pinned)
Dim ret As Long = CLng(GC.AddrOfPinnedObject.ToInt32)
GC.Free()
Return ret
End Function
How do I get around this stumbling block ?

Thanks in advance

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 22nd, 2013, 4:10 pm
by Mattotone
Send Raph the developer an email, freestyler is written in Vb so maybe he can help

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 22nd, 2013, 4:14 pm
by Pidea
Thanks Matt, I have sent him an email about something else and am waiting a reply. I know he's busy but I want to progress this so at the moment I'm playing with your library that you made. Just PM'd you actually about a small issue with it.

Phil

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 26th, 2013, 7:12 am
by Pidea
Just an update: as FS is written in VB6 and I'm using VB 2010 it's not possible to talk directly to FS as there are too many issues (missing commands, changed/missing data types etc) but it is possible using a third-party class such as the one written by Mattotone.

With his help (thanks Matt) I've got my app toggling the blackout now but I'm still struggling with the sequence of commands necessary to load a sequence into a cue as the included documentation doesn't make it clear which commands are needed to do this and the sequence that they need to be carried out in?

I can see that you need to use SendData with the function codes 5 (to load the sequence) followed by function code 6 (to set the options) and then (possibly) function 7 (to go to the first step) but do you need to send anything before or after this ?

Does anyone have this information that they could share ?

Ta

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 26th, 2013, 9:07 am
by Mattotone
Heres the debugger i used when creating GoMonkey.
It shows that sequences names are being sent properly and also the function code sent.

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 26th, 2013, 9:34 am
by Pidea
Thanks Matt, that's very helpful. It shows that it's getting the function code but that the dmx data length is zero and that the dmx data array is null. Hmmm ....

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 26th, 2013, 10:00 am
by Mattotone
I wonder if I compile the class into a dll if that would work any better. Il also generate the commands as bytes and see if you can send that.

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 26th, 2013, 10:05 am
by Pidea
Thanks Matt, this is really wierd. I can check the byte array that I'm sending and it's got the data in it, the declaration at the top of my VB code is correct otherwise it couldn't find the function (and it does).

Life is so much simpler in the world of PHP ;-)

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 26th, 2013, 11:24 pm
by Mattotone
Heres A DLL file iv compiled of the class.

FreestylerConnection.sender.sendMessageFunction(byte[] message, int functionCode);
FreestylerConnection.sender.sendCommandFunction(int command, int value)

Iv also encoded a string into a byte array for you to test encase its the conversion from string to byte that is the problem.
String "00Green Chase RVS.chb"
byte[] message:= 48,48,71,114,101,101,110,32,67,104,97,115,101,32,82,86,83,46,99,104,98

matt

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 26th, 2013, 11:33 pm
by Pidea
Thanks for doing that Matt, really appreciate it. However, I'm getting an error when adding it to my VB project:

"Warning 4 Namespace or type specified in the Imports 'FS_Go_Monkey' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases."

My guess is that the sendMessageClass needs to be public and not private so needs to be recompiled ? I'm on Windows XP 32-bit of it makes any difference ...

Also, what's the namespace that you have used in the DLL as I need to reference that too. It's late here in the UK so hopefully you'll see this in the next few hours.

cheers

Phil

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 27th, 2013, 8:40 am
by Mattotone
Hi phil,

You was up later then I was lol.
Name space freestylerConnection
The class is sender
Methods are static so can be called inline

FreestylerConnection.sender.sendMessageFunction(byte[] message, int functionCode); // renamed SendCh message
FreestylerConnection.sender.sendCommandFunction(int command, int value)// renamed old sendMessageFunc

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 27th, 2013, 11:18 am
by Pidea
Thanks for those parameters Matt, I changed everything in my VB code and dropped yesterdays DLL in to the project and managed to connect as before. In other words, I can toggle blackout using the sendCommandFunction but can't send any DMX data.

I reckon that it's something to do with how the byte data is being handled within your C# library. It obviously works for you on your machine but it doesn't work here on a different environment.

I've spent a while this morning looking at your C# code and noticed that the declarations at the top where different - they are supposed to be IntPtr not int's apparrently ! So I went ahead and changed these and then had loads of errors due to mismatched types.

I've fixed all of these (with the help of Google as C# is not a language I know) and it now compiles but, guess what, nothing happens so I think it's time to bite the bullet and learn C# :o

Could you post the code to your listener class that you sent over yesterday ? I'll have a tinker with that and see if I can get it to read the DMX values that are getting sent in - it'll be easier than doing it with a library.

Ho hum !

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 27th, 2013, 11:36 pm
by Mattotone
hi phil,

Module Module1

Sub Main()
Dim b2() As Byte = New Byte() {48, 48, 71, 114, 101, 101, 110, 32, 67, 104, 97, 115, 101, 32, 82, 86, 83, 46, 99, 104, 98}
FreestylerConnection.sender.sendMessageFunction(b2, 5)
End Sub

End Module

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 28th, 2013, 12:12 am
by Mattotone
New DLL file tested on windows 7 x64 and x86 using VB2010
You may need to run your app as Administrator

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 28th, 2013, 12:35 am
by Pidea
Hi Matt, now there's something happening but I can't quite put my finger on it ...

IF I use the latest DLL and run your VB code nothing happens - no cues are added to the cue window.
However, if I run your your latest message tester then run your VB code again, a cue called Green Chase RVS appears in slot 00.

This is a massive step forward - we've got some communication going on but it appears that I need to run your latest tester in order to flush any output ? Interestingly, the old version of the test ran as a Windows Forms app but the latest one is command line only - is that right ?

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 28th, 2013, 12:40 am
by Mattotone
Ah. The new tester is written vb its the compiled code I posted. The console app is sender and the win forms is receiver.

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 28th, 2013, 12:46 am
by Pidea
As in this:

Code: Select all

Sub Main()
Dim b2() As Byte = New Byte() {48, 48, 71, 114, 101, 101, 110, 32, 67, 104, 97, 115, 101, 32, 82, 86, 83, 46, 99, 104, 98}
FreestylerConnection.sender.sendMessageFunction(b2, 5)
End Sub
Interesting, I'll have to ensure that the function that I use to turn a string into a byte array outputs the same.

I've got a long drive tomorrow to Norfolk (five hours+ from here) so I'm off to bed now but I'll take a look at this tomorrow evening. I'll let you know how I get on. Feeling much more optimistic now :-)

Thanks again for your help !

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: March 31st, 2013, 11:18 pm
by Pidea
Quick update: it looks like there are significant differences between .Net on older Windows platforms (specifically Windows XP) and the newer 64-bit versions. As a result it doesn't look like it's possible for a Visual Basic app to talk to Freestyler if they are both running on Windows XP unless the VB app is developed in VB6 which is what FS was developed in.

But ....

VB 2010 (and presumably later versions although I haven't confirmed this) can talk to Freestyler using Matt's library on the 64-bit platforms (Windows 7 and Windows 8) so development of my program continues now that I've bought a Windows 8 laptop.

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: April 1st, 2013, 9:46 am
by Mattotone
my library has also been tested on windows 7 32 bit, so can confirm that windows 7 32bit is supported.
I dot have a xp development machine to compile my code, but will test the compiled versions on it.

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: April 16th, 2013, 11:57 pm
by Mattotone
any updates

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: April 17th, 2013, 12:02 am
by Pidea
Hi Matt, yes I've made lots of progress now that I'm developing on Windows 8 on a new laptop. I just couldn't get anywhere on Windows XP for some unknown reason.

I'm just adding, er, doing battle with MIDI support so that I can trigger sequence changes from my Roland percussion pad which outputs MIDI and then I've got a few bugs to fix before I'll release it.

All being well, that'll be in the next few days.

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: April 18th, 2013, 1:03 pm
by Mattotone
All good, I also battled with midi as theres not many examples or documentation out there. Coupled with a lack of pInvoke knowledge. Managed to do it although I cant get midi device names.

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: April 18th, 2013, 2:30 pm
by Pidea
Take a look at this class Matt: https://code.google.com/p/midi-dot-net/ It's what I used and allows you to get MIDI names. The documentation is a bit cryptic but I figured it out in the end - I'm just too old school to understand all this class stuff !

But I'm getting there !

Re: Getting Visual Basic 2010 to talk to FreeStyler

Posted: May 3rd, 2013, 3:53 pm
by Pidea
Hey Matt, finally got my app live: viewtopic.php?f=38&t=5217

Thanks for your help again !