MSCOMM Troubles

P

Pete

Hello,

Have just started using VB.Net and have been trying to work on some
updates to a very old piece of code. Currently we send messages via
the serial port in the following formate

i176.8005 <CR>
i176.800555.44 <CR>
o176.8005.222 <CR>

The <CR> showing the end of a message arriving on the comm port. For
some reason the handle input routine that I've written recognises
every other character and outputs it correctly to the text box I'm
using. But always seems to fail when it comes to the <CR> character.
Displaying a weird box instead.

I've tried several different ways of looking at the data and have cut
and pasted the text outputted into notepad where the weird black boxes
remain, but then in MSWord it happily starts a newline.

Not at all sure where I'm going wrong. Am I using the wrong comparison
to detect the <CR>? It never seems to make it into this part of the if
statement. All other characters are being displayed fine but this
isn't!

Hope someone can help?

Pete


Private Sub SUComm1_OnComm(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles SUComm1.OnComm

Static SUBuff As String
Dim NewChar As Char

SUBuff = SUComm1.Input()
Call HandleInput(SUBuff)

End Sub

Sub HandleInput(ByVal SUBuff As String)

' This is where you will process your input. This
' includes trapping characters, parsing strings,
' separating data fields, etc. For this case, you
' are simply going to display the data in the TextBox.

Dim BufferSize, Counter As Integer
Dim NextChar, Buffer As String
Dim ENQInt, ETXInt, STXInt, ACKInt, CRInt As Integer
Dim ENQStr, STXStr, ACKStr, ETXStr, CRStr As String
Dim CommStr, IDStr, ProStr, ValStr As String

STXStr = Chr(2)
ETXStr = Chr(3)
ENQStr = Chr(5)
ACKStr = Chr(6)
CRStr = Chr(13)
CommStr = Chr(111)
IDStr = Chr(105)
ProStr = Chr(112)
ValStr = Chr(118)

Buffer = SUBuff

If Buffer = ENQStr Then
'ENQ Received from SU send ENQ back. Not waiting for CR.
SUOutput.SelectionStart = Len(SUOutput.Text)
SUOutput.SelectedText = Str(Asc(Buffer)) + Chr(13) +
Chr(10)
PCOutput.SelectionStart = Len(PCOutput.Text)
PCOutput.SelectedText = "5" + Chr(13) + Chr(10)
SUComm1.Output() = ENQStr
ElseIf Buffer = ETXStr Then
'ETX Received from SU send STX back. Not waiting for CR.
SUOutput.SelectionStart = Len(SUOutput.Text)
SUOutput.SelectedText = Str(Asc(Buffer)) + Chr(13) +
Chr(10)
PCOutput.SelectionStart = Len(PCOutput.Text)
PCOutput.SelectedText = "2" + Chr(13) + Chr(10)
SUComm1.Output() = STXStr
ElseIf Buffer = CRStr Then
'CR Received from SU output to screen.
SUOutput.SelectionStart = Len(SUOutput.Text)
SUOutput.SelectedText = Chr(13) + Chr(10)
Else
If Asc(Buffer) > 32 Then
'Output buffer to the screen.
SUOutput.SelectionStart = Len(SUOutput.Text)
SUOutput.SelectedText = Buffer
Else
'output asc value to the screen.
SUOutput.SelectionStart = Len(SUOutput.Text)
SUOutput.SelectedText = Str(Asc(Buffer))
End If
End If
End Sub
 
S

scorpion53061

Hi,


Note Dick's comment concerning

"If Right$(Buffer, 2) = vbCrLf Then"

-----------



I developed an application that takes a 10-digit barcode string from my
com
port and processes it. I noticed, however, that for each string
transmitted
by the barcode scanner I get 2 comm events (or 2 OnComm's). The first
one
only contains the first 7 characters or the string. The second contains
the
last three characters. I set up the code to handle this so that the two
strings are combined to form the desired 10-digit string.

But I'm wondering, Why does this happen? What settings do I need to
have in
place to guarantee a full 10-digit string every time? Is this the
function
of the InputLen property of the MSCOMM object? What about
InBufferCount?
<<

This is completely normal. Just double-buffer data and process it when
it
all arrives. Set InputLen = 0 (the default), and RThreshold = 1. For
example,

Private Sub MSComm1_OnComm ()
Static Buffer As String
Buffer = Buffer & MSComm1.Input
If Right$(Buffer, 2) = vbCrLf Then
Process Buffer 'do something interesting here
Buffer = "" 'cleanup
End If
End Sub

Dick
--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.



If you really want to use the MSCOMM32.ocx with VB.NET then to add it to
the toolbox, right click on the toolbox, select "Add/Remove items...",
select the "COM Componets" tab and tick "Microsoft Communications
Control". Press OK and you should have it in your toolbox.

If you have the time I would rewrite the code. Have a look at
http://www.codeproject.com/dotnet/DotNetComPorts.asp?target=Serial|Communications
I know its C# and this is a vb newsgroup but the principles are the
same.


----- Steve Liebenberg wrote: -----

I have used the ms comms control extensively in my vb6
prof. developments which I am in the process of converting
to vb.net.
However, I do not seem to find the comms control in
the .net toolbar. How can I register this control in .net?




I have written a VB app using the RS323.vb code from the VB resource
kit.
When I try to run the code from a network drive I get a
securityexception
error. If I copy the file to the local c drive it works perfectly. I
have
seen posts that say the best thing to do is change the runtime security
policys on the PCs. I would rather code the program so I don't have to
change the security policy at all. I have seen a few examples of
permission
code but none are dealing with a serial port. Below is a partial copy
of
the code I am trying to make work. I think I doing this correctly, but
it
still gives the SecurityException when run on the network drive. Part
of
the problem, I think, is which permission do I need to use in the Dim
compermission line? The ultimate goal is to put this application on a
network drive and let people use it without making any changes to the
PC.
Can this be done? Thank you for your time.

Jason Dravet

'Imports System.Security
Imports System.Security.Permissions

Public Class Form1
Inherits System.Windows.Forms.Form
Private msCommPort As New Rs232
Dim setmute As Boolean = True
Dim volumelevel As Integer = 24
Dim compermission As FileIOPermission

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
compermission = New FileIOPermission(PermissionState.Unrestricted)
compermission.PermitOnly()
msCommPort.Open(1, 9600, 8, Rs232.DataParity.Parity_None,
Rs232.DataStopBit.StopBit_1, 4096)
msCommPort.Write("PON")
System.Threading.Thread.Sleep(5000)
msCommPort.Write("AVL:000")
msCommPort.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
msCommPort.Open(1, 9600, 8, Rs232.DataParity.Parity_None,
Rs232.DataStopBit.StopBit_1, 4096)
msCommPort.Write("POF")
msCommPort.Close()
Button4.FlatStyle = Windows.Forms.FlatStyle.Flat
Button4.BackColor = Color.Gray
Button4.Text = "Muted"
End Sub
 

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