1、在Form1的设计模式下添加以下控件:
2、添加好控件之后我们就可以打开Form1.vb进行编程了:
1 '使用串口需要引用的命名空间 2 Imports System.IO.Ports 3 Imports System 4 5 6 Public Class Form1 7 8 9 10 '在设计视图中双击Form1窗体出现的函数,在第一次显示窗体前发生的事件写在这个里面,类似于ViewWillAppear 11 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 12 13 Dim ports As String() = SerialPort.GetPortNames() '必须用命名空间,用SerialPort,获取计算机的有效串口 14 15 Dim port As String 16 17 For Each port In ports 18 portnamebox.Items.Add(port) '向combobox中添加项 19 Next 20 21 '初始化界面 22 baudratebox.Text = baudratebox.Items(2) '注释和不注释的地方可以替换 23 portnamebox.Text = portnamebox.Items(0) 24 'baudratebox.SelectedIndex() = 2 25 ' portnamebox.SelectedIndex() = 0 26 Serial_Port1() '初始化串口 27 Label3.Text = SerialPort1.IsOpen 28 statuslabel.Text = "串口未连接" 29 statuslabel.ForeColor = Color.Red 30 sendbox.Text = "123" 31 receivebytes.Text = "0" 32 linecheck.Enabled = True 33 timebox.Enabled = True 34 35 '让发送、接收数据按钮不能点击(失能) 36 Button1.Enabled = False 37 Button2.Enabled = False 38 Button3.Enabled = False 39 40 41 42 43 End Sub 44 45 46 Private Sub Serial_Port1() '设置串口参数 47 'SerialPort1.BaudRate = Val(baudratebox.Text) '波特率 48 'SerialPort1.PortName = portnamebox.Text '串口名称 49 SerialPort1.PortName = portnamebox.SelectedItem 50 SerialPort1.BaudRate = Val(baudratebox.SelectedItem) 51 SerialPort1.DataBits = 8 '数据位 52 SerialPort1.StopBits = IO.Ports.StopBits.One '停止位 53 SerialPort1.Parity = IO.Ports.Parity.None '校验位 54 End Sub 55 56 57 '关闭串口连接 58 Private Sub closebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closebtn.Click 59 Try 60 SerialPort1.Close() '关闭串口 61 62 '让发送、接收数据按钮不能点击(失能) 63 Button1.Enabled = False 64 Button2.Enabled = False 65 Button3.Enabled = False 66 67 Label3.Text = SerialPort1.IsOpen 68 If SerialPort1.IsOpen = False Then 69 statuslabel.Text = "串口未连接" 70 statuslabel.ForeColor = Color.Red 71 receivebox.Text = "" 72 receivebytes.Text = "" 73 End If 74 Catch ex As Exception 75 MessageBox.Show(ex.Message) 76 End Try 77 End Sub 78 79 80 '打开串口连接 81 Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openbtn.Click 82 Try 83 SerialPort1.Open() '打开串口 84 timebox.Enabled = True 85 '使能按钮 86 Button1.Enabled = True 87 Button2.Enabled = True 88 Button3.Enabled = True 89 90 91 Label3.Text = SerialPort1.IsOpen 92 If SerialPort1.IsOpen = True Then 93 statuslabel.Text = "串口已连接" 94 statuslabel.ForeColor = Color.Green 95 End If 96 Catch ex As Exception 97 MessageBox.Show(ex.Message) 98 End Try 99 End Sub 100 101 102 103 '手动发送数据 104 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 105 send() 106 End Sub 107 108 109 '触发接收事件,接收数据 110 Public Sub Sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived 111 Me.Invoke(New EventHandler(AddressOf Sp_Receiving)) '调用接收数据函数 112 End Sub 113 114 115 116 '接收数据过程 117 Private Sub Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs) 118 119 ' Dim strIncoming As Byte 120 Dim strIncoming As Integer 121 Dim str1() As String 122 Dim str2() As String 123 Dim bytes() As Byte 124 Dim i As Integer 125 Try 126 Threading.Thread.Sleep(100) '添加的延时 127 ' receivebytes.Text = Str(Val(receivebytes.Text) + SerialPort1.BytesToRead) 128 receivebytes.Text = Str(SerialPort1.BytesToRead) 129 130 If SerialPort1.BytesToRead > 0 Then 131 132 ReDim bytes(SerialPort1.BytesToRead) 133 'strIncoming = Convert.ToByte(SerialPort1.ReadByte()) 134 If receivecheck.Checked = True Then 135 strIncoming = SerialPort1.ReadByte() 136 bytes(0) = strIncoming 137 For i = 1 To SerialPort1.BytesToRead 138 strIncoming = SerialPort1.ReadByte() '读取缓冲区中的数据 139 bytes(i) = strIncoming 140 Next 141 ' SerialPort1.Write(sendbox.Text)'发送数据 142 SerialPort1.DiscardInBuffer() 143 str1 = Split(BitConverter.ToString(bytes), "-") 144 145 ReDim str2(str1.Length - 1) '去除str1中最后的字符 146 For i = 0 To str1.Length - 2 147 str2(i) = str1(i) 148 Next 149 receivebox.Text = receivebox.Text & Join(str2, " ") 150 'BitConverter.ToString(bytes) 151 Else 152 receivebox.Text = receivebox.Text & SerialPort1.ReadExisting() 153 End If 154 155 End If 156 157 Catch ex As Exception 158 MessageBox.Show(ex.Message) 159 End Try 160 End Sub 161 162 Public Sub send() '发送数据过程 163 Dim databyte() As Byte 164 Dim str1() As String 165 Dim str2 As String 166 Dim str3 As String 167 Dim i As Integer 168 169 Try 170 If sendcheck.Checked = False Then '不按照16进制发送 171 'timecheck.Enabled = True 172 173 If linecheck.Checked = False Then '判断是否选中分行发送 174 SerialPort1.Write(sendbox.Text) 175 Else 176 SerialPort1.WriteLine(sendbox.Text) 177 End If 178 179 Else '按照16进制发送 180 If InStr(sendbox.Text, " ") Then '判断是否有空格 181 str1 = Split(sendbox.Text) 182 str2 = Join(str1, "") 183 Else 184 str2 = sendbox.Text 185 End If 186 187 If str2.Length Mod 2 = 0 Then '判断字符串字节数是否为偶数 188 ReDim databyte(str2.Length / 2) '重新定义数组 189 For i = 0 To str2.Length / 2 - 1 190 databyte(i) = Convert.ToByte(Mid(str2, 2 * i + 1, 2), 16) '两个字符转换为一个16进制字节 191 'databyte(i) = Val(Mid(str2, 2 * i + 1, 2)) 192 Next 193 SerialPort1.Write(databyte, 0, databyte.Length - 1) 194 sendbytes.Text = Str(Val(sendbytes.Text) + databyte.Length - 1) 195 Else 196 197 str3 = Mid(str2, 1, (str2.Length - 1)) & "0" & Mid(str2, str2.Length) 198 ReDim databyte(str3.Length / 2) 199 For i = 0 To str3.Length / 2 - 1 200 databyte(i) = Convert.ToByte(Mid(str3, 2 * i + 1, 2), 16) 201 Next 202 SerialPort1.Write(databyte, 0, databyte.Length - 1) 203 sendbytes.Text = Str(Val(sendbytes.Text) + databyte.Length - 1) 204 End If 205 'databyte = System.Text.Encoding.Default.GetBytes(sendbox.Text)把每个字符转换成字节 206 207 End If 208 209 Catch ex As Exception 210 MessageBox.Show(ex.Message) 211 End Try 212 213 End Sub 214 215 '更改串口设置 216 Private Sub portnamebox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles portnamebox.SelectedIndexChanged 217 Try 218 Serial_Port1() 219 Catch ex As Exception 220 MessageBox.Show(ex.Message) 221 End Try 222 End Sub 223 224 '清空发送区数据 225 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 226 sendbox.Text = "" 227 End Sub 228 229 '清空接收区数据 230 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click 231 receivebox.Text = "" 232 receivebytes.Text = 0 233 234 End Sub 235 236 '定时发送数据 237 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick 238 Timer1.Interval = timebox.Text 239 send() 240 End Sub 241 242 '选择定时发送的触发事件 243 Private Sub timecheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timecheck.CheckedChanged 244 245 If timecheck.Checked = True Then 246 If timebox.Text = "" Then 247 MsgBox("时间间隔不能为0") 248 timecheck.Checked = False 249 Else 250 send() 251 timebox.Enabled = False 252 End If 253 Else 254 timebox.Enabled = True 255 End If 256 End Sub 257 258 259 260 261 262 263 End Class
然后运行,由于我电脑上没有串口,所以使用usb转串口线,这个线所用的转换芯片是ch340。
安装好驱动之后在计算机管理中查是COM3.
然后把串口的2,3针脚用杜邦线短路,因为这样的话我用电脑发送后又可以用电脑的同一个串口接收数据,方便测试。
测试效果如下: