'------------------COM ---------- Dim COM_NO As Byte Dim DELAY As Long Dim TXCNT As Long Dim RXCNT As Long Dim TOTALTX As Long '----------------------------- Private Sub Form_Load() On Error GoTo err '-------------CHECK THE COM------------- Dim i As Integer For i = 1 To 16 MSComm1.CommPort = i On Error Resume Next MSComm1.PortOpen = True If err.Number = 0 Then COM_NO = i Else MSComm1.PortOpen = False End If Next i '----------------------------------------- MSComm1.CommPort = COM_NO If MSComm1.PortOpen = True Then MSComm1.PortOpen = False If MSComm1.PortOpen = False Then MSComm1.Settings = "9600,n,8,1" MSComm1.RThreshold = 1 '******核心语句,去掉则无法触发 'MSComm1.SThreshold = 1 '发送缓冲区中允许的最小字符数触发 MSComm1.OutBufferSize = 10 MSComm1.InputLen = 0 '***********CHANGEED MSComm1.InputMode = comInputModeBinary '1-- 'MSComm1.InputMode = comInputModeText MSComm1.PortOpen = True '--------------------------------- End If If MSComm1.PortOpen = False Then MsgBox "串口打开失败,请检查": End Exit Sub err: MsgBox "串口错误" End Sub Private Sub MSComm1_OnComm() Dim Instring As Byte Dim ins As String Dim RE_BUF As Double On Error GoTo err Select Case MSComm1.CommEvent Case comEvReceive Dim InByte,i As Integer InByte = MSComm1.Input For i = 0 To UBound(InByte) Instring = (InByte(i)) Next End Select If Instring = 255 And FUNC = "CHOSE" Then MAIN_PWM = True: MsgBox "PWM设置成功": Exit Sub If Instring < 5 And FUNC = "CHOSE" Then If Instring = 1 Then CH1 = True: Form5.Text1.Text = Form5.Text1.Text + "通道1 OK" If Instring = 2 Then CH2 = True: Form5.Text1.Text = Form5.Text1.Text + "通道2 OK" If Instring = 3 Then CH3 = True: Form5.Text1.Text = Form5.Text1.Text + "通道3 OK" If Instring = 4 Then CH4 = True: Form5.Text1.Text = Form5.Text1.Text + "通道4 OK" Exit Sub Else End If Instring = Hex(Instring) If Instring > 10 And Instring < 18 Then If Instring = 11 Then CH1_STEP = 1 If Instring = 12 Then CH1_STEP = 3 If Instring = 13 Then CH1_STEP = 2 If Instring = 14 Then CH1_STEP = 4 If Instring = 15 Then CH1_STEP = 6 If Instring = 16 Then CH1_STEP = 5 If Instring = 17 Then CH1_STEP = 7 If CH1_STEP > 0 Then Form8.Show If CH1_STEP > 0 Then Form8.Command1.Value = True Exit Sub Else End If If Instring > 20 And Instring < 28 Then If Instring = 21 Then CH3_STEP = 1 If Instring = 22 Then CH3_STEP = 3 If Instring = 23 Then CH3_STEP = 2 If Instring = 24 Then CH3_STEP = 4 If Instring = 25 Then CH3_STEP = 6 If Instring = 26 Then CH3_STEP = 5 If Instring = 27 Then CH3_STEP = 7 If CH3_STEP > 0 Then Form10.Show If CH3_STEP > 0 Then Form10.Command1.Value = True Exit Sub Else End If If Instring > 30 And Instring < 38 Then If Instring = 31 Then CH2_STEP = 1 If Instring = 32 Then CH2_STEP = 3 If Instring = 33 Then CH2_STEP = 2 If Instring = 34 Then CH2_STEP = 4 If Instring = 35 Then CH2_STEP = 6 If Instring = 36 Then CH2_STEP = 5 If Instring = 37 Then CH2_STEP = 7 If CH2_STEP > 0 Then Form9.Show If CH2_STEP > 0 Then Form9.Command1.Value = True Exit Sub Else End If If Instring > 40 And Instring < 48 Then If Instring = 41 Then CH4_STEP = 1 If Instring = 42 Then CH4_STEP = 3 If Instring = 43 Then CH4_STEP = 2 If Instring = 44 Then CH4_STEP = 4 If Instring = 45 Then CH4_STEP = 6 If Instring = 46 Then CH4_STEP = 5 If Instring = 47 Then CH4_STEP = 7 If CH4_STEP > 0 Then Form11.Show If CH4_STEP > 0 Then Form11.Command1.Value = True Else End If Exit Sub err: MSComm1.InBufferCount = 0 Call cominit End Sub '发送函数做的是全局函数,调用的form2的com控件 Public Function RS232_SEND(SEND_COUNT As Integer) If Form2.MSComm1.PortOpen = False Then Form2.MSComm1.PortOpen = True Form2.MSComm1.OutBufferCount = 0 'Debug.Print "start" Dim i For i = 1 To SEND_COUNT Dim cc(0) As Byte cc(0) = RS232_SEND_BUFFER(i - 1) Form2.MSComm1.Output = cc Debug.Print RS232_SEND_BUFFER(i - 1) Next i 'Debug.Print "end" End Function
串口会出现假死机的现象初步怀疑是软体原因我在接收程序中作了修改一旦开始接收就把MSComm1.RThreshold = 0 暂时停止接收触发
如果接收出错则清空接受区,重新初始化串口
接收缓冲区设置为40,inputleng设置为1
看效果如何
如果还不行就考虑是下位机pic单片机的问题