trouble with Serial Port class vs Hyperterminal

B

Bill

I am using VS2005 and VB and the .Net 2 Serial Port class at 9600 8 N 1

I have been using some example code to connect to another device over a
serial port and issue text commands and receive feedback


Everthing works - but the characters sent are getting mangled/dropped and
commands are not being completely read by the device;

eg sending "Get Serial" is echoed as Ge - Invalid Command" The exact
amount of the original command echoed varies randomly. and the response
part - "Invalid Command" is always complete so it is just a sending problem.

All this works just fine every time using Hyperterminal at 9600 8N1
In HT if I send "Get Serial" - it returns " Serial = 123456" and this
always works!


The Serial Port class has lots of "knobs" to tweak various attributes such a
buffer size and timeouts
How should I set it up to emulate Hyperterminal which works perfectly?


Thanks

Bill
 
D

Dick Grier

Hi,

I don't think there is anything to "tweek." You didn't post any code, so
guessing is probably not profitable. You can download a working set example
code from my homepage. Perhaps it will get you started. If you need more
help, post EXACTLY the code that you are implementing and I or someone else
may be able to help.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
B

Bill

Here is the code.
I assumed - perhaps incorrectly that if I can send a receive characters then
the code is working and the issue is in the serial comm itself.

Bill
Public Class Form1

Dim WithEvents serialPort As New IO.Ports.SerialPort



Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles MyBase.Load

For i As Integer = 0 To My.Computer.Ports.SerialPortNames.Count - 1

cbbComPorts.Items.Add(My.Computer.Ports.SerialPortNames(i))

Next

btndisconnect.Enabled = False

End Sub

Private Sub btnConnect_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnconnect.Click

If serialPort.IsOpen Then

serialPort.Close()

End If

Try

With serialPort

..PortName = cbbComPorts.Text

..BaudRate = 9600

..Parity = IO.Ports.Parity.None

..DataBits = 8

..StopBits = IO.Ports.StopBits.One

..Handshake = IO.Ports.Handshake.RequestToSendXOnXOff

..WriteTimeout = 1000

..ReadTimeout = 1000

..RtsEnable = True

..ReceivedBytesThreshold = 16

..NewLine = vbLf



End With

serialPort.Open()

lblmessage.Text = cbbComPorts.Text & " connected."

btnconnect.Enabled = False

btndisconnect.Enabled = True

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btndisconnect.Click

Try

serialPort.Close()

lblmessage.Text = serialPort.PortName & " disconnected."

btnconnect.Enabled = True

btndisconnect.Enabled = False

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub



Private Sub btnSend_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnsend.Click

Try

serialPort.WriteLine("Get_Serial")

With txtdatareceived

..SelectionColor = Color.Black

..AppendText(txtdatatosend.Text & vbCrLf)

..ScrollToCaret()

End With

txtdatatosend.Text = String.Empty

Catch ex As Exception

MsgBox(ex.ToString)

End Try

End Sub



'-------------------------------------------

' Event handler for the DataReceived

'-------------------------------------------

'The delegate and the updateTextBox() subroutine is defined as follows:

'------------------------------------------------------

' Delegate and subroutine to update the Textbox control

'------------------------------------------------------

Public Delegate Sub myDelegate()

Public Sub updateTextBox()

With txtDataReceived

..Font = New Font("Garamond", 12.0!, FontStyle.Bold)

..SelectionColor = Color.Red

..AppendText(serialPort.ReadExisting)

..ScrollToCaret()

End With

End Sub



Private Sub serialPort_DataReceived(ByVal sender As Object, ByVal e As
System.IO.Ports.SerialDataReceivedEventArgs) _

Handles serialPort.DataReceived

txtdatareceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object()
{})

End Sub

End Class
 
B

Bill

I just tried Dick's code and it works - Thanks!

So starting with working code I was able to find my problem - but not a
solution.

If I add a button to Dick's sample that writes my string then I see the same
error
With SerialPort

If .IsOpen = True Then

..Write("Get Serial" & vbCrLf)

End If

End With

So it seems that writing characters one at a time works - but not strings.

Any ideas?
 
D

Dick Grier

Hi,

There is no problem sending full strings that I know of (I do it all the
time).

However... Your device may expect a delay between each character (just as
though you had typed it). So, sending the data one character at a time (use
a loop), and add a delay (say 50 mS) between each character).

This is a frequent problem when working with legacy hardware. Back in the
"old days," assumptions were made that make our job a little harder at
times.

Dick
--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
D

Dick Grier

Great!

BTW, "Get Serial" & vbCr should be all you need. The Lf in your previous
code should be ignored by the connected device. This makes it a little
simpler.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top