Home > Software engineering >  Send Vb API function error
Send Vb API function error

Time:10-27

I made a communication program using API function, sending function as follows, If you remove the If Len (COMString) & gt; 255 Then... Between End if a few words, can appear error, and bRead (LenVal)=Asc (Mid $(COMString LenVal + 1, 1)) in the Mid $instead of Mid also can appear error, which expert to explain, note: send data every time can't more than 20 characters
 Function WriteCommString (ByVal COMString As String) As Boolean 
On Error GoTo handelwritelpt
Dim LenVal, SendBytes As Long
Dim retval As Long
Dim bRead (255) As Byte
If Len (COMString) & gt; Then 255
Retval=WriteCommString (Left $(COMString, 255))
Retval=WriteCommString (Right $(COMString, Len (COMString) - 255))
The Exit Function
End the If
For LenVal=0 To Len (COMString) - 1
BRead (LenVal)=Asc (Mid $(COMString LenVal + 1, 1))
Next LenVal
Retval=WriteFile (CommHand, bRead (0), Len (COMString), SendBytes, 0)
If retval=0 Then
Retval=GetLastError ()
MsgBox "a serial port to send data error:" & amp; Retval
CloseComm
WriteCommString=False
The Exit Function
End the If
WriteCommString=true
The Exit Function
Handelwritelpt:
WriteCommString=False
CloseComm
The Exit Function
End the Function

CodePudding user response:

If you really "send data every time can't more than 20 characters," that you said the two the cause of the error, I'm afraid I can't explain!
You said that If block, is restricted to send every time the length of the string (because it is with a fixed size buffer),
If you send the string so little, and that If statement has no effect,

Mid $() to the Mid () is also "make mistakes", also can't explain,
In Mid $() nature than in Mid () well,
Even if change to Mid (), in Asc () function, Mid () will also be the result of the automatic type conversion, it is lawful to parameters, can cause an error!

CodePudding user response:

If there are any errors, the building Lord: why don't you said what is the specific error message?
Also, if wrong, you can also enter debug state, have a look at how it is "wrong"!

CodePudding user response:

There is a to be sure, you this WriteCommString () function, only suitable for sending ASCII standard string,
Chinese characters (or other non ASCII characters), bRead (LenVal)=Asc (... ) go wrong there,
  • Related