Quantcast
Viewing all articles
Browse latest Browse all 27201

Well, I didn't THINK I was a total moron, but.....

Hi, folks... been a while since I've been here, but now I find myself banging my head against another wall... The following code reads lines from a serial port (barcode scanner actually), and displays them on-screen.... what I was hoping to do was trigger certain actions based on the first character of what I've read... but the "if mychar = target" test only appears to work on the first pass through the loop! It appears that the "mychar = crap.substring..." assignment is only working on the first pass.... In every case the "crap" variable does have a new value, and that prints out fine, but "mychar" only seems to get assigned the first character on the first pass... all subsequent passes, mychar is blank!
Can anybody see where I'm going awry?

Code follows:

Imports System.Threading
Imports MSCommLib
Imports System.IO.Ports

Module Module1
Public c_port As String
Public sp As New System.IO.Ports.SerialPort
Public comport As New MSComm

Sub Main()
Dim i As Integer
Dim crap, mychar As String
Const target As String = Chr(48)
For Each sp As String In My.Computer.Ports.SerialPortNames
c_port = sp
Next
Call INIT(c_port)
Do While 1 = 1
crap = Read_BC()
Console.WriteLine(crap)
mychar = crap.Substring(0, 1)
Console.WriteLine(mychar)
If mychar = target Then
Console.WriteLine("Work order! Go grab data!")
Else : Console.WriteLine("regular tag")
End If
Loop
End Sub
Private Function Read_BC() As String
Dim junk As String = ""
Dim i As Int16
Do Until junk <> ""
Try
junk = sp.Readline
Catch ex As Exception
End Try
Loop
Return junk
End Function
Private Sub INIT(ByVal commport As String)
sp.PortName = commport
sp.BaudRate = 9600
sp.DataBits = 8
sp.Parity = Parity.None
sp.StopBits = 1
sp.Handshake = Handshake.None
sp.ReadTimeout = 1000
sp.WriteTimeout = 1000
sp.NewLine = Chr(13)
sp.Open()
Return
End Sub



End Module


Thanks for any light anyone can shed on this!

Joe (old mainframe hack)

Viewing all articles
Browse latest Browse all 27201

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>