I am new to using Visual Basic and just have started coding using the language.
From simple coding, I want to try something a bit complex such as sending and receiving data through a serial port communication. I was able to go through a lot of tutorials about it and came about trying this code:
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class Form1
Dim myPort As Array
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiving of data
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.AddRange(myPort)
Button2.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SerialPort1.PortName = ComboBox1.Text
SerialPort1.BaudRate = ComboBox2.Text
SerialPort1.Open()
Button1.Enabled = False
Button2.Enabled = True
Button3.Enabled = True
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Write(RichTextBox1.Text & vbCr) 'concatenate with \n
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
SerialPort1.Close()
Button1.Enabled = True
Button2.Enabled = False
Button3.Enabled = False
End Sub
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
If Me.RichTextBox2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBox2.Text &= [text] 'append text
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
End Class
Button1 is designated for Opening the Port while Button2 and Button3 are used to Write and Close the port respectively. The RichTextBox1 is where I can write the data I want to send and the RichTextBox2 is where the data received is displayed. Now my problem is whenever I click the button to Write data on the port nothing displays. I want to ask help on how to get about with this.
You can't write a message and read it on the same end of the serial communication.
If you want to "fake" the serial communication, so that your pc talks to itself, you have 2 solutions:
Use some nullmodem emulator to virtually create COM ports pairs that are connected through software. Software like com0com, does just what you are looking for, but sometimes is a bit tricky makings thingks work well at the start.
Another solution is to have 2 serial ports in your pc (with an usb
adapter for example) and connect the TRx pin of the sending port to the RDx pin
of the reading port, and the RDx pin of the sending port to the TRx pin
of the reading port. All other pins just 1 to 1. Then you can also read from write from one port and read from the other.
Both approaches do the same, the first through software, the second physically.
Here you have another SO question about that, where you can get more info. Also google "faking Serial ports" should help.
Related
I am trying to communicate with a CBC Autoanaylyser machine which sends data across RS232 serial port. These are the device settings
enter image description here
I am connecting it to com4 port using a serial to usb adapter
ON COM4 port I am using the following VB code to read the data coming through.
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class ComReadWrite
Dim myPorts As Array
Dim txtline As String
Dim txtchar As String
Dim txtbyte As String
Dim txtexisting As String
Delegate Sub setTextCallBack(ByVal txt As String)
Private Sub ComReadWrite_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPorts = IO.Ports.SerialPort.GetPortNames()
portnamecombo.Items.AddRange(myPorts)
WriteButton.Enabled = False
CloseButton.Enabled = False
End Sub
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
SerialPort1.PortName = portnamecombo.Text
SerialPort1.BaudRate = BaudRateBox.Text
SerialPort1.ReadTimeout = 500
SerialPort1.Parity = Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = StopBits.One
SerialPort1.Open()
Start.Enabled = False
WriteButton.Enabled = True
CloseButton.Enabled = True
End Sub
Private Sub WriteButton_Click(sender As Object, e As EventArgs) Handles WriteButton.Click
SerialPort1.Write(WriteBox.Text)
End Sub
Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click
SerialPort1.Close()
Start.Enabled = True
WriteButton.Enabled = False
CloseButton.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
RecievedText(SerialPort1.ReadExisting())
End Sub
Private Sub RecievedText(ByVal txt As String)
If Me.ReadBox.InvokeRequired Then
Dim x As New setTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(txt)})
Else
Me.ReadBox.Text &= txt
End If
End Sub
Private Sub portnamecombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles portnamecombo.SelectedIndexChanged
End Sub
Private Sub Label5_Click(sender As Object, e As EventArgs)
End Sub
Private Sub BindingSource1_CurrentChanged(sender As Object, e As EventArgs) Handles BindingSource1.CurrentChanged
End Sub
End Class
On running the program the form only reads weird unreadable characters like OOOOOOOO but not any thing readable as shown in pic
only shows weird characters
The documentation which came with the device has the following pages which seem to be relevant.
page1
page2
We need to communicate the commands in ASCII code. Enq is Chr(5) ack is Chr(6). These were the unreadable characters.
Hey Thanks to the Stack overflow community for the help. I found the solution it is as follows
The machine is supposed to say enq to which host is supposed to reply ack. But when I read the serial port machine seems to be sending an unreadable character.
Well the enq is equal to ASCII code 5 which does not have any character associated with it so it's unreadable character. So if instead of serialport1.readexisting() I write serialport1.readchar() I get that the machine is saying 5. That is machine is actually sending the enq.
Now we need to send the whose ASCII value is 6
If I say serialport1.write('6') it's not get to work.
What will work is serialport1.write(Chr(6))
And using this I got the machine to send the data.
I am new to visual basic . I am sending data from arduino evey 1s say current. I wanted to print on text box. Here is my code on VB side . Problem i am facing here . I am getting data but Not ovwrwriting .it just print one after the other. I am using VB2010.
VB code
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
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
'SerialPort1.PortName = cmbPort.Text()
'SerialPort1.BaudRate = cmbBaud.Text()
btnDisconnect.Enabled = False 'Initially Disconnect Button is Disabled
End Sub
Private Sub btnConnect_Click_1(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_1(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_1(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 doe
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.Current_Read.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.Current_Read.Text &= [text]
End If
End Sub
Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
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)
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
Private Sub RELAY_ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RELAY_ON.Click
SerialPort1.Write("1")
End Sub
Private Sub RELAY_OFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RELAY_OFF.Click
SerialPort1.Write("0")
End Sub
End Class
Arduino Code.
float Output_Current ;
void TakeReading()
{
newaverage = analogRead(A5);
Output_Current = 0.0336666666667*newaverage - 17.17;
}
void loop()
{
Serial.println( Output_Current );
delay(1000);
}
I started learning about TCP/Sockets yesterday and decided to make a chatbox for a friend and I.
Unfortunately, i am having some difficulties with MultiThreading.
Whenever i am using it, i can no longer receive messages from my friend.
But, if i disable it then, everything works perfectly.
I don't know what's going on here, could somebody help?
Imports System.Net.Sockets
Imports System.Net
Public Class ServerClient
Dim _TCPServer As Socket
Dim _TCPListener As TcpListener
Dim _ListenerThread As System.Threading.Thread
Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
'When Submit is pressed, send some text to the client
Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(txtInput.Text)
txtBox.AppendText(vbCrLf & "Server: " & txtInput.Text)
txtInput.Clear()
_TCPServer.Send(bytes)
End Sub
Private Sub TCPListen()
'If somebody calls port 2424, accept it, unblock the socket and start the timer
_TCPListener = New TcpListener(IPAddress.Any, 2424)
_TCPListener.Start()
_TCPServer = _TCPListener.AcceptSocket()
btnSend.Enabled = True
txtBox.AppendText("Connection Established" & vbCrLf)
_TCPServer.Blocking = False
_Timer.Enabled = True
End Sub
Private Sub _Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _Timer.Tick
'If data has been sent, receive it
Try
Dim rcvdbytes(_TCPServer.ReceiveBufferSize) As Byte
_TCPServer.Receive(rcvdbytes)
txtBox.AppendText(vbCrLf & "Client: " & System.Text.Encoding.ASCII.GetString(rcvdbytes) & vbCrLf)
Catch ex As Exception
End Try
End Sub
Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Link the new thread to TCPListen(), allow access to all threads and wait for a call
_ListenerThread = New Threading.Thread(AddressOf TCPListen)
Control.CheckForIllegalCrossThreadCalls = False
txtBox.AppendText("Waiting for connection.." & vbCrLf)
btnSend.Enabled = False
_ListenerThread.Start()
End Sub
End Class
This example project contains four classes - TcpCommServer, TcpCommClient, clsAsyncUnbuffWriter and CpuMonitor. With these classes, you will not only be able to instantly add TCP/IP functionality to your VB.NET applications, but also has most of the bells and whistles we're all looking for. With these classes, you will be able to connect multiple clients to the server on the same port. You will be able to easily: throttle bandwidth to the clients, and send and receive files and data (text?) along 250 provided channels simultaneously on a single connection.
http://www.codeproject.com/Articles/307315/Reusable-multithreaded-tcp-client-and-server-class
Well, i learned BackgroundWorkers could do the exact same thing and now it all works.
Private Sub ServerClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Wait for a call
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
TCPListen()
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
_Timer.Enabled = True
End Sub
I'm trying to make an application that can read a 12 byte hex string from the serial port and display it into a text box, the source is a RFID module that is connected to my USB port using a RS232-USB converter.
My first problem was accessing the text box in the datarecieved event due to invalid cross-threaded operation. Upon googling that, I copy/pasted the code I needed to get it work, these are all the cross threading functions.
The OpenPort function and Datarecieved event are my own code, the rest I got off google.
The application then worked, but I would get an incomplete code, the first swipe I would get all 12 digits, and at every other swipe, the first byte would vanish. Upon some more reading, I decided to make an array, the size of the incoming data (shown in the code below), but now I get the cross-threading error again.
I know there's something wrong with the way I'm using the array. This is way beyond my VB knowledge, and I'm in over my head here, could use some expert help. I'm an electronics engineer and self taught myself a little programming to create some apps for my hardware.
Thanks.
Updated code:
Public Class Form3
Dim msg As String
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each s In System.IO.Ports.SerialPort.GetPortNames()
lstPorts.Items.Add(s)
Next s
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If lstPorts.SelectedIndex = -1 Then
MessageBox.Show("Please select a port")
Exit Sub
Else
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.PortName = lstPorts.SelectedItem.ToString
SerialPort1.Open()
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
msg = SerialPort1.ReadExisting
Me.Invoke(New Action(
Sub()
txtReceived.Clear()
txtReceived.Text += msg
End Sub))
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Close()
txtReceived.Text = ""
End Sub
End Class
Anything you read here:
If SerialPort1.IsOpen Then
msg = SerialPort1.ReadExisting()
End If
You will lose when you overwrite it here:
msg = enc.GetString(bytesarray)
I don't generally recommend trying to read strings directly from serial ports (or TCP/IP connections either for that matter). Stick with the array approach only and get rid of ReadExisting. Only when you know you have a whole "message" would I convert the bytes to a string.
Some generic tips (though this varies by protocol):
Define a buffer to read your messages into that is large enough to hold your entire message, and don't make it a local variable.
Keep track of the offset into that buffer.
When you first read, your offset will be 0 and lets say you read 2 bytes. Your offset will then be +2 for a value of 1.
Upon your second (and third, etc) read you insert starting at that offset, effectively appending the new bytes.
After each read you must inspect your buffer to see if you have received a whole message. You can do this by the total amount read (if you have fixed-length messages), or by some info in thebuffer (like a length prefix). Once you read your message you need to remove it from the buffer.
I suggest starting with a simple approach and after removing the message you reset your offset back to zero and continue the process. If the data you had received was LONGER than the message you had removed, you need to shift those extra bytes to the "left" so that they start at position 0. (A more efficient approach is to use a circular buffer, but KISS for now).
In your DataRecv event you should do something like this to update your UI:
Me.Invoke(New Action(
Sub()
'this code runs on the GUI thread, update textboxes!
Me.Text += msg
End Sub))
I'm still not a fan of ReadExisting, but if you are sure you are always getting prinatble characters out of your device then try something like this:
Public Class Form3
Private pendingMsg As New StringBuffer()
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each s In System.IO.Ports.SerialPort.GetPortNames()
lstPorts.Items.Add(s)
Next s
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If lstPorts.SelectedIndex = -1 Then
MessageBox.Show("Please select a port")
Exit Sub
Else
SerialPort1.BaudRate = 9600
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.PortName = lstPorts.SelectedItem.ToString
SerialPort1.Open()
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim completedMsg As String
completedMsg = String.Empty
pendingMsg.Append(SerialPort1.ReadExisting())
If pendingMsg.Length >= 24 Then '12 bytes in hex is 24 chars
completedMsg = pendingMsg.ToString(0, 24) '1st 24 bytes are a msg
pendingMsg.Remove(0, 24) 'pop off the msg from our buffer
Me.Invoke(New Action(
Sub()
txtReceived.Text = completedMsg
End Sub))
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Close()
txtReceived.Text = ""
End Sub
End Class
Hello there I'm encountering an issue recently and been searching more than two months but haven't found a solution for it yet. I have to write a program/application (which I already started in VB.NET 2010) that connects through Panasonic KX-TEM824 PBX machine via RS232 port (cable already connected : COM15/16 depending on USB I connect) and while it's connected to parse(receive) the data from the PBX itself, data that has caller ID, time of call started and ended, duration of the call and etc. I have found some already application on the Internet that does the work but they are free to try after awhile requires to buy or restart the app again. But I assume there's not much to implement on the code side. Please I really need help. I'm posting code below. P.s. it's for study purpose.
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
I also have this PBX unit, so im curious now, but I cant check it out till im back at work tomorrow.
This code looks like a msdn sample code, and has not been customised in any way for the pbx applicatiom. Perhaps you should do as hans suggested and get it working with a terminal program first, then set up your app with only the correct baud, parity and stop bits. IIRC the panasonic uses fixed settings, therefore providing options will just complicate the issue.