06-02-2019 07:49 AM - edited 06-02-2019 07:52 AM
The same program works normally in win7, but often crashes in win10. At the beginning of the computer boot, the probability of crash is very high, and no error is reported. Then open the program several times before it works properly. It's not always wrong, just occasionally.
Programs generally crash after the "TextBox 1. Text" display device ID. It's strange that if "TextBox 1. Text" already shows the ID, the function is running out. But why did it crash?
The DEBUG mode of VS2017 is used. It won't crash.
VISA.dll is installed in Agilent IOlibrary. I use VB.NET, VS2017.
The code:
Declare Function viOpenDefaultRM Lib "VISA32.DLL" Alias "#141" (sesn As Long) As Long
Declare Function viGetDefaultRM Lib "VISA32.DLL" Alias "#128" (sesn As Long) As Long
Declare Function viFindRsrc Lib "VISA32.DLL" Alias "#129" (ByVal sesn As Long, ByVal expr As String, vi As Long, retCount As Long, ByVal desc As String) As Long
Declare Function viFindNext Lib "VISA32.DLL" Alias "#130" (ByVal vi As Long, ByVal desc As String) As Long
Declare Function viOpen Lib "VISA32.DLL" Alias "#131" (ByVal sesn As Long, ByVal viDesc As String, ByVal mode As Long, ByVal timeout As Long, vi As Long) As Long
Declare Function viClose Lib "VISA32.DLL" Alias "#132" (ByVal vi As Long) As Long
Declare Function viVSPrintf Lib "VISA32.DLL" Alias "#205" (ByVal vi As Long, ByVal Buffer As String, ByVal writeFmt As String, params As Long) As Long
Declare Function viVScanf Lib "VISA32.DLL" Alias "#272" (ByVal vi As Long, ByVal readFmt As String, params As String) As Long
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim status As Long
Dim defrm As Long
Dim vi As Long
Dim strRes As String
Dim strRes1 As String
Dim strRes2 As String
Dim actual As Long
status = viOpenDefaultRM(defrm)
status = viOpen(defrm, "GPIB0::7::INSTR", 0, 0, vi)
status = viVPrintf(vi, "*IDN?" + vbCrLf, 0)
System.Threading.Thread.Sleep(10)'Increasing delay time reduces the probability of crash
status = viVScanf(vi, "%t", strRes)
TextBox1.Text = strRes
End Sub
Solved! Go to Solution.
06-03-2019 09:45 PM
06-04-2019 01:57 AM - edited 06-04-2019 01:59 AM
Solved. Change "string" to "String Builder"!!!!!
Declare Function viVScanf Lib "VISA32.DLL" (ByVal vi As Integer, ByVal readFmt As String, ByRef params As StringBuilder) As Integer
but why?
06-04-2019 03:28 AM - edited 06-04-2019 03:30 AM
Correction: The initial value of params should be set very large.
For example:
Public strRes As String = “000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”
Then use "viVScanf" to work properly.
status = viVScanf(vi, "%t", strRes)
Because no matter "string" or "string builder" for the computer's memory allocation space is limited. When the data uploaded by VI exceeds the limit, it may overwrite the wrong memory space and cause EXE to crash.