標籤

顯示具有 RS232 標籤的文章。 顯示所有文章
顯示具有 RS232 標籤的文章。 顯示所有文章

2011年9月18日 星期日

RS-232 通訊讀取

對一個初學者來說, RS-232 通訊是一件苦差事. 從最早的 MSCOMM 到 .Net FrameWork 之後, 語法變更十分巨大. 因此如果您也是從組合語言起家的. 那進入 .Net FrameWork 之後, 你會完全不知道該怎麼辦.
下面的程式. 為基本 RS-232 通訊, 並與台灣 TCBUS 連接的範例. 程式碼如下.
其實, 最大的分別點只有. "當COM PORT 傳回資料時" 進行讀取, 使用"委派" 讓程式得以在多工下執行. 並沒有什麼其他的困難點, 而 TCBUS 最後的結尾字元為 0x13h 所以只需要大方的.
serialPort.WriteLine(cmd)
就完事了. 如果通訊方式非以換行字元為主, 那請參考前部落格的方式. 以陣列方式填入即可.

Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.Windows.Forms
Imports System.Net.Sockets
Imports System.Net
Imports System.Text
Public Class Form1
Public Shared ports As String() = IO.Ports.SerialPort.GetPortNames()
Public Shared myport As String
Public read_flag As Boolean
Public mystring As String
Public m_length As Integer
Public m_arRxBfx(25) As Byte
Public buffer_string As String
Public m_arTxBfx(25) As Byte
Delegate Sub PrintToText(ByVal InputString As String)
Private Sub ShowText(ByVal strReceive As String)
If Me.response.InvokeRequired Then
Dim d As New PrintToText(AddressOf ShowText)
Me.Invoke(d, New Object() {strReceive})
Else
Me.response.Text += mystring
End If
End Sub
Delegate Sub findport(ByVal InputString As String)
Public Shared Sub showport(ByVal strReceive As String)
If Form1.response.InvokeRequired Then
Dim d As New findport(AddressOf showport)
Form1.Invoke(d, New Object() {strReceive})
Else

For Each port In ports
' listBox1.Items.Add("Item " & x.ToString())
Form1.PortUsed.Items.Add(port)
myport = port
Next
Form1.PortUsed.SelectedIndex = 0
End If
'Form1.PortUsed.SetSelected(1, True)
End Sub
Private WithEvents serialPort As New IO.Ports.SerialPort
Dim sent_string(24) As Array
Dim register As Byte
Dim command As Byte
Dim station As Byte

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call P_learn()
End Sub
Public Sub Connect(ByVal PortNo As String)
If PortNo = "" Then
PortNo = myport
End If
If serialPort.IsOpen Then
serialPort.Close()
End If
Try
With serialPort
.PortName = PortNo
.BaudRate = 38400
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.Handshake = IO.Ports.Handshake.None
End With
serialPort.Open()
Catch ex As Exception
End Try
End Sub
Private Sub mySerialPort_DataReceived(ByVal sender As Object, _
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles serialPort.DataReceived
read_flag = True
mystring = ""
m_length = serialPort.BytesToRead
If m_length Mod 25 = 0 Then
mystring = serialPort.ReadExisting + Chr(10)
ShowText("OK")
Else
Thread.Sleep(1000)
End If
read_flag = False
End Sub
Private Sub P_learn(ByVal cmd As String)
Connect(myport)
serialPort.WriteLine(cmd)
End Sub
Private Sub P_send(ByVal cmd As String)
Connect(myport)
serialPort.WriteLine(cmd)
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
P_send()
End Sub

Private Sub CircuitName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CircuitName.HandleCreated
Dim count As String
For i = 0 To 99
If i < 10 Then
count = "0" + i.ToString
Else
count = i.ToString
End If
CircuitName.Items.Add(count)
Next
End Sub
Protected Overrides Sub OnLoad(ByVal e As EventArgs)
showport("OK")
End Sub

Private Sub P_learn()
MsgBox("沒有字串")
End Sub
Private Sub P_send()
MsgBox("沒有字串")
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReadAll.Click
Dim cmd As String = "*C0009FEFF00FF0200000000"
response.Text = ""
RSCommand.Text = cmd
P_send(cmd)
End Sub
Private Sub SendCommand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendCommand.Click
Dim cmd As String
cmd = RSCommand.Text
P_send(cmd)
End Sub
Private Sub Attrib_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Attrib.HandleCreated
If Attrib.Text = "" Then
Attrib.Text = "0"
End If
End Sub
Private Sub PlusAttrib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlusAttrib.Click
Dim a As Integer
a = Attrib.Text
a = a + 1
Attrib.Text = a
End Sub
Private Sub MinusAttrib_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MinusAttrib.Click
Dim a As Integer
a = Attrib.Text
a = a - 1
Attrib.Text = a
End Sub
Private Sub OnCmd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OnCmd.Click
Dim st As Integer
Dim c As Integer
Dim precmd As String = "*C00190E0100"
Dim midcmd As String = "01"
Dim endcmd As String = "000000A"
Dim st_string As String
st = StationName.SelectedItem
c = Attrib.Text
st_string = Hex(st)
If st_string.Length = 1 Then
st_string = "0" + st_string
End If
st_string = precmd + st_string + midcmd + c.ToString + endcmd
RSCommand.Text = st_string
End Sub
Private Sub StationName_init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StationName.HandleCreated
For i = 10 To 99
StationName.Items.Add(i)
Next
StationName.SelectedIndex = 0
End Sub

End Class