1

您好,我最近遇到一个问题,搜索了两个多月,但还没有找到解决方案。我必须编写一个程序/应用程序(我已经在 VB.NET 2010 中开始),它通过 Panasonic KX-TEM824 PBX 机器通过 RS232 端口连接(电缆已经连接:COM15/16 取决于我连接的 USB)并且它连接到解析(接收)来自 PBX 本身的数据,具有来电显示的数据,通话开始和结束的时间,通话的持续时间等。我在互联网上找到了一些已经完成工作的应用程序,但他们可以免费尝试一段时间后需要再次购买或重新启动应用程序。但我认为在代码方面没有太多要实现的。请我真的需要帮助。我在下面发布代码。ps 是为了学习。

Imports System 
Imports System.ComponentModel 
Imports System.Threading 
Imports System.IO.Ports 
Public Class frmMain 
Dim myPort As Array 'COM Ports detected on the system will be stored here 
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data 

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box. 
myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available 
cmbBaud.Items.Add(9600) 'Populate the cmbBaud Combo box to common baud rates used 
cmbBaud.Items.Add(19200) 
cmbBaud.Items.Add(38400) 
cmbBaud.Items.Add(57600) 
cmbBaud.Items.Add(115200) 

For i = 0 To UBound(myPort) 
cmbPort.Items.Add(myPort(i)) 
Next 
cmbPort.Text = cmbPort.Items.Item(0) 'Set cmbPort text to the first COM port detected 
cmbBaud.Text = cmbBaud.Items.Item(0) 'Set cmbBaud text to the first Baud rate on the list 

btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled 

End Sub 

Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click 
SerialPort1.PortName = cmbPort.Text 'Set SerialPort1 to the selected COM port at startup 
SerialPort1.BaudRate = cmbBaud.Text 'Set Baud rate to the selected value on 

'Other Serial Port Property 
SerialPort1.Parity = IO.Ports.Parity.None 
SerialPort1.StopBits = IO.Ports.StopBits.One 
SerialPort1.DataBits = 8 'Open our serial port 
SerialPort1.Open() 

btnConnect.Enabled = False 'Disable Connect button 
btnDisconnect.Enabled = True 'and Enable Disconnect button 

End Sub 

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click 
SerialPort1.Close() 'Close our Serial Port 

btnConnect.Enabled = True 
btnDisconnect.Enabled = False 
End Sub 

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click 
SerialPort1.Write(txtTransmit.Text & vbCr) 'The text contained in the txtText will be sent to the serial port as ascii 
'plus the carriage return (Enter Key) the carriage return can be ommitted if the other end does not need it 
End Sub 

Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived 
ReceivedText(SerialPort1.ReadExisting()) 'Automatically called every time a data is received at the serialPort 
End Sub 
Private Sub ReceivedText(ByVal [text] As String) 
'compares the ID of the creating Thread to the ID of the calling Thread 
If Me.rtbReceived.InvokeRequired Then 
Dim x As New SetTextCallback(AddressOf ReceivedText) 
Me.Invoke(x, New Object() {(text)}) 
Else 
Me.rtbReceived.Text &= [text] 
End If 
End Sub 

Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged 
If SerialPort1.IsOpen = False Then 
SerialPort1.PortName = cmbPort.Text 'pop a message box to user if he is changing ports 
Else 'without disconnecting first. 
MsgBox("Valid only if port is Closed", vbCritical) 
End If 
End Sub 

Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged 
If SerialPort1.IsOpen = False Then 
SerialPort1.BaudRate = cmbBaud.Text 'pop a message box to user if he is changing baud rate 
Else 'without disconnecting first. 
MsgBox("Valid only if port is Closed", vbCritical) 
End If 
End Sub 
End Class
4

1 回答 1

0

我也有这个 PBX 单元,所以我现在很好奇,但我不能检查它,直到我明天回来工作。

此代码看起来像一个 msdn 示例代码,并且没有以任何方式为 pbx 应用程序定制。也许您应该按照 hans 的建议进行操作,并首先使其与终端程序一起工作,然后仅使用正确的波特率、奇偶校验位和停止位设置您的应用程序。IIRC松下使用固定设置,因此提供选项只会使问题复杂化。

于 2012-02-06T22:49:45.663 回答