In VB.net, I am sending SCPI commands via GPIB-to-USB to an agilent/keysight device. I am using this sample to write data to instruments. https://www.keysight.com/main/editorial.jspx?cc=BR&lc=por&ckey=492255&nid=-33170.1090751.08&id=492255
Here is a portion of my code that I am having trouble with.
Dim P As Integer
Dim Power As Integer
For P = -50 To 0 Step 10
System.Threading.Thread.Sleep(500)
Power = P
instrument.WriteString("POW" & Str$(P) & "dbm")
Console.WriteLine("Power" & P) 'Testing if this portion of the for loop is reached
My code builds successfully, but the device I am communicating with displays an error message saying "invalid separator." I believe the problem is that P starts as a negative number. If I were to do something like, For P=0 to 10 step 1, it would work on the device receiving the command. However, Str$ does not seem to be work with negative numbers. Using Console.WriteLine, I do see that my loop is working correctly in the console output. Based on the console output, the structure of the loop is not the problem.
How could I make this work with P starting as a negative number? I tested the code and device to see if it can take negative numbers by simply using instrument.WriteString("POW -5 dbm") and that works. I really believe the problem involves Str$. I also tried doing ("POW" & P.ToString() & "dbm") but that did not work either. I also replaced Str$ with CStr but that did not work either. Also note that the sleep/delay is not an issue because I implemented that in another for loop and it worked with no errors.
I just figured it out. There has to be a space after POW. It should look like "POW "
Related
I have a program that gives different outputs depending on if I debug with F10 or simply run the program. Even with F5 from one point to another returns the "weird" output. But if I debug it step by step it gives the correct output.
The project contains some commands that I send to a function generator and, depending on what returns, it shows one picture or another.
I thought that the problem could be that the function generator would need more time to send and receive the data, but I made it 10x longer and the problem doesn't change.
What can cause this problem? Maybe something in the cache or it's definitely my code's problem?
The code where the problem happens:
result = ""
Do Until result <> ""
SendToMachine("SOURCE2:APPL" & vbLf, result)
If result = "" Then GoTo finish
Select Case result
Case ....
End Select
finish:
Loop
Normally the first loop, result gives nothing because it always fails, but the second time if i do it with F10 it works perfectly. It returns the value that is supposed to return and after that the code works perfectly.
When i dont use F10 it shows some rare values.
Maybe the problem is using Goto?.
So.
I am making a bruteforcer in visual basic.
It has a charset as shown below:
Dim charset as string
charset = "abcdefghijklmnopqrstuvwxyz1234567890."
There is 37 different chars right? The program is made to search for all the different combinations made with this charset, with a maximum of 3 different letters. For example
This is a combination that can be made: ac6
So since there is 37 letters and 3 slots the possible combinations are 37^3
But I wanted my program not to try the same combination twice.
So it saves every single combination tried in this location (Desktop)
Dim filex As System.IO.StreamWriter
filex = My.Computer.FileSystem.OpenTextFileWriter("c:\Users\" + Environment.UserName + "\desktop\alreadytested.txt", True)
filex.WriteLine(combination)
filex.Close()
And, at the start of the Sub that checks for new combinations, I have this
text = File.ReadAllText("c:\Users\" + Environment.UserName + "\desktop\alreadytested.txt")
index = text.IndexOf(combination) 'checks if it has been generated already
If index >= 0 Then
keyword() 'The sub
End If
But after some combinations (in this case the max 37^3 ~= 50.000 and I the program tried around 5200 times) I get this error
An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll
And this error points in this line of code
filex = My.Computer.FileSystem.OpenTextFileWriter("c:\Users\" + Environment.UserName + "\desktop\alreadytested.txt", True)
But why? at 5200 there is still 44800 possible random combinations, why do I get an overflow?
It would make sense if I got it when I had 50000 combinations out of 50000 possible tested, but now I have 10% only, so why do I get an overflow?
You keep on recursively calling the keyword() method. Every time you call a method its return address and possibly its arguments is/are added to the call stack. The callstack can only take a certain amount of calls before it overflows, and for your computer and that specific method of yours, that amount seems to be about 5200.
You should switch to using for example a While-loop instead, and whenever you want to block the rest of the execution and go back to the beginning of the loop you'd just call Continue While.
A little side note is also that you shouldn't open and close the file every time you read/write to it. Store the contents of the file in a long string (or even better, in a HashSet(Of T)) instead and check that every time you need to, then at the end of the loop you may write all the contents to a file.
If you still wish to write to the file during the process then do so. But instead open a stream before your loop which you keep writing to until the loop is finished, then close the stream.
We have an Excel-file with a large amount of VBA behind it. The Excel-file works just fine on my computer, but so far 3 of my colleagues (non-IT'ers) have gotten this error:
Runtime error 5:
Invalid procedure call or argument
The error is located on this line, and I don't see why it is throwing an error there because it's just a simple Set (and it works perfectly fine on my computer):
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
We all have the exact same Excel-file. I even sent them my version of the file, in which everything is running fine and no errors happen, but even when they open my version of the file they still get the above error on the above line!
What exactly could cause this? We all have Office 2013 and updates are installed automatically. The problem started about 2 weeks ago with one colleague and since this week I heard from two other colleagues that they have the same problem. One even said it suddenly worked again after he moved to a different desk (which I doubt would have an influence) but shortly after, it started getting the error again.
I have absolutely no idea why they get the error, or what might cause it. Seeing as we now all have the same version and they still get the error, I am thinking it might have something to do with Excel itself but that's just my idea.
Does this sound familiar to anyone here? Or does anyone know what might cause this, and how it can be fixed?
Edit: a while ago I checked on my colleagues their computers to see if the CommandBar was present, and it was. Even then the error still happened.
I suggest you to use a function that checks if there is a CommandBar in your Application like this:
Function IsCommandBarValid(cbName As String) As Boolean
Dim i As Long
IsCommandBarValid = True
For i = 1 To Application.CommandBars.Count
If (Application.CommandBars(i).Name = cbName) Then
Exit Function
End If
Next i
IsCommandBarValid = False
End Function
Now, You can use it to see that your user have that CommandBar in his or her Application, then make it like this:
If (Not IsCommandBarValid("Attributions")) Then
Call Application.CommandBars.Add(Name:="Attributions")
End If
' And after this add your code
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
Invalid procedure call or argument (Error 5)
Some part of the call can't be completed. This error has the following causes and solutions:
An argument probably exceeds the range of permitted values. For example, the Sin function can only accept values within a certain range. Positive arguments less than 2,147,483,648 are accepted, while 2,147,483,648 generates this error.
Check the ranges permitted for arguments.
This error can also occur if an attempt is made to call a procedure that isn't valid on the current platform. For example, some procedures may only be valid for Microsoft Windows, or for the Macintosh, and so on.
Check platform-specific information about the procedure.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
MSDN Source Article
I'm having some kind of problem in a piece of code.
I need to find values in a excel book and then copy it to another. The thing is that i need to copy it concept by concept in the right place. The problem is that the file doesnt come every week in the same order.
So i need to find the concept and then copy the next cell that is the value of that concept.
First of all i use need to locate the correct line so i can start copying (this part is easy and its done).
Secondly, having the correct line, i need to find the concept, which in this example im going to use the "619".
After having find the location of this value i store the value in "c_audiovisual".
On Error GoTo ErrhandlerCAV
lRowC_AV = Application.WorksheetFunction.Match(619, Range("A" & line & ":FI" & line), 0) + 1
'On Error GoTo ErrhandlerCAV
Continue:
If errorCAV = 1 Then
c_audiovisual = 0
errorCAV = 0
Else
c_audiovisual = ActiveSheet.Cells(line+ m2, lRowC_AV).Value
End If
I've made a escape in case that this concept isn't present in that line (which sometimes occurs).
Sometimes this piece of code works, and other it doesn't.
When i'm on debug mode (pressing F8) it works. And when i use small files to look for the values it works. On bigger files sometimes don't.
Any ideas?
I faced similar issues. I fixed it with using ThisWorkbook.Sheets("MySheet").Range(...) inside the Match function. Or try ActiveWorkbook as applicable. Let me know if this worked. Curious to know.
Btw I noticed column and row number both are "line". Is that a mistake?
I imported the Winsock feature in my vb.net application, so I can make a Chat System. I just have one little problem with my program. In the GetData method of my program,
CLIENT SIDE:
*Dim strData As String*
AxWinsock1.GetData(strData, vbString)
TextBox1.Text = TextBox1.Text & _
strData & vbCrLf
It will underline the whole first line, unless have an maxLen as Object in. So I plugged in Nothing, since I thought it was optional. Now when I debug, and I send a message from the server, it won't display anything. I put in vbByte as the maxLen object, and now it only shows part of the message. Can anyone tell me how to fix this. This works in VB6...
PS: I am not going to use the System.Namespaces function of VB.NET since, I find the Winsock feature much easier.
Thanks
vbByte is a constant with a value of 17, so you actually send a max length of 17 ;-)
You will want to send a bigger number as the max length. If what you want to send as a max length is the upper bound of the Byte data type you could send Byte.MaxValue (which is 255).
Edit:
I don't know what is the upper bound, but you can try Integer.MaxValue, or some arbitrary big value like 1000000...
Looking at the documentation, I noticed two things:
maxLen should be optional, I don't know why it would flag an error when you don't specify it, what kind of error is it?
It's mentioned that it's common to use GetData with a DataArrival event, is that your case? If it is, you could pass the totalBytes argument as the maxLen.