views:

662

answers:

3

I inherited an Excel VBA spreadsheet. I have been tasked to convert it to VB.

The application acquires data from a PLC using a DCOM object (I think). The following code runs the sub SBR1Select when MX1.0 is updated.

Sub Workbook_Open()
    ActiveWorkbook.SetLinkOnData "dassidirect|strandburner!MX1.0", "SBR1Select"
End Sub

When I open Control Panel -> Administrative Tools -> Component Services, and then navigate to Console Root -> Component Services -> Computers -> My Computer -> DCOM Config the item "DASSIDirect" is listed, which I believe is what Excel references.

How do I connect VB code to the PLC through the DASSIDIRECT component?

NOTE: DASSIDIRECT is also a Windows service (not sure if this helps answer the question).

+1  A: 

I did some work with PLCs along time ago and asked one of my old colleagues. He says you need InTouch from WonderWare which has a set of .Net 2 compatible assemblys that will work with DASSIDIRECT to give you what you need.

Here's the latest version and I hope it is what you are looking for.

MaSuGaNa
A: 

Wow, DDE. DDE, as I am sure you are aware is fairly old. Network DDE has already been depreciated and will no longer work in Vista. I can only imagine that trend is likely to continue. However the DDE API in user32 still works (the reference is here: http://msdn.microsoft.com/en-us/library/ms674605(VS.85).aspx) and you can call that from .net apps, with a little work. The only examples I could find were for VB6 and they were old even then. As I am sure you can imagine it's a little painful. At any rate I dug this example out of API-Guide (from Mentalis). It's not .Net but it ought to get you started anyway:

'This application shows you how to use DDE to create a better PrevInstance-function.
'Compile this sample and start it two times
'The first instance minimizes itself and when the second instance is started, it will
'send a message to the first instance to maximize itself.

'in a module
Public Const VBServerName = "TestServer"
Public Const VBTopicName = "SHOW_YOUR_MAIN_WINDOW"
Public Const XCLASS_BOOL = &H1000
Public Const XTYPF_NOBLOCK = &H2 ' CBR_BLOCK will not work
Public Const XTYP_CONNECT = &H60 Or XCLASS_BOOL Or XTYPF_NOBLOCK
Public Const CP_WINANSI = 1004 ' default codepage for windows & old DDE convs.
Public Const SW_RESTORE = 9
Public Const DDE_FACK = &H8000
Public Const XCLASS_FLAGS = &H4000
Public Const XTYP_EXECUTE = &H50 Or XCLASS_FLAGS
Public Const DNS_REGISTER = &H1
Public Const DNS_UNREGISTER = &H2
Public Declare Function DdeInitialize Lib "user32" Alias "DdeInitializeA" (pidInst As Long, ByVal pfnCallback As Long, ByVal afCmd As Long, ByVal ulRes As Long) As Integer
Public Declare Function DdeCreateStringHandle Lib "user32" Alias "DdeCreateStringHandleA" (ByVal idInst As Long, ByVal psz As String, ByVal iCodePage As Long) As Long
Public Declare Function DdeConnect Lib "user32" (ByVal idInst As Long, ByVal hszService As Long, ByVal hszTopic As Long, pCC As Any) As Long
Public Declare Function DdeNameService Lib "user32" (ByVal idInst As Long, ByVal hsz1 As Long, ByVal hsz2 As Long, ByVal afCmd As Long) As Long
Public Declare Function DdeFreeStringHandle Lib "user32" (ByVal idInst As Long, ByVal hsz As Long) As Long
Public Declare Function DdeQueryString Lib "user32" Alias "DdeQueryStringA" (ByVal idInst As Long, ByVal hsz As Long, ByVal psz As String, ByVal cchMax As Long, ByVal iCodePage As Long) As Long
Public Declare Function DdeUninitialize Lib "user32" (ByVal idInst As Long) As Long
Function DdeCllback(ByVal uType As Long, ByVal uFmt As Long, ByVal hConv As Long, ByVal hszTopic As Long, ByVal hszItem As Long, ByVal hData As Long, ByVal dwData1 As Long, ByVal dwData2 As Long) As Long
    DdeCllback = Form1.AppDdeCallback(uType, uFmt, hConv, hszTopic, hszItem, hData, dwData1, dwData2)
End Function
'in a form
Dim isRun As Boolean, idInst As Long, hszVBServer As Long, hszVBTopic As Long
Dim hconvVBServer As Long, dderesult As Long
Private Sub Form_Load()
   'KPD-Team 2000
   'URL: http://www.allapi.net/
   'E-Mail: [email protected]
   If DdeInitialize(idInst, AddressOf DdeCllback, 0, 0) Then Exit Sub
   hszVBServer = DdeCreateStringHandle(idInst, VBServerName, CP_WINANSI)
    hszVBTopic = DdeCreateStringHandle(idInst, VBTopicName, CP_WINANSI)
    'try to find the first instance
    hconvVBServer = DdeConnect(idInst, hszVBServer, hszVBTopic, ByVal 0&)
    If hconvVBServer Then
        Unload Me
        Exit Sub
    End If
    DdeNameService idInst, hszVBServer, 0, DNS_REGISTER
    Me.WindowState = vbMinimized
End Sub
Private Sub Form_Unload(Cancel As Integer)
        DdeFreeStringHandle idInst, hszVBServer
        DdeFreeStringHandle idInst, hszVBTopic
        'only unregister the DDE server for first instance
        If isRun Then
            If DdeNameService(idInst, hszVBServer, 0, DNS_UNREGISTER) Then
                MsgBox "in ServiceUnRegister", vbOKOnly, "Error"
            End If
        End If
        DdeUninitialize idInst
End Sub
Function AppDdeCallback(ByVal wType As Long, ByVal wFmt As Long, ByVal hConv As Long, ByVal hszTopic As Long, ByVal hszItem As Long, ByVal hData As Long, ByVal lData1 As Long, ByVal lData2 As Long) As Long
        Dim iCount As Long, Buffers As String, Ret As Long
        Select Case wType
            Case XTYP_CONNECT
                iCount = DdeQueryString(idInst, hszTopic, vbNullString, 0, CP_WINANSI)
                Buffers = Space(iCount)
                DdeQueryString idInst, hszTopic, Buffers, iCount + 1, CP_WINANSI
                If Buffers = VBTopicName Then
                    Me.WindowState = vbNormal
                    'add any code for the first instance have found the second one is launch
                    Ret = DDE_FACK
                End If
                AppDdeCallback = Ret
            Case XTYP_EXECUTE
                AppDdeCallback = Ret
        End Select
End Function
Oorang
A: 

The easiest way to handle this is to use VB 6 and create an "ActiveX EXE" which communicates DDE to SIDirect. Doing DDE from VB 6 is trivial. Then expose create a primary interop assembly for the VB 6 module, using tlbimp, or simply reference the EXE as a COM component from your C# code.

zumalifeguard