Strange Serial Port problem
Strange Serial Port problem
Strange Serial Port problem
Strange Serial Port problem
Strange Serial Port problem
Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem
Strange Serial Port problem Strange Serial Port problem
Strange Serial Port problem
Go Back  Xtreme Visual Basic Talk > > > Strange Serial Port problem


Reply
 
Thread Tools Display Modes
  #1  
Old 05-26-2012, 09:42 AM
philman philman is offline
Freshman
 
Join Date: Mar 2011
Posts: 32
Default Strange Serial Port problem


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:
Code:
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.
Reply With Quote
  #2  
Old 05-26-2012, 03:46 PM
passel's Avatar
passelStrange Serial Port problem passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,028
Default

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.
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
  #3  
Old 05-27-2012, 01:41 AM
philman philman is offline
Freshman
 
Join Date: Mar 2011
Posts: 32
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Strange Serial Port problem
Strange Serial Port problem
Strange Serial Port problem Strange Serial Port problem
Strange Serial Port problem
Strange Serial Port problem
Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem Strange Serial Port problem
Strange Serial Port problem
Strange Serial Port problem
 
Strange Serial Port problem
Strange Serial Port problem
 
-->