-------VB 程式--------------------------------------------------------
Public Class Form1
Dim dat(2) As Byte
Dim readbf(2) As Byte
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim A As Byte
dat(0) = 5
SerialPort1.Write(dat, 0, 1) '在這裡顯示==>"通訊埠已經關閉"
System.Threading.Thread.Sleep(1)
SerialPort1.DiscardInBuffer()
SerialPort1.Read(readbf, 0, 1)
readbf(0) = A
TextBox1.Text = A
End Sub
End Class
--------------------------------------------------------------------
----BS2程式-------------------------------
' {$STAMP BS2px}
' {$PBASIC 2.5}
dat VAR Byte
DO
SERIN 16,16780,[dat]
DEBUG ? dat,CR
IF dat>0 THEN
SEROUT 16,16780,[DEC 125]
ENDIF
LOOP
END
--------------------------------------------------------------------作者: ghostwolf90 時間: 2009-7-19 20:07
這是一個用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作者: ghostwolf90 時間: 2009-9-11 01:22
請問有無看懂?在給我MAIL 我把程式碼給你作者: robot2009 時間: 2010-2-3 17:33
請問是指有BS2p能這樣做嗎?!?! 我看大家貼有關RS232的 範例都是 PIN:16