Brandon,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.
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
I guess it wasn't true.Yes, no access to hardware via scripts.
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?While flag = FALSE ' put script in idle until comm event
Users browsing this forum: No registered users and 1 guest