初來貴地
新人第一帖~~~望能得到眾神回復為盼
初學者 做了一個小程式,涉及到一個串口設備的簡單通信
主要就是采值,用了幾個市面常用的串口工具都能和設備正常通信(參照圖片),
找到了其中的串口精靈1.0的原始碼 放到軟體里 不知道為什么就接受不到資料了
有人知道為什么嗎?是代碼的原因嗎
或者有沒人大神共享一份能用的串口原始碼啊?
通信失敗的截圖
、使用封裝好的串口工具都能正常通信



uj5u.com熱心網友回復:
代碼不完整,不知道你怎么寫的。用的是serialport控制元件么uj5u.com熱心網友回復:
看你的代碼是讀取磁盤檔案轉為byte,然后用串口發送(接受也是,把串口發送的byte保存到檔案);但你的測驗截圖又是只是一些字符發送; 關于串口通訊,VS里就有很好的例子啊。看幫助檔案就能找到。做一個簡單的執行緒就可以了。
[
'打開串口連接
Private Sub Button_Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Open.Click
SerialPort1.Open() '打開串口
Dim SerialComm = New Threading.Thread(New Threading.ThreadStart(AddressOf Sp_Receiving))
SerialComm.Start() '開執行緒
End Sub
'接收資料
Private Sub Sp_Receiving()
Dim strIncoming As String
If SerialPort1.BytesToRead > 0 Then
strIncoming = SerialPort1.ReadExisting.ToString '讀取緩沖區中的資料
SerialPort1.DiscardInBuffer()
ListBox1.Items.Add(strIncoming)
End If
End Sub
uj5u.com熱心網友回復:
Imports SystemImports System.IO
Public Class frmMain
Public HexSendFlag As Boolean = False
Public HexRecieveFlag As Boolean = False
Public AutoClearFlag As Boolean = False
Delegate Sub RecieveRefreshMethodDelegate(ByVal [text] As String) '宣告委托
Dim RecieveRefresh As New RecieveRefreshMethodDelegate(AddressOf RecieveRefreshMethod) '定義一個委托實體
Sub RecieveRefreshMethod(ByVal str As String) '定義一個實體方法
ShowRecieveData(str)
End Sub
Private Sub ShowRecieveData(ByVal str As String)
On Error GoTo Err
TextBoxRecieve.Text += str
Exit Sub
Err: MsgBox("資料接識訓顯示錯誤!" + vbNewLine + ErrorToString())
End Sub
Private Sub SerialPortInit()
'串口設定GUI初始化
Dim I As Integer
ComboBoxPortName.BeginUpdate()
For I = 1 To 50
ComboBoxPortName.Items.Add("COM" + I.ToString)
Next
ComboBoxPortName.EndUpdate()
ComboBoxPortName.SelectedIndex = 0
ComboBoxBaudRate.SelectedIndex = 11
ComboBoxParity.SelectedIndex = 0
ComboBoxDataBits.SelectedIndex = 0
ComboBoxStopBits.SelectedIndex = 0
SerialPort.Encoding = System.Text.Encoding.Default
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPortInit()
ButtonOpenClose_Click(Nothing, Nothing)
BarWorkStatus.Text = "軟體就緒。"
BarWorkStatus.Visible = True
End Sub
Private Sub frmMain_Closing(ByVal sender As System.Object, ByVal e 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 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 If
Else
SerialPort.PortName = ComboBoxPortName.Text
End If
End Sub
Private Sub ComboBoxBaudRate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxBaudRate.SelectedIndexChanged
SerialPort.BaudRate = Val(ComboBoxBaudRate.Text)
End Sub
Private Sub ComboBoxParity_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxParity.SelectedIndexChanged
SerialPort.Parity = ComboBoxParity.SelectedIndex
End Sub
Private Sub ComboBoxDataBits_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxDataBits.SelectedIndexChanged
SerialPort.DataBits = Val(ComboBoxDataBits.Text)
End Sub
Private Sub ComboBoxStopbits_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBoxStopBits.SelectedIndexChanged
SerialPort.StopBits = ComboBoxStopBits.SelectedIndex + 1
End Sub
Private Sub ButtonOpenClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpenClose.Click
On Error GoTo Err
If ButtonOpenClose.Text = "打開串口(&O)" Then
If SerialPort.IsOpen = True Then SerialPort.Close()
SerialPort.Open()
ButtonOpenClose.Text = "關閉串口(&O)"
LabelCOMStatus.Text = "串口已打開"
LabelCOMStatus.ForeColor = Color.Green
Else
If CheckBoxAutoSend.Checked = True Then
MsgBox("請先關閉自動發送!")
Exit Sub
End If
If SerialPort.IsOpen = True Then SerialPort.Close()
ButtonOpenClose.Text = "打開串口(&O)"
LabelCOMStatus.Text = "串口已關閉"
LabelCOMStatus.ForeColor = Color.Red
End If
Exit Sub
Err: MsgBox("串口不存在或已被占用!" + vbNewLine + ErrorToString())
End Sub
Private Sub CheckBoxHexRecieve_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxHexRecieve.CheckedChanged
HexRecieveFlag = CheckBoxHexRecieve.Checked
End Sub
Private Sub CheckBoxHexSend_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxHexSend.CheckedChanged
HexSendFlag = CheckBoxHexSend.Checked
End Sub
Private Sub ButtonSendData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSendData.Click
On Error GoTo Err
Dim outDataBuf As String = TextBoxSend.Text
If outDataBuf = "" Then Exit Sub
If SerialPort.IsOpen = True Then
If HexSendFlag = True Then
'-----------十六進制發送------------
outDataBuf = outDataBuf.Replace(" ", "") '清除空格與回車
outDataBuf = outDataBuf.Replace(vbNewLine, "")
If outDataBuf.Length Mod 2 <> 0 Then
MsgBox("請輸入正確的十六進制數,用空格和回車隔開。")
Exit Sub
End If
Dim outBytes(outDataBuf.Length / 2 - 1) As Byte
For I As Integer = 1 To outDataBuf.Length - 1 Step 2
outBytes((I - 1) / 2) = Val("&H" + Mid(outDataBuf, I, 2))
Next
SerialPort.Write(outBytes, 0, outDataBuf.Length / 2)
BarCountTx.Text = Val(BarCountTx.Text) + outDataBuf.Length / 2
Else
'-------------文本發送--------------
SerialPort.Write(outDataBuf)
BarCountTx.Text = Val(BarCountTx.Text) + outDataBuf.Length '發送位元組計數
End If
Else
MsgBox("串口未打開,請先打開串口。")
End If
Exit Sub
Err: MsgBox("資料輸入或發送錯誤!" + vbNewLine + ErrorToString())
End Sub
Private Sub ButtonClearSendBuf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClearSendBuf.Click
TextBoxSend.Text = ""
End Sub
Private Sub ButtonClearRecieveBuf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonClearRecieveBuf.Click
TextBoxRecieve.Text = ""
End Sub
Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort.DataReceived
If HexRecieveFlag Then
'-----------十六進制顯示------------
Dim inDataLen As Integer = SerialPort.BytesToRead()
If inDataLen > 0 Then
Dim inBytes(inDataLen - 1) As Byte, bytes As Byte
Dim strHex As String = ""
SerialPort.Read(inBytes, 0, inDataLen)
For Each bytes In inBytes
strHex = strHex + [String].Format("{0:X2} ", bytes)
Next
TextBoxRecieve.Invoke(RecieveRefresh, strHex)
BarCountRx.Text = (Val(BarCountRx.Text) + inDataLen).ToString '接收位元組計數
End If
Else
'-------------文本顯示--------------
Dim str As String
str = SerialPort.ReadExisting
TextBoxRecieve.Invoke(RecieveRefresh, str)
BarCountRx.Text = (Val(BarCountRx.Text) + str.Length).ToString '接收位元組計數
End If
'TextBoxRecieve.AppendText(stringout)
End Sub
Private Sub TextBoxRecieve_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxRecieve.TextChanged
TextBoxRecieve.SelectionStart = TextBoxRecieve.Text.Length
TextBoxRecieve.ScrollToCaret()
If AutoClearFlag = True And TextBoxRecieve.Lines.Length > TextBoxRecieve.Height / TextBoxRecieve.Font.Height * 3 Then
TextBoxRecieve.Text = ""
End If
End Sub
Private Sub CheckBoxAutoClear_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxAutoClear.CheckedChanged
AutoClearFlag = CheckBoxAutoClear.Checked
End Sub
Private Sub TimerAutoSend_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerAutoSend.Tick
ButtonSendData_Click(Nothing, Nothing)
End Sub
Private Sub TextBoxSendPeriod_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBoxSendPeriod.TextChanged
If Val(TextBoxSendPeriod.Text) > 0 Then
TimerAutoSend.Interval = Val(TextBoxSendPeriod.Text)
TimerAutoSend.Enabled = CheckBoxAutoSend.Checked
Else
CheckBoxAutoSend.Checked = False
TimerAutoSend.Enabled = CheckBoxAutoSend.Checked
MsgBox("請確保發送周期為正整數!")
TextBoxSendPeriod.Text = "1000"
End If
End Sub
Private Sub CheckBoxAutoSend_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxAutoSend.CheckedChanged
If CheckBoxAutoSend.Checked = True Then
If SerialPort.IsOpen = True Then
If Val(TextBoxSendPeriod.Text) > 0 Then
TimerAutoSend.Interval = Val(TextBoxSendPeriod.Text)
TimerAutoSend.Enabled = CheckBoxAutoSend.Checked
Else
CheckBoxAutoSend.Checked = False
MsgBox("請確保發送周期為正整數!")
TextBoxSendPeriod.Text = "1000"
End If
Else
CheckBoxAutoSend.Checked = False
MsgBox("請在發送前先打開串口!")
End If
Else
TimerAutoSend.Enabled = CheckBoxAutoSend.Checked
End If
End Sub
Private Sub BarBunttonClearCount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BarBunttonClearCount.Click
BarCountRx.Text = "0"
BarCountTx.Text = "0"
End Sub
Private Sub ButtonSendFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSendFile.Click
Dim DialogOpen As New System.Windows.Forms.OpenFileDialog
DialogOpen.Multis
uj5u.com熱心網友回復:
不知道怎么上傳代碼
uj5u.com熱心網友回復:
多謝回復 代碼我填上去了 您看看
uj5u.com熱心網友回復:
簡單的東西被你弄的極其復雜:其實就2步驟:1.配置串口
SerialPort gserialport=new SerialPort();
gserialport.PortName = "Com1"; //串口號
gserialport.BaudRate = 57600; //波特率
gserialport.Parity = Parity.None;
gserialport.StopBits = StopBits.One;
gserialport.DataBits = 8; //資料位
gserialport.Handshake = Handshake.None;
if (!gserialport.IsOpen)
{
gserialport.Open();
}
2.讀資料(有些串口需要預發指令的需要先發送指令在讀取)
if (GlobalSerialPort.gserialport.IsOpen)
{
int n = GlobalSerialPort.gserialport.BytesToRead;
byte[] buf = new byte[n]; //buf就是你讀到的資料啦
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/63460.html
標籤:VB.NET
