当我尝试向显示器写入其工作并向我显示文本时,但是当我从文本框中删除文本并按发送数据时,它不会在显示中更改,而保留以前的文本。如果我写另一个文本并发送它,它会在同一行显示旧文本和新文本。
我的问题是:如何从显示中删除旧文本并显示新文本?
我使用下面的代码:
Imports System.IO.Ports
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Clear()
For Each Nport As String In SerialPort.GetPortNames
ComboBox1.Items.Add(Nport)
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
With SerialPort1
.PortName = ComboBox1.Text
.BaudRate = 9600
.DataBits = 8
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
End With
If Not (SerialPort1.IsOpen = True) Then
SerialPort1.Open()
End If
SerialPort1.DiscardOutBuffer()
SerialPort1.Write(TextBox1.Text)
SerialPort1.Close()
MsgBox("Data sent")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class
答案 0 :(得分:0)
您需要使用显示器提供的控制代码来清除它。根据{{3}},一些可用的控制代码是:
主页 ---------------- 0x1 hex 1十进制
退格 --------- 0x8十六进制8十进制
标签 ------------------- 0x9 hex 9十进制
换行 ----------- 0xA十六进制10十进制
清除Scrn --------- 0xC十六进制12十进制
回车 - 0xD十六进制13十进制
使用上述内容可以执行以下操作:
Public Class Form1
Dim displayHome As Byte() = New Byte(0) {&H1}
Dim displayClear As Byte() = New Byte(0) {&HC}
Dim displayNewLine As Byte() = New Byte(0) {&HD}
Dim displayLineFd As Byte() = New Byte(0) {&HA}
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ComboBox1.Items.Clear()
For Each Nport As String In SerialPort.GetPortNames
ComboBox1.Items.Add(Nport)
Next
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
With SerialPort1
.PortName = ComboBox1.Text
.BaudRate = 9600
.DataBits = 8
.Parity = IO.Ports.Parity.None
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
End With
If Not (SerialPort1.IsOpen = True) Then
SerialPort1.Open()
End If
SerialPort1.DiscardOutBuffer()
'This is one way though the manufacturer doesn't
'recommend it do to screen flashing.
SerialPort1.Write(displayClear, 0, 1)
SerialPort1.Write(TextBox1.Text)
'Another way would be to go to the home position
'and make sure you pad enough to cover all of the
'previous total.
SerialPort1.Write(displayHome, 0, 1)
SerialPort1.Write(TextBox1.Text.PadLeft(20))
SerialPort1.Close()
MsgBox("Data sent")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class