Home > Software engineering >  Why my program VB runtime prompts "port has opened" but after I turned off the port, have
Why my program VB runtime prompts "port has opened" but after I turned off the port, have

Time:10-17


VB:
Left my program is normal at the beginning, but the printer here would say when I "port has opened the" (I checked your code and before the computer did not open any port)


Then I added, in front of the port open command the following command:
If MSComm1. PortOpen=False Then
MSComm1. PortOpen=True
End the If

Hopes to avoid the "port has opened the" problem, but it will have an error and said I port is not open,

Do not know to have warrior can help me solve the problem!

CodePudding user response:

Your program is repeat open the serial port?
You had better be to step through the program, see if the problem where the
In general, if MSComm1 PortOpen=true, you perform again: MSComm1. PortOpen=true, it will press has opened the mouth,

CodePudding user response:

The great spirit:
You said I also understand this truth, I also step through lead, also did not find the problem,
And I think the following three lines of code have to block out the problem, but still won't do?
If MSComm1. PortOpen=False Then
MSComm1. PortOpen=True
End the If

CodePudding user response:

If you are an abnormal shutdown, a serial port not closing?
I suggest you post
all the codeAnd restart the computer to see, will not have this problem,

CodePudding user response:

Not don't want to stick the code, but too much code, and related hardware also many, for almost 8 COMM port work at the same time, then only the COMM port has a problem, I can only stick part:

Private D As Port
Private LF the As String
Private ESC As String
Private FS As String
Private GS As String
Private frmSerialPort As SerialP

Private Sub Command2_Click ()
"'" 'choose series parallel
' ' ' ' 'set the port attribute

If Combo1. Text="COM1" Then Com=1



The Debug. Print "com=" & amp; Com
MPort=Com MSComm1.Com
Bps=Combo2. Text
Data=https://bbs.csdn.net/topics/Combo3.Text
Check=Combo4. Text
SStop=Combo5. Text

MSComm1. Settings=Bps & amp; Check & amp; The Data & amp; SStop '9600 baud, parity, 8 bits of data, a stop bit,
MSComm1. OutBufferSize=1024 '32 byte is the Limit.
MSComm1. InBufferSize=512

MSComm1. RThreshold=1
MSComm1. SThreshold=1
MSComm1. InputLen=1
MSComm1. Handshaking=sControl 'comNone Set Handshaking

MSComm1. DTREnable=True
MSComm1. RTSEnable=True
MSComm1. PortOpen=True
The Set D=frmSerialPort
' ' ' ' 'object attribute set
Command5, Enabled=True
Command2. Enabled=False
Command3. Enabled=True
Command4. Enabled=True
Check1. Enabled=True


End Sub

Private Sub Command3_Click ()
' ' ' ' 'disconnect port
'empty output caching
MSComm1. OutBufferCount=0

'empty input buffer
MSComm1. InBufferCount=0

'port is set to stop
MSComm1. PortOpen=False

'attribute is set to restore to the original state
Command5, Enabled=False
Command3. Enabled=False
Command4. Enabled=True
Command2. Enabled=True
Check1. Value=https://bbs.csdn.net/topics/0
Check1. Enabled=False
Timer1. Enabled=False
' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '

End Sub

Private Sub Command4_Click ()
'exit interface
End
End Sub

Private Sub Command5_Click ()
'* * * * * * * * * * *
'"' test print ASCII characters
Dim the Timer As String


'a=PCSS_USER_Charge_3. BatteryNum * 5
The Timer=Format (Now (), "yyyy - MM - dd hh: MM: ss")


D.O utput (thanks to the use of the postal car coffer filling system ")


D.O utput (LF)
D.O utput (LF)
D.O utput (" card number: ")
D.O utput (PCSS_Authentication. RFmessage. Caption)
D.O utput (LF) 'line breaks
D.O utput (LF)
D.O utput (" button before balance: ")
D.O utput (BeforeMoney)
'the D.O utput (PCSS_Authentication. PrinterCarBalance. Caption)
D.O utput (" RMB ")
D.O utput (LF)
D.O utput (LF)
D.O utput (" replace the battery number: ")

D.O utput (PCSS_USER_Charge_3. ChangeSumText. Caption)
D.O utput (" block ")
D.O utput (LF)
D.O utput (LF)

D.O utput (" amount of use: ")
D.O utput (STR (PCSS_USER_Charge_3. ChangeSumText. Caption * 5))
D.O utput (" RMB ")
D.O utput (LF)
D.O utput (LF)

D.O utput (" button after balance: ")
D.O utput (PCSS_USER_Charge_4. PrinterCardMoneyToWrite. Caption)
D.O utput (" RMB ")
D.O utput (LF)
D.O utput (LF)

D.O utput (" time: ")
D.O utput (Timer)
D.O utput (LF)
D.O utput (LF)
D.O utput (LF)

'send GS V m paper cutting command
MSComm1. The Output=$(29) + CRH CRH + CRH $$(86) (66) + CRH $(0)

End Sub

Private Sub Form_Load ()
LF=CRH $(10)
ESC=CRH $(27)
FS=CRH $(28)
GS=CRH $(29)


Combo4. AddItem "N"
Combo4. AddItem "O"
Combo4. AddItem "E"
Combo4. Text="N"

'* * * * * * * * * * * * * * * * * * * * * * * * * *
'class attribute set
The Set frmParallelPort=New ParallelP
The Set frmSerialPort=New SerialP

'* * * * * * * * * * * * * * * * * * * * * * * * * *
"' timing Settings
Text1. Text="1000"

End Sub

CodePudding user response:

In the form of the Unload event to add:

If MScomm1. PortOpen then MScomm1. PortOpen=False

CodePudding user response:

First see a serial number if you want to open the bottom slogan
Closed the second program is not to be sure to close a serial port because when open the serial port of the thread to destroy system will automatically shut down
Third inspect whether there is a third party programs are using a serial port

Make a serial port monitoring software can see out

CodePudding user response:

I also met you such a question agghh ahhh
  • Related