Home > Net >  The couple first post a little doubt about serial communication
The couple first post a little doubt about serial communication

Time:09-20

In the early to your
The couple first post ~ ~ ~ hope can hope to get back the gods

Beginners do a small program, involves a serial port equipment simple communication
Market is mainly adopt value, used several commonly used serial port tool can and normal communication (see photo),
Found the 1.0 com wizard source on the software don't know why we won't be able to accept a data
Does anyone know why? Is the cause of the code
Or have not Shared a god of National People's Congress can use the serial port of the source code?
Communication failure screenshots
,

Encapsulation is good serial port tool can normal communication


CodePudding user response:

Code is not complete, don't know how you write, using the serialport controls

CodePudding user response:

Look at your code is read from disk file into a byte, and then use a serial port to send (accept, too, the serial port to send byte saved to a file). But your test screenshot is just send some characters; On a serial port communication, there is a good example, in the VS see can find help file,
It is ok to do a simple thread,
[
 
'to open a serial port connection
Private Sub Button_Open_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles Button_Open. Click
SerialPort1. The Open () 'Open the serial port
Dim SerialComm=New Threading. Thread (New Threading. ThreadStart (AddressOf Sp_Receiving))
SerialComm. Start () 'open thread
End Sub


'receiving data
Private Sub Sp_Receiving ()
Dim strIncoming As String
If SerialPort1. BytesToRead & gt; 0 Then
StrIncoming=SerialPort1. ReadExisting. ToString 'data in a read buffer
SerialPort1. DiscardInBuffer ()
ListBox1. Items. The Add (strIncoming)
End the If
End Sub

CodePudding user response:

Imports System
Imports System. IO

Public Class frmMain
Public HexSendFlag As Boolean=False
Public HexRecieveFlag As Boolean=False
Public AutoClearFlag As Boolean=False

The Delegate Sub RecieveRefreshMethodDelegate (ByVal [text] As String) 'statement entrust
Dim RecieveRefresh As New RecieveRefreshMethodDelegate (AddressOf RecieveRefreshMethod) 'definition of a delegate instance

Sub RecieveRefreshMethod (ByVal STR As String) 'define an instance method
ShowRecieveData (STR)
End Sub

Private Sub ShowRecieveData (ByVal STR As String)
On Error GoTo Err

TextBoxRecieve. Text +=STR

The Exit Sub
Err: MsgBox (" data to receive or display an error!" + + ErrorToString vbNewLine ())
End Sub

Private Sub SerialPortInit ()
'a serial port Settings GUI initialization
Dim As Integer I
ComboBoxPortName. BeginUpdate ()
For I=1 To 50
ComboBoxPortName. Items. The Add (" COM "+ I.T oString)
Next
ComboBoxPortName. EndUpdate ()

ComboBoxPortName. SelectedIndex=0
ComboBoxBaudRate. SelectedIndex=11
ComboBoxParity. SelectedIndex=0
ComboBoxDataBits. SelectedIndex=0
ComboBoxStopBits. SelectedIndex=0

SerialPort. Encoding=System. Text. Encoding. The Default

End Sub

Private Sub frmMain_Load (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles MyBase. Load
SerialPortInit ()
ButtonOpenClose_Click (Nothing, Nothing)
BarWorkStatus. Text="software in place,"
BarWorkStatus. Visible=True
End Sub

Private Sub frmMain_Closing (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles MyBase. Closing
If SerialPort. IsOpen Then SerialPort. Close ()
End Sub

Private Sub ComboBoxPortName_SelectedIndexChanged (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles ComboBoxPortName. SelectedIndexChanged
If SerialPort. IsOpen=True Then
ButtonOpenClose_Click (Nothing, Nothing)
If SerialPort. IsOpen=False Then
SerialPort. PortName=ComboBoxPortName. Text
ButtonOpenClose_Click (Nothing, Nothing)
End the If
The Else
SerialPort. PortName=ComboBoxPortName. Text
End the If
End Sub

Private Sub ComboBoxBaudRate_SelectedIndexChanged (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles ComboBoxBaudRate. SelectedIndexChanged
SerialPort. BaudRate=Val (ComboBoxBaudRate. Text)
End Sub

Private Sub ComboBoxParity_SelectedIndexChanged (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles ComboBoxParity. SelectedIndexChanged
SerialPort. Parity=ComboBoxParity. SelectedIndex
End Sub

Private Sub ComboBoxDataBits_SelectedIndexChanged (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles ComboBoxDataBits. SelectedIndexChanged
SerialPort. DataBits=Val (ComboBoxDataBits. Text)
End Sub

Private Sub ComboBoxStopbits_SelectedIndexChanged (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles ComboBoxStopBits. SelectedIndexChanged
SerialPort. StopBits=ComboBoxStopBits. SelectedIndex + 1
End Sub

Private Sub ButtonOpenClose_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles ButtonOpenClose. Click
On Error GoTo Err
If ButtonOpenClose. Text="open the serial port (& amp; O) "Then
If SerialPort. IsOpen=True Then SerialPort. Close ()
SerialPort. The Open ()
ButtonOpenClose. Text="close a serial port (& amp; O)
"LabelCOMStatus. Text="serial port has opened the"
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related