新人首帖 关于串口通信的一点小疑问

zuo114 2020-03-08 05:40:39
初来贵地
新人第一帖~~~望能得到众神回复为盼

初学者 做了一个小程序,涉及到一个串口设备的简单通信
主要就是采值,用了几个市面常用的串口工具都能和设备正常通信(参照图片),
找到了其中的串口精灵1.0的源码 放到软件里 不知道为什么就接受不到数据了
有人知道为什么吗?是代码的原因吗
或者有没人大神共享一份能用的串口源码啊?
通信失败的截图


使用封装好的串口工具都能正常通信


...全文
312 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_29638825 2021-01-26
  • 打赏
  • 举报
回复
能不能发个源程序给我 757526837@qq.com
zuo114 2020-03-09
  • 打赏
  • 举报
回复
引用 2 楼 良朋 的回复:
看你的代码是读取磁盘文件转为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
多谢回复 代码我填上去了 您看看
zuo114 2020-03-09
  • 打赏
  • 举报
回复
不知道怎么上传代码
zuo114 2020-03-09
  • 打赏
  • 举报
回复
Imports System Imports 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
良朋 2020-03-09
  • 打赏
  • 举报
回复
看你的代码是读取磁盘文件转为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
撞墙的蝙蝠 2020-03-09
  • 打赏
  • 举报
回复
简单的东西被你弄的极其复杂:
其实就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就是你读到的数据啦
}
threenewbee 2020-03-08
  • 打赏
  • 举报
回复
代码不完整,不知道你怎么写的。用的是serialport控件么

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧