Strange Serial Port problem

philman
05-26-2012, 08:42 AM
Years ago I wrote a serial terminal program in VB6, and have been using it without problems. Now I am learning VB.net, so I decided to write a simple serial port program. This is how I tested the program:
My PC has 2 serial ports (actual ports - not using USB to serial converter). I have linked them with a cross-over serial cable. To test I ran the VB.net program on COM3 and the VB6 terminal on COM4.
I could send data from VB.net to VB6, but not the other way around. However, running 2 instances of the VB6 program, I had full comms, and also running 2 instances of the VB.net program I had full comms. I then ran Putty with the VB6 program and it worked perfectly. Putty and the VB.net program gave the same problem that I had with VB6/VB.net programs. I repeated these tests using 2 PCs and got the same results.

To summarise:
VB.net - VB6 (only one way comms: VB.net to VB6 )
VB.net - VB.net (works fine)
VB6 - VB6 (works fine)
VB.net - Putty(only one way comms: VB.net to Putty)
VB6 - Putty(works fine)

So it seems that something is not right with the VB.net software:
Imports System
Imports System.IO.Ports
Imports System.Threading
Public Class Form1
Shared _serialPort As SerialPort
Dim _continue As Boolean
Dim readThread As Thread = New Thread(AddressOf Read)
Private Delegate Sub showDataDelegate(ByVal inp As String)

Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
_continue = False
readThread.Abort()
_serialPort.Close()
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
_serialPort = New SerialPort()
_serialPort.PortName = "COM3"
_serialPort.BaudRate = 9600
_serialPort.Parity = Parity.None
_serialPort.StopBits = StopBits.One
_serialPort.Handshake = Handshake.None

_serialPort.ReadTimeout = 500
_serialPort.WriteTimeout = 500

_serialPort.Open()
_continue = True

readThread.IsBackground = True
readThread.Start()
End Sub

Public Sub Read()
While (_continue)
Try
Dim message As String
message = _serialPort.ReadLine()
displayData(message)
Catch ex As TimeoutException
' Do nothing
End Try
End While
End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
_serialPort.WriteLine(txtSend.Text)
End Sub

Public Sub displayData(ByVal inp As String)
If Me.InvokeRequired Then
Dim thisSub As showDataDelegate = AddressOf displayData
Me.Invoke(thisSub, inp)
Else
txtRec.Text = inp
End If

End Sub

End Class

I'd greatly appreciate any suggestions why I am having these strange problems.

passel
05-26-2012, 02:46 PM
Try adding

_serialPort.RtsEnable = True

to your setup. I haven't tried your code, but I think I've had to set that property to true to receive data. I haven't done a lot of Serial Comms in VB.Net to this point.

philman
05-27-2012, 12:41 AM
Thanks Passel, but adding that line did'nt work. I must also point out that the parameters data bits, baud rate, parity, stop bits and handshake are set the same on all the programs I tried. I also added the line:
_serialPort.DataBits = 8

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum