【问题标题】:How can I receive data from a PBX machine after I get gonnected to it?连接到 PBX 机器后如何接收数据?
【发布时间】:2012-02-06 10:18:17
【问题描述】:

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

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

【问题讨论】:

  • 您没有设置 Handshake 属性。必需,或者您需要自己将 DtrEnable 和 RtsEnable 属性设置为 true。首先将其与 HyperTerminal 或 Putty 等其他应用程序一起使用,这样您就可以确信连接良好并且您选择的设置是正确的。
  • "...我必须编写一个程序/应用程序(我已经在 VB.NET 2010 中开始..." >>>> philrobotics.com/serial-port-interfacing-with-vb-net-2010 Copy&Paste 不是在写,你已经有什么了试过了吗?

标签: vb.net serial-port pbx


【解决方案1】:

我也有这个 PBX 装置,所以我现在很好奇,但我明天要上班才能检查它。

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

【讨论】:

    猜你喜欢
    • 2020-05-15
    • 1970-01-01
    • 2012-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 2020-12-09
    • 1970-01-01
    • 2015-03-31
    相关资源
    最近更新 更多