|
這是一個用VB傳字串給RS232的程式,一個ComboBox,三個Button,兩個Textbox,程式碼如下
Imports System.IO.Ports
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnOpenPort.Text = "開啟"
btnSend.Text = "傳送"
btnReceive.Text = "接收"
Label1.Text = "通訊"
Label2.Text = "傳送的資料"
Label3.Text = "收到的資料"
cmbPort.Items.Clear()
For Each PortName As String In SerialPort.GetPortNames
cmbPort.Items.Add(PortName)
Next
cmbPort.SelectedIndex = 0
End Sub
Private Sub btnOpenPort_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenPort.Click
If SerialPort1.IsOpen Then
SerialPort1.Close()
'btnOpenPort.imagkey = "open"
btnOpenPort.Text = "開啟"
txtSend.Text = " "
txtReceive.Text = " "
btnSend.Enabled = False
btnReceive.Enabled = False
Else
SerialPort1.PortName = cmbPort.SelectedItem.ToString
SerialPort1.BaudRate = 9600
SerialPort1.Parity = Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = StopBits.One
SerialPort1.Open()
btnOpenPort.Text = "關閉"
btnSend.Enabled = True
btnReceive.Enabled = True
End If
End Sub
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
SerialPort1.Write(txtSend.Text)
End Sub
Private Sub btnReceive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReceive.Click
txtReceive.Text = SerialPort1.ReadExisting()
End Sub
End Class
BS2板程式沒有太大變化
84如果不行直接改回16780
----BS2程式-------------------------------
' {$STAMP BS2px}
' {$PBASIC 2.5}
dat VAR Byte
DO
SERIN 16,84,[dat]
DEBUG ? dat,CR
IF dat>0 THEN
SEROUT 16,84,[DEC 125]
ENDIF
LOOP
END |
|