[英]How to read a single character from a com port
thank you for looking over my question.感谢您查看我的问题。 I'm trying to read from a COM port.
我正在尝试从 COM 端口读取数据。 I have a launchpad microcontroller and I've already made it so I can send single character codes to it and have the microcontroller respond accordingly.
我有一个启动板微控制器,我已经完成了,所以我可以向它发送单个字符代码,并让微控制器做出相应的响应。 Here's the application as it is currently:
这是当前的应用程序:
I can currently make pins go high and low to my hearts content.我目前可以使引脚 go 高低满足我的心。 However, I would like to a Read pin value from the controller.
但是,我想从 controller 读取引脚值。 I made a subroutine that opens up the com port and sends the character that causes the a pin to go high or low .
我做了一个子程序,打开 com 端口并将导致 a 引脚的字符发送到 go高或低。
The code on the microcontroller is setup so that if it gets the 'T' character it sends a single character back immediately.设置了微控制器上的代码,以便如果它得到“T”字符,它会立即发送回一个字符。 How do I retrieve this single character from the com port?
如何从 com 端口检索这个单个字符?
I'm new to vb.net, so be gentle.我是 vb.net 的新手,所以要温柔。 The code is below for reference.
代码如下供参考。 I open and close the com port to send data using the sendCom() function.
我打开和关闭 com 端口以使用 sendCom() function 发送数据。 I would like to have a readCom() function that would grb my character if possible.
如果可能的话,我想要一个 readCom() function 会影响我的角色。
Thanks for looking over my dilemma.感谢您查看我的困境。
~Triston ~特里斯顿
/------------------------------------ Code for reference ------------------------------------------------ /------------------------------------ 参考代码 ---------- --------------------------------------
Public Class Form1
Dim myBtn0 As Boolean
Dim myBtn1 As Boolean
Dim myBtn2 As Boolean
Dim myBtn3 As Boolean
Dim myBtn4 As Boolean
Dim myBtn5 As Boolean
Dim myBtn6 As Boolean
Dim myBtn7 As Boolean
Function sendCom(ByVal Data As Char)
Dim myCom As System.IO.Ports.SerialPort
myCom = My.Computer.Ports.OpenSerialPort("COM6", 9600, 0, 8)
myCom.Write(Data)
myCom.Close()
myCom.Dispose()
End Function
Function pinFlip(ByVal pinNum As Integer, ByVal pState As Boolean) As Integer
InfoBox.Clear()
InfoBox.Multiline = True
Select Case pinNum
Case 0
If pState Then
InfoBox.AppendText("C" + Environment.NewLine)
InfoBox.ScrollToCaret()
Else
InfoBox.AppendText("c" + Environment.NewLine)
InfoBox.ScrollToCaret()
End If
Case 1
If pState Then
InfoBox.AppendText("D" + Environment.NewLine)
InfoBox.ScrollToCaret()
Else
InfoBox.AppendText("d" + Environment.NewLine)
InfoBox.ScrollToCaret()
End If
Case 2
If pState Then
InfoBox.AppendText("E" + Environment.NewLine)
InfoBox.ScrollToCaret()
Else
InfoBox.AppendText("e" + Environment.NewLine)
InfoBox.ScrollToCaret()
End If
Case 3
If pState Then
InfoBox.AppendText("F" + Environment.NewLine)
InfoBox.ScrollToCaret()
Else
InfoBox.AppendText("f" + Environment.NewLine)
InfoBox.ScrollToCaret()
End If
Case 4
If pState Then
InfoBox.AppendText("G" + Environment.NewLine)
InfoBox.ScrollToCaret()
Else
InfoBox.AppendText("g" + Environment.NewLine)
InfoBox.ScrollToCaret()
End If
Case 5
If pState Then
InfoBox.AppendText("H" + Environment.NewLine)
InfoBox.ScrollToCaret()
Else
InfoBox.AppendText("h" + Environment.NewLine)
InfoBox.ScrollToCaret()
End If
Case 6
If pState Then
InfoBox.AppendText("I" + Environment.NewLine)
InfoBox.ScrollToCaret()
Else
InfoBox.AppendText("i" + Environment.NewLine)
InfoBox.ScrollToCaret()
End If
Case 7
If pState Then
InfoBox.AppendText("J" + Environment.NewLine)
InfoBox.ScrollToCaret()
Else
InfoBox.AppendText("j" + Environment.NewLine)
InfoBox.ScrollToCaret()
End If
End Select
End Function
Private Sub GetPorts_Click(sender As Object, e As EventArgs) Handles GetPorts.Click
' Show all available COM ports.
ListBox1.Items.Clear()
For Each sp As String In My.Computer.Ports.SerialPortNames
ListBox1.Items.Add(sp)
Next
End Sub
Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
Dim Data As Char
'Dim Com6 As System.IO.Ports.SerialPort
'Com6 = My.Computer.Ports.OpenSerialPort("COM5", 9600, 0, 8)
Data = "A"
'Com6.Open()
'System.Threading.Thread.Sleep(200)
'Com6.Write(Data)
'System.Threading.Thread.Sleep(200)
'Com6.Close()
'System.Threading.Thread.Sleep(200)
'Com6.Dispose()
sendCom(Data)
P0.BackColor = Color.Green
P1.BackColor = Color.Green
P2.BackColor = Color.Green
P3.BackColor = Color.Green
P4.BackColor = Color.Green
P5.BackColor = Color.Green
P6.BackColor = Color.Green
P7.BackColor = Color.Green
myBtn0 = True
myBtn1 = True
myBtn2 = True
myBtn3 = True
myBtn4 = True
myBtn5 = True
myBtn6 = True
myBtn7 = True
InfoBox.Clear()
InfoBox.Multiline = True
InfoBox.AppendText("All Set High" + Environment.NewLine)
InfoBox.ScrollToCaret()
End Sub
Private Sub SendB_Click(sender As Object, e As EventArgs) Handles SendB.Click
Dim Data As Char
'Dim Com6 As System.IO.Ports.SerialPort
'Com6 = My.Computer.Ports.OpenSerialPort("COM5", 9600, 0, 8)
'Com6.Open()
Data = "B"
' Send strings to a serial port.
'System.Threading.Thread.Sleep(200)
'Com6.Write(Data)
'System.Threading.Thread.Sleep(200)
'Com6.Close()
'System.Threading.Thread.Sleep(200)
'Com6.Dispose()
sendCom(Data)
P0.BackColor = Color.Red
P1.BackColor = Color.Red
P2.BackColor = Color.Red
P3.BackColor = Color.Red
P4.BackColor = Color.Red
P5.BackColor = Color.Red
P6.BackColor = Color.Red
P7.BackColor = Color.Red
myBtn0 = False
myBtn1 = False
myBtn2 = False
myBtn3 = False
myBtn4 = False
myBtn5 = False
myBtn6 = False
myBtn7 = False
InfoBox.Clear()
InfoBox.Multiline = True
InfoBox.AppendText("All Set Low" + Environment.NewLine)
InfoBox.ScrollToCaret()
End Sub
Private Sub myRead_Click(sender As Object, e As EventArgs) Handles myRead.Click
InfoBox.Clear()
InfoBox.Multiline = True
InfoBox.AppendText("The READ!" + Environment.NewLine)
InfoBox.ScrollToCaret()
End Sub
Private Sub PortInfo_Click(sender As Object, e As EventArgs) Handles PortInfo.Click
Dim Com6 As System.IO.Ports.SerialPort
Dim myData As String
InfoBox.Clear()
InfoBox.Multiline = True
Com6 = My.Computer.Ports.OpenSerialPort("COM6", 9600, 0, 8)
myData = "Port Name: " + CStr(Com6.PortName)
InfoBox.AppendText(myData + Environment.NewLine)
InfoBox.ScrollToCaret()
myData = "BaudRate: " + CStr(Com6.BaudRate)
InfoBox.AppendText(myData + Environment.NewLine)
InfoBox.ScrollToCaret()
myData = "Parity: " + CStr(Com6.Parity)
InfoBox.AppendText(myData + Environment.NewLine)
InfoBox.ScrollToCaret()
myData = "Data Width: " + CStr(Com6.DataBits)
InfoBox.AppendText(myData + Environment.NewLine)
InfoBox.ScrollToCaret()
myData = "Stop Bits: " + CStr(Com6.StopBits)
InfoBox.AppendText(myData + Environment.NewLine)
InfoBox.ScrollToCaret()
Com6.Dispose()
End Sub
Private Sub Btn0_Click(sender As Object, e As EventArgs) Handles Btn0.Click
Select Case myBtn0
Case False
P0.BackColor = Color.Green
myBtn0 = True
sendCom("C")
Case True
P0.BackColor = Color.Red
myBtn0 = False
sendCom("K")
End Select
pinFlip(0, myBtn0)
End Sub
Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles Btn1.Click
Select Case myBtn1
Case False
P1.BackColor = Color.Green
myBtn1 = True
sendCom("D")
Case True
P1.BackColor = Color.Red
myBtn1 = False
sendCom("L")
End Select
pinFlip(1, myBtn1)
End Sub
Private Sub Btn2_Click(sender As Object, e As EventArgs) Handles Btn2.Click
Select Case myBtn2
Case False
P2.BackColor = Color.Green
myBtn2 = True
sendCom("E")
Case True
P2.BackColor = Color.Red
myBtn2 = False
sendCom("M")
End Select
pinFlip(2, myBtn2)
End Sub
Private Sub Btn3_Click(sender As Object, e As EventArgs) Handles Btn3.Click
Select Case myBtn3
Case False
P3.BackColor = Color.Green
myBtn3 = True
sendCom("F")
Case True
P3.BackColor = Color.Red
myBtn3 = False
sendCom("N")
End Select
pinFlip(3, myBtn3)
End Sub
Private Sub Btn4_Click(sender As Object, e As EventArgs) Handles Btn4.Click
Select Case myBtn4
Case False
P4.BackColor = Color.Green
myBtn4 = True
sendCom("G")
Case True
P4.BackColor = Color.Red
myBtn4 = False
sendCom("O")
End Select
pinFlip(4, myBtn4)
End Sub
Private Sub Btn5_Click(sender As Object, e As EventArgs) Handles Btn5.Click
Select Case myBtn5
Case False
P5.BackColor = Color.Green
myBtn5 = True
sendCom("H")
Case True
P5.BackColor = Color.Red
myBtn5 = False
sendCom("P")
End Select
pinFlip(5, myBtn5)
End Sub
Private Sub Btn6_Click(sender As Object, e As EventArgs) Handles Btn6.Click
Select Case myBtn6
Case False
P6.BackColor = Color.Green
myBtn6 = True
sendCom("I")
Case True
P6.BackColor = Color.Red
myBtn6 = False
sendCom("Q")
End Select
pinFlip(6, myBtn6)
End Sub
Private Sub Btn7_Click(sender As Object, e As EventArgs) Handles Btn7.Click
Select Case myBtn7
Case False
P7.BackColor = Color.Green
myBtn7 = True
sendCom("J")
Case True
P7.BackColor = Color.Red
myBtn7 = False
sendCom("R")
End Select
pinFlip(7, myBtn7)
End Sub
End Class结束 Class
Your function sendCom(ByVal Data As Char) opens the COM port, then sends data, then closes it, never keeping it open for receiving data.您的 function sendCom(ByVal Data As Char)打开 COM 端口,然后发送数据,然后关闭它,从不保持打开状态以接收数据。 This is fine if you want to poll for received data and timeout if there is none.
如果您想轮询接收到的数据并在没有数据时超时,这很好。 See the example here for the receive function that you need to add to your code.
有关需要添加到代码中的接收 function,请参见此处的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.