alternative

Forum to discuss MLScript plugin that enables VBScript and JavaScript scripting within MLServer3.
mwebster
Intermediate
Intermediate
Posts: 271
Joined: Mon Mar 20, 2006 10:38 pm
Contact:

alternative

Postby mwebster » Sat Feb 23, 2008 5:19 pm

Cinemar doesn't let you access com ports from scripts, right? So to write scripts that utilize the com port on my computer I'd have to buy another plugin? Seems odd, maybe I'm wrong.

I've written scripts in vb, lua, and perl. They're ok for very simple stuff, but I've found that using the free microsoft stuff (vb or c express) I can do more and it's easier to develop the code. I'm sure there are reasons scripting is cleaner from Cinemar's end, but I would pay for a good way to get data in and out of my vb apps and mlserver, but wouldn't pay for this plugin. Currently I use the tcpsend plugin and command line to get things back and forth.

I think that letting the users do more and share the application specific stuff and give the professional developers more time to enhance the core apps would be a good thing for everyone.

Not complaining, I think the stuff I've bought has been a good value.

Matt

User avatar
DavidL
Simply Incredible
Simply Incredible
Posts: 11071
Joined: Sat Feb 08, 2003 9:39 am
Location: Metamora, Michigan
Contact:

Postby DavidL » Sat Feb 23, 2008 6:08 pm

Matt, are u using MLDatabase? Maybe that might help integration with your apps?

Yes, no access to hardware via scripts. Today would have to be via a MLGenericDevice plugin (which the script capability was just modded today to match MLScript syntax et al).

User avatar
bhiga
Expert
Expert
Posts: 854
Joined: Tue Mar 08, 2005 10:28 pm
Location: San Jose, CA
Contact:

Postby bhiga » Sat Feb 23, 2008 10:31 pm

Actually there is a way to access com ports via VBScript - I've done in the past to drive an LCD character display and Primera Composer autoloader. Unfortunately I lost that code in a crash, so I'm still trying to replicate it.

MLGenericDevice would be a better way to go as it's better integrated and portable.
- Brandon
My MainLobby stuff (plug-ins, screenshots, etc)

mwebster
Intermediate
Intermediate
Posts: 271
Joined: Mon Mar 20, 2006 10:38 pm
Contact:

Postby mwebster » Sun Feb 24, 2008 12:19 am

smoothtlk,

I haven't tried MLDatabase, but I'll check it out again. I haven't had a need to update groups of mlserver variables quickly.

Matt

mwebster
Intermediate
Intermediate
Posts: 271
Joined: Mon Mar 20, 2006 10:38 pm
Contact:

Postby mwebster » Sun Feb 24, 2008 12:34 am

bhiga,

If you run across the method for accessing com ports via VBScript, I'd love to hear some details. I searched, but couldn't find a method. I've done it in HomeSeer, but they provide the ability.

I haven't looked at it much, but I don't think I need any of the advanced features of MLGenericDevice. I just want to be able to use the com port on my pc from ML.

worf83
Senior Member
Senior Member
Posts: 131
Joined: Mon Feb 14, 2005 11:40 am
Contact:

Postby worf83 » Thu Aug 28, 2008 9:50 pm

bhiga wrote:Actually there is a way to access com ports via VBScript - I've done in the past to drive an LCD character display and Primera Composer autoloader. Unfortunately I lost that code in a crash, so I'm still trying to replicate it.

MLGenericDevice would be a better way to go as it's better integrated and portable.


Brandon,

Have you been able to replicate the code using VBScript ?

TIA

AL

User avatar
CinemarDave
Site Admin
Site Admin
Posts: 10535
Joined: Fri Feb 07, 2003 8:56 am
Location: Planet Earth
Contact:

Postby CinemarDave » Thu Aug 28, 2008 10:35 pm

This should give you a leg up.

Code: Select all

REM VBScript Example of using MSComm control for send and receive

REM ---------------------------------------------------------------------------

' NOTES:
' This is a complete example of using the MSComm control distributed with Visual
' Studio 6 to both send and receive data. The received data is stored in a text
' file. Both binary and printable text characters can be received. The text file
' is stored in the same directory as this script. The number of characters to
' receive before ending the script is settable through 'MyThreshold'.

' For maximum efficiency of burst-mode data, the event handler buffers the
' incoming data before writing it to the file. Since it is not possible to
' know how many characters may be in the MSComm control's input buffer, the
' only sure way of knowing how many characters are received is to accumulate
' the count through a call to the control's InBufferCount property. While this
' may appear to be counter-intuitive, the control does not interrupt the program
' on every received character. It is not possible to know how many characters
' are waiting without using the InBufferCount property. When the Input()
' method is called, it will take InBufferCount number of bytes (all of them) out
' of the input buffer if the InputLen property is set to '0'.

' After the threshold number of bytes are received, the event handler processes
' the contents of the receive buffer by writing the contents to the log file
' two bytes at a time with commas separating each pair of bytes.

Option Explicit

Dim objTest                             ' MSComm control reference pointer
Dim objFSO                              ' file system reference pointer
Dim objLogFile                          ' file pointer
Dim s                                   ' general purpose string
Dim path                                ' path to current directory
Dim msg                                 ' message sent out of comm port
Dim flag                                ' indicates whether or not script continues
Dim rxCnt
Dim rxBuf
Dim i

const logfile = "\log.txt"              ' file where received data is stored

const MyPort = 1                        ' COM1
const MyBaud = "57600"                  ' bps rate
const MyThreshold = 6                   ' how many characters to receive until done

const comEvSend = 1                     ' enumeration of comm events
const comEvReceive = 2
const comEvCTS = 3
const comEvDSR = 4
const comEvCD = 5
const comEvRing = 6
const comEvEOF = 7

const comInputModeText = 0              ' enumeration of input mode constants
const comInputModeBinary = 1

flag = 0                                ' keep program running until comm event
rxCnt = 0

Set objFSO = CreateObject _
("Scripting.FileSystemObject")          ' create a file system object

path = objFSO.GetAbsolutePathName ("c:")' get the path to this directory

Set objLogFile = objFSO.CreateTextFile _
(path & logfile, True)                  ' create a text file in local directory

Set objTest = WScript.CreateObject _
("MSCOMMLib.MSComm", "[/b]MSCommEvent_")    ' second parameter (MSCommEvent_) +
                                        ' name of event (OnComm) creates the
                                        ' event handler that is called when
                                        ' the event fires

objTest.CommPort = MyPort               ' select a port to use
objTest.InputLen = 0                    ' if = 0, will retrieve all waiting chars
objTest.InputMode = comInputModeText    ' causes Input() to return string (not array)
objTest.RThreshold = 2                  ' must be non-zero to enable receive
objTest.PortOpen = TRUE                 ' open COM port for use

s = MyBaud & ",n,8,1"                   ' settings: baud,parity,bits,stop in BSTR
objTest.Settings = s                    ' send to COM port

msg = "Test Message "
objTest.Output = msg                    ' send test message out of port

While flag = FALSE                      ' put script in idle until comm event
    Wscript.Sleep (1000)
Wend

objLogFile.Close                        ' close log file
objTest.PortOpen = FALSE                ' close port

Wscript.DisconnectObject objTest        ' destroy object
Wscript.DisconnectObject objFSO         ' destroy object
Wscript.DisconnectObject objLogFile     ' destroy object

Set objTest = Nothing                   ' uninitialize reference pointer
Set objFSO  = Nothing                   ' uninitialize reference pointer
Set objLogFile = Nothing                ' uninitialize reference pointer

Wscript.Echo "Script completed."

REM ---------------------------------------------------------------------------

Sub MSCommEvent_OnComm                  ' OnComm event handler
    Select Case objTest.CommEvent
        Case comEvReceive
            rxCnt = rxCnt + objTest.InBufferCount
            rxBuf = rxBuf & objTest.Input
            If rxCnt >= MyThreshold Then
                For i = 2 To MyThreshold Step 2
                    objLogFile.Write (Mid (rxBuf, i - 1, 2))
                    objLogFile.Write (",")
                Next
                flag = TRUE
                rxCnt = 0
            End If
        Case Else
    End Select
End Sub

worf83
Senior Member
Senior Member
Posts: 131
Joined: Mon Feb 14, 2005 11:40 am
Contact:

Postby worf83 » Thu Aug 28, 2008 10:40 pm

Dave,

Thank you very much for the script. I really appreciate all your help.

AL

mwebster
Intermediate
Intermediate
Posts: 271
Joined: Mon Mar 20, 2006 10:38 pm
Contact:

Postby mwebster » Sat Aug 30, 2008 1:00 am

Thanks for the example. A post above from Cinemar says:
Yes, no access to hardware via scripts.

I guess it wasn't true.

One concern I have about the example is this:
While flag = FALSE ' put script in idle until comm event
Wscript.Sleep (1000)
Wend

Will this hang mlserver for one second? I guess I'm not clear on how the script engine works. Is each script ran in its own thread independent of mlserver?

Also, is there a way to write to mlserver's event log from a script? If so, I could integrate my dsc security stuff into mlserver, which would be great.

User avatar
CinemarDave
Site Admin
Site Admin
Posts: 10535
Joined: Fri Feb 07, 2003 8:56 am
Location: Planet Earth
Contact:

Postby CinemarDave » Sat Aug 30, 2008 1:26 am

This script is not a good example of practical use. You cannot put any script into a loop like this because it will hang MLServer. You would not want to try to build a two-way plugin using script.

You can write to the MLServer event log using script.

MLServer.LogMLEvent SourceName, SourceEvent, LogLevel

Where:
SourceName = Usually the name of the script that is running
SourceEvent = The descriptive text
LogLevel = a number representing the event status
0 = Error
1 = Info
2 = Vebose
3 = Verbose Trace

mwebster
Intermediate
Intermediate
Posts: 271
Joined: Mon Mar 20, 2006 10:38 pm
Contact:

Postby mwebster » Sat Aug 30, 2008 11:43 am

You would not want to try to build a two-way plugin using script.


This is what I need to do. What methods are available for me to do this?


Return to “MLScript”

Who is online

Users browsing this forum: No registered users and 1 guest