Hey all, i am trying to turn on a A/V Reciever with a RS232 command using the VB6 comm32. To turn it on it says to use:
Command code Parameter code CR Code set example
PW ON <CR> PWON<CR>
And this is my VB6 code i am currently using that doesnt seem to work...
MSComm.CommPort = 2
MSComm.Settings = "9600,n,8,1"
MSComm.PortOpen = True
If Not MSComm.PortOpen Then
MsgBox "not opened"
Else
MSComm.Output = "PWON" & Chr(13)
Do While MSComm.InBufferCount > 0
Text1.Text = Text1.Text & MSComm.Input
Loop
End If
The reciever never turns on. What could i be doing incorrectly? I checked to make sure the com port was 2 and it is.
David
You are just sending the characters <CR> rather than a real carriage return (ASCII code 13). Documentation for serial peripherals often puts the names of control characters in brackets (see Wikipedia for a list of them). You need the line:
MSComm.Output = "PWON" & Chr(13)
It also seems that the code that follows to read data from the serial port should be changed because if the data has not arrived in the serial port's buffer yet, it will read nothing. Take a look at Microsoft's example for how to do so. You could decide to stop reading once a particular substring in the input has been found, once a certain number of bytes have been read (Len function), etc.
Related
I'm facing a random problem. When executing SAS programs with VBScript and the SASEGObjectModel.Application.7.1, looping through CodeCollection get stuck sometimes, even if the program execution was succeeded (the final data bases are correctly created in our server). The script simple doesn't go to the next program of CodeCollection (the prompt executing the script still open... ad infinitum). The SAS program It happens is random, also the frequency. I'm going with something like this:
Dim oSasApp
Set oSasApp = CreateObject("SASEGObjectModel.Application.7.1")
oSasApp.SetActiveProfile("some-profile")
Dim oSasProj
Set oSasProj = oSasApp.Open("some-project.egp", "")
Dim oProgramList
Set oProgramList = oSasProj.CodeCollection
Dim programOrder
Set programOrder = ...here I assign the SAS programs order array reading from a .txt...
For Each program in programOrder
For Each sasProgram in oProgramList
If sasProgram.Name = program Then
sasProgram.Run
sasProgram.Log.SaveAs "some-folder/" & sasProgram.Name & ".txt"
End If
Next
Next
oSasProj.Close
oSasApp.Quit
The problem is not the Log saving, as the log txt file of the stucked program is also correctly created.
Any idea? Maybe problems in our SAS server? Should I declare some kind of options?
SAS Guide version: 7.15
Windows: 10
Tks
So... for people facing the same problem. As I commented above, if I press enter on prompt the script flows again. So it is waiting for my input, for reasons I can't tell. I did 2 things to get around it. Not sure if all of them are necessary or if only one solves it, but here it goes:
First, by VBScript I turned off a list of generations and I applied a delay after the SAS program runs:
For Each program in programOrder
For Each sasProgram i oProgramList
If sasProgram.Name = program Then
sasProgram.GenSasReport = False
sasProgram.GenHTML = False
sasProgram.GenListing = False
sasProgram.GenPDF = False
sasProgram.GenRTF = False
sasProgram.Run
WScript.Sleep(2000)
sasProgram.Log.SaveAs "some-folder/" & sasProgram.Name & ".txt"
End If
Next
Next
Them, in my batch file, wich I use to call the VBScript with the "cscript" command, I set it to apply "y" to every single message the VBScript could ask:
cd ./script-folder
echo y | cscript script-file-name.vbs
And that is it.
I found this one-line example that allows to use the Windows SAPI Text-to-Speech feature in VBScript:
CreateObject("SAPI.SpVoice").Speak("This is a test")
I wonder if the SAPI Speech Recognition could be used in a VBScript program in the same easy way. When I seek for such information the tons of SAPI information that appear are related to C++, like the Microsoft SAPI site, or to Text-to-Speech in VBS. I tried to find documentation about the SAPI COM object Speech Recognition part that could be used in a VBScript, but found none.
Do you know if such a documentation exists? TIA
EDIT: Additional request added after the first answer was recevied
Although the first answer below provide a link to the SAPI COM object documentation, I want to attract your attention to a point in my question: "I wonder if the SAPI Speech Recognition could be used in a VBScript program IN THE SAME EASY WAY". The SAPI documentation is huge! I read several pages of it and I am completely lost... My goal is to recognize just a few single words, say 8 or 10, and show a different message in the screen each time that one of they was recognized; that is it! (The program should be a console application started via cscript). Is there a simple example of VBS code that achieve such thing? If the required code to program this solution needs to have several pages, then it is not the answer I am looking for...
Here is a working example of vbscript listening a wav file:
scriptRunning = true
Sub rc_Recognition(StreamNumber, StreamPosition, RecognitionType, Result)
Wscript.Echo "Reco: ", Result.PhraseInfo.GetText, ".", RecognitionType
End Sub
Sub rc_StartStream(StreamNumber, StreamPosition)
Wscript.Echo "Start: ", StreamNumber, StreamPosition
End Sub
Sub rc_EndStream(StreamNumber, StreamPosition, StreamReleased)
Wscript.Echo "End: ", StreamNumber, StreamPosition, StreamReleased
scriptRunning = false
End Sub
outwav = "C:\SOFT\projects\af2t\t.wav"
Const SAFT22kHz16BitMono = 22
Const SSFMOpenForRead = 0
set sapiFStream = CreateObject("SAPI.SpFileStream")
sapiFStream.Format.Type = SAFT16kHz16BitMono
sapiFStream.Open outwav, SSFMOpenForRead
MsgBox "A SpeechLib::ISpRecoContext object will be created"
Const SGDSActive = 1
Set rct = WScript.CreateObject("SAPI.SpInProcRecoContext", "rc_")
Set rgnz = rct.Recognizer
Set rgnz.AudioInputStream = sapiFStream
Set rcGrammar = rct.CreateGrammar
'rcGrammar.DictationLoad
rcGrammar.DictationSetState SGDSActive
i = 0
while scriptRunning and i < 100
WScript.Sleep(50)
i = i + 1
wend
MsgBox "A SpeechLib::ISpRecoContext object has been created"
The magical part of the code is this line (the "rc_" prefix param allows events to be caught by the subs):
Set rct = WScript.CreateObject("SAPI.SpInProcRecoContext", "rc_")
The recorded text in the t.wav file I used for testing has been generated with SAPI.SpVoice::Speak and MS-David voice ;-)
I spent 10 days figuring out how to write this script. Microsoft is removing documentation about automation, COM, old style scripts, etc. A shame.
So, this works in dictation mode reading a wav file. But I couldn't correct it to make it work in live dictation mode (i.e. using the microphone as direct input). Any help appreciated for this. Thanks.
EDIT: direct/live dictation mode solved. If interested I share the vbscript code.
EDIT2: text sample spoken in the wav: Hello world. This is a talk about gear tooth profile using a circle involute.
Console output from the vbscript
C:\SOFT\projects\af2t>cscript r.vbs
Microsoft (R) Windows Script Host Version 5.812
Copyright (C) Microsoft Corporation. Tous droits réservés.
Start: 1 0
Reco: Hello world . 0
Reco: this is a talk about gear to the profile using a circle invalid . 0
End: 1 195040 -1
C:\SOFT\projects\af2t>
Yes. Look at the SAPI Automation Overview; it will tell you all about the late-bound COM interfaces and objects available to VBScript.
I am trying to loop through all controls in a form:
For Each ctrl in Me.Controls
in order enable/disable the control based on some conditions.
But there is a control on the form that gives an error when I try to access it. What kind of control does that, and how do I find it?
When you get your error and click Debug, is the error on the line setting a control's Enabled property?
If so, add a Debug.Print statement writing out the control's name. Do so on the line before setting the Enabled property.
Here's what I mean:
Dim ctrl As Control
For Each ctrl In Me.Controls
Debug.Print ctrl.Name
ctrl.Enabled = True
Next
The Debug.Print statement will write out to the Immediate Window the name of the control that was last processed in the loop, presumably the one that caused your error.
EDIT
This might work. Put this control in a Panel control and set the Panel's Enabled property to False. If I recall correctly, in VB6 setting a container control's Enabled property to False will also set the container's child controls Enabled to False. If your control's Enabled property really is read-only, I'm curious what would happen.
Try this:
Dim ctr As Control
Dim CtrStatus Boolean
CtrStatus = False
For Each ctr In Me.Controls
If (SSTab.hwnd = GetParent(ctr.hwnd)) Then
Call CallByName(ctr, "Enabled", VbLet, CtrStatus)
else
ctr.Enabled = CtrStatus
End If
Next
Another approach is as follows, that also works at runtime (as opposed to just in the IDE):
private sub SetEnabled()
on error goto errHandle
Dim ctrl As Control
For Each ctrl In Me.Controls
ctrl.Enabled = True
Next
exitPoint:
exit sub
errHandle:
MsgBox "Error " & err.Description & " with Control " & ctrl.Name
resume exitPoint
end sub
Suppress the error reports before the loop and then set it back to standard error handling:
On Error Resume Next
For Each ctrl In Me.Controls
ctrl.Enabled = lIsEnabled
Next
On Error GoTo 0
OR name your controls with a standard prefix/suffix that you can check by name and skip in the loop.
For Each ctrl In Me.Controls
If Left(ctrl.Name, 3) = "sst" Then
ctrl.Enabled = lIsEnabled
End If
Next
Tosa: from your comment on AngryHacker's answer, I think you are checking the container incorrectly.
Your code is like this
' BAD CODE '
If ctrl.Container = fraMovies Then
For me that gives error 450 Wrong number of arguments or invalid property assignment. Do you get the same error?
The code should use Is rather than =, like this
' GOOD CODE '
If ctrl.Container Is fraMovies Then
Explanation. You want to check whether two variables "point" to the same control. Controls are objects: you must use Is not = to check whether two object variables "point" to the same object. This is a classic pitfall in VB6.
One last word. Next time, could you try to post 10 lines or less of actual code, reproducing the error, and give the exact error number and message and the exact line on which it occurs? It really does make it much easier for us to solve your problem - I know it's work for you to shorten the code, but you'll get better answers that way.
EDIT Welcome back! :) You said some controls don't have a Container property. You could try wrapping the test in On Error Resume Next, something like this.
' GOOD CODE '
Dim bMatch As Boolean
Dim ctrl As Control
For Each ctrl In Me.Controls
bMatch = False
On Error Resume Next
bMatch = (ctrl.Container Is fraMovies)
On Error Goto 0
If bMatch Then
ctrl.Enabled = True
End If
Next
To avoid such problems follow the given rules while naming contols
When you name an element in your Visual Basic application, the first character of that name must be an alphabetic character or an underscore.
**Begin each separate word in a name with a capital letter, as in FindLastRecord and RedrawMyForm.
Begin function and method names with a verb, as in InitNameArray or CloseDialog.
Begin class, structure, module, and property names with a noun, as in EmployeeName or CarAccessory.
Begin interface names with the prefix "I", followed by a noun or a noun phrase, like IComponent, or with an adjective describing the interface's behavior, like IPersistable. Do not use the underscore, and use abbreviations sparingly, because abbreviations can cause confusion.
Begin event handler names with a noun describing the type of event followed by the "EventHandler" suffix, as in "MouseEventHandler".
In names of event argument classes, include the "EventArgs" suffix.
If an event has a concept of "before" or "after," use a suffix in present or past tense, as in "ControlAdd" or "ControlAdded".
For long or frequently used terms, use abbreviations to keep name lengths reasonable, for example, "HTML", instead of "Hypertext Markup Language". In general, variable names greater than 32 characters are difficult to read on a monitor set to a low resolution. Also, make sure your abbreviations are consistent throughout the entire application. Randomly switching in a project between "HTML" and "Hypertext Markup Language" can lead to confusion.
Avoid using names in an inner scope that are the same as names in an outer scope. Errors can result if the wrong variable is accessed. If a conflict occurs between a variable and the keyword of the same name, you must identify the keyword by preceding it with the appropriate type library. For example, if you have a variable called Date, you can use the intrinsic Date function only by calling DateTime.Date.
I have this Windows Speech Recognition (WSRMacro) script which compounds multiple words that are spoken into a single word:
"Happy children"
-> "Happychildren"
However, a bug in the script appears under certain circumstances and I do not know how to deduce what the problem is. Although the above example works, the following does not:
"Happy children bake a cake"
Instead of compounding the words as above, the Alternates Panel appears with the following prompt:
-> Alternates Panel (Say the number next to the item you want followed by OK):
(1) Replace that withhappychildrenbakeacake
(2) replace that withhappychildrenbakeacake
(3) replace that with no space happy no space
children no space bake no space a no space cake
Can I infer any particular bug in the script below from the Alternates Panel output above?
Or is there anything I can add to the script to get more useful feedback about the nature of the bug?
<command priority="5">
<listenFor>compound that</listenFor>
<emulateRecognition>select that</emulateRecognition>
<sendKeys>{250 WAIT}{{CTRL}}c{250 WAIT}</sendKeys>
<script language="VBScript">
<![CDATA[
that = Application.clipboardData.GetData("text")
Set regEx = New RegExp
regEx.Pattern = "[^\s\w,;:]"
If regEx.Test(that) Then
Application.SetTextFeedback("Try again without any punctuation selected")
Else
regEx.Pattern = "(\s) *(\S)"
regEx.Global = True
that = regEx.Replace(" " & that, "$1no space $2")
On Error Resume Next
Application.EmulateRecognition("replace that with" & that)
If 0 <> Err.Number Then
Application.SetTextFeedback("Try again with only the digits selected")
End If
End If
]]>
</script>
</command>
Sounds like you're trying to use this in an application that doesn't natively support Text Services Framework.
More seriously, why aren't you using the builtin commands 'Remove spaces from that' or 'concatenate that'?
I am trying to use WScript.Shell SendKeys method to emulate sending a key press from the Number Pad.
I have an application that I am writing automated testing for using QTP. It is a Web Browser based application and the input is into a Java App within the web page. The input only accepts key presses from the Number Pad and the Enter key.
So far I am using this code:
Dim strInputKey
strInputKey = "{ENTER}"
Set objWsh = CreateObject("WScript.Shell")
Browser("Launch Browser").Page("Test Application").WebElement("Item ID").Click
objWsh.SendKeys strInputKey
This works fine for sending the Enter key, but I can't quite figure out if there is a way to send Number Keys. Any help would be greatly appreciated.
I am not sure if there are any undocumented ways of achieving this. I have read http://msdn.microsoft.com/en-us/library/8c6yea83(VS.85).aspx but it doesn't go into great detail.
I don't have the rep to comment on the above answer that said
objWsh.SendKeys chr(79) & chr(80) & chr(81)
but I don't think it's correct
objWsh.SendKeys chr(79) & chr(80) & chr(81)
For a start, it sends the letters O,P,Q
And he wants numbers, like 1234567890
and the link goes to keyboard scan codes.. I think those are for knowing what key on the keyboard was pressed. They are different from ascii codes.
79,80,81 are the keyboard scan codes for some numbers on the number pad / numpad.
Chr though, uses ascii codes. not keyboard scan codes.
Furthermore, just specifying a digit, here, since it isn't done by pressing a key, it doesn't specify and needn't specify, which was key was used, since a key wasn't used.
To sendkeys some numbers (from the number pad), is just same as sending keys from the top row. You just want to send some numbers.
If all he wants to know is how to use sendkeys to send digits, then obviously.
objWsh.SendKeys 12345
or
str="12345"
objWsh.SendKeys str
But if the questioner didn't realise that objWsh.SendKeys 12345 would do it, then perhaps the questioner is just confused. I guess from the green tick, he voted an answer that is like objWsh.SendKeys "OPQ".
I am aware that this is an old question, but for the sake of haing correct questions and answers..
You'll need to use the keycodes for the number pad.
Here's a list of them:
http://www.empirisoft.com/directrt/help/_helpcontents.htm?directrt_key_codes.htm
So to send "123", you would need to do:
objWsh.SendKeys chr(79) & chr(80) & chr(81)