How to detect/identify a serial port programatically in a winform? - vb.net

I am programming a windows form that will communicate with a microcontroller.
After the winform is loaded, it should automatically detect the comport where the microcontroller is connected .
What I am doing is:
- Get the names of the available ports
- with a loop through these names :
- assign the port name to the serial port instance in the form
- send a command for the detection
- wait some time
- check if some text has been received
- compare the received message to the identification message
- if it is the right one break the loop and return the result, if not
continue
Below is the code of the function. I am using thread.sleep method. But the function is not working and I don't detect the board when it is there.
Can You tell me what is wrong with the function? Is the time delay blocking the reception? how should I ameliorate it?
This function is excuted at the beginning after loading the form. If I don't find the port, nothing can go forward. On the other side, there are no other threads I have to take into account at that stage of the excution. I thought about the DatarRceived Event, but it does not make sense at this stage, it will be activated after the right port has been detected.
Please let me know what you think
thank you
Public Function connect(testport As SerialPort, recognizeText As String, userCommand As String) As Boolean
Dim intReturnASCII As Integer = 0
Dim charReturnValue = Chr(intReturnASCII)
Dim returnMessage As String = ""
Dim count As Integer = 0
Dim ports As String() = IO.Ports.SerialPort.GetPortNames
If testport.IsOpen Then
testport.Close()
End If
Try
For Each newport As String In ports
testport.PortName = newport
testport.Open()
testport.Write(STX & userCommand & ETX)
Thread.Sleep(200) ' stop the userform and wait for the reception of the response
count = testport.BytesToRead
While count > 0
intReturnASCII = testport.ReadByte
returnMessage = returnMessage + Convert.ToChar(intReturnASCII)
count -= 1
End While
testport.Close()
XMCPort = newport ' Danach instantiate the serial port publicly with port name , is true instantiate
If returnMessage.Contains(recognizeText) Then
Return True
End If
Next
Return False
Catch ex As Exception
Return False
End Try
count = 0
returnMessage = ""
End Function

Related

Vb.NET Device Unique Identifier Win10

I'm trying to get a Device Unique Identifier in vb.net code. I have tried with
Private Function SystemSerialNumber() As String
Dim value As String = ""
Dim baseBoard As ManagementClass = New ManagementClass("Win32_BaseBoard")
Dim board As ManagementObjectCollection = baseBoard.GetInstances()
If board.Count > 0 Then
value = board(0)("SerialNumber")
If value.Length > 0 Then value = value.Substring(2)
End If
Return value
End Function
Which works on some computers but of the board doesn't have a serial number it returns "Default String" or whatever they put in there. Even tried with Win32_Processor and some have it and others just return "To be filled by O.E.M" lol
Also tried with,
Private Function SystemSerialNumber() As String
Dim value As String
Dim q As New SelectQuery("Win32_bios")
Dim search As New ManagementObjectSearcher(q)
Dim info As New ManagementObject
For Each info In search.Get
value = info("SerialNumber").ToString
Return value
Next
End Function
But its the same some devices have it some don't and just returns default string.
So I'm now trying is:
Private Function SystemSerialNumber() As String
Dim value As String
value = Windows.System.Profile.SystemIdentification.GetSystemIdForPublisher()
End Function
But I'm having trouble referencing to it. I tried Imports Windows.System but it just gives the error it cant be found.
As a side note I'm using this program in tablets with windows10, laptops, and desktops.
UPDATE: I'll be using as suggested by Heinzi. Thanks!
Also changed variable names to be more accurate.
Private Function NetworkAdapterMacAddress() As String
Dim McAddress As String
Dim netadapter As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim mo As ManagementObject
Dim adapter As ManagementObjectCollection = netadapter.GetInstances()
For Each mo In adapter
If mo.Item("IPEnabled") = True Then
McAddress = mo.Item("MacAddress").ToString()
Return McAddress
End If
Next
End Function
Well, there is no guaranteed ID that identifies every PC out there uniquely (fortunately, I might add. Privacy is a good thing).
You best bets are probably
the MAC of the network adapter (changes when the network adapter is replaced) or
the Windows Computer SID (changes when Windows is reinstalled).
Oh, and on a philosophical note, you might want to ponder on the Ship of Theseus.

Ping multiple device names (hostname) on the Network

A DataGridView displays hostnames at Column index 0, computer / printer names on the network.
pc1
pc2
print3
pc5
print
....
There are more than 500 such names.
I know how to ping them:
For i = 0 To DataGridView1.Rows.Count - 1
Try
If My.Computer.Network.Ping(DataGridView1.Item(0, i).Value) = True Then
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Lime
Else
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
End If
Catch ex As Exception
DataGridView1.Rows(i).DefaultCellStyle.BackColor = Color.Red
End Try
Next
The problem is that the Ping takes a very long time and the application freezes.
How can you speed up this procedure?
And let's say if the node is not available, then simply remove it from the list.
An example to Ping multiple addresses at the same time, using the async version of provided by the Ping class, Ping.SendPingAsync().
This version is await-able, not the same as the Ping.SendAsync() method, still asynchronous but event-driven.
Since you're using a DataGridView to both store the IpAddress/HostName and to present the PingReply results, you need to determine a way to match the Ping result to correct Cell of the DataGridView from which the Ip/Host address was taken.
Here, I'm passing to the method the Row's Index, so when the Ping result comes back, asynchronously, we can match the response to a specific Cell in the DataGridView.
To make the initialization method more generic, I'm passing also the index of the Column where the Ip/Host address is stored and the index of the Column that will show the result (you could also just pass all indexes, not a DataGridView Control reference to the method and handle the results in a different way).
A loop extracts the addresses from the the DataGridView and creates a List(Of Task), adding a PingAsync() Task for each address found.
When the collection is completed, the List(Of Task) is passed to the Task.WhenAll() method, which is then awaited.
This method starts all the Task in the list and returns when all Task have a result.
► Note that the Ping procedure sets a TimeOut, to 5000ms here, so all the Tasks will return before or within that interval, successful or not.
You can then decide if you want to reschedule the failed Pings or not.
The UI update is handled using a Progress delegate. It's just a method (Action delegate) that is called when the Ping procedure has a result to show.
It can also be used when the method that updates the UI runs in a different Thread: the Report() method will call the Progress object delegate in the Thread that created the delegate: the UI Thread, here (in the example, we're not actually ever leaving it, though).
This is how it works:
Assume you're starting the ping sequence from Button.Click event handler.
Note that the handler is declared async.
Private Async Sub btnMassPing_Click(sender As Object, e As EventArgs) Handles btnMassPing.Click
Await MassPing(DataGridView1, 1, 2)
End Sub
Initialization method and IProgress<T> report handler:
Imports System.Drawing
Imports System.Net.NetworkInformation
Imports System.Net.Sockets
Imports System.Threading.Tasks
Private Async Function MassPing(dgv As DataGridView, statusColumn As Integer, addressColumn As Integer) As Task
Dim obj = New Object()
Dim tasks = New List(Of Task)()
Dim progress = New Progress(Of (sequence As Integer, reply As Object))(
Sub(report)
SyncLock obj
Dim status = IPStatus.Unknown
If TypeOf report.reply Is PingReply Then
status = DirectCast(report.reply, PingReply).Status
ElseIf TypeOf report.reply Is SocketError Then
Dim socErr = DirectCast(report.reply, SocketError)
status = If(socErr = SocketError.HostNotFound,
IPStatus.DestinationHostUnreachable,
IPStatus.Unknown)
End If
Dim color As Color = If(status = IPStatus.Success, Color.Green, Color.Red)
Dim cell = dgv(statusColumn, report.sequence)
cell.Style.BackColor = color
cell.Value = If(status = IPStatus.Success, "Online", status.ToString())
End SyncLock
End Sub)
For row As Integer = 0 To dgv.Rows.Count - 1
If row = dgv.NewRowIndex Then Continue For
Dim ipAddr = dgv(addressColumn, row).Value.ToString()
tasks.Add(PingAsync(ipAddr, 5000, row, progress))
Next
Try
Await Task.WhenAll(tasks)
Catch ex As Exception
' Log / report the exception
Console.WriteLine(ex.Message)
End Try
End Function
PingAsync worker method:
Private Async Function PingAsync(ipAddress As String, timeOut As Integer, sequence As Integer, progress As IProgress(Of (seq As Integer, reply As Object))) As Task
Dim buffer As Byte() = New Byte(32) {}
Dim ping = New Ping()
Try
Dim options = New PingOptions(64, True)
Dim reply = Await ping.SendPingAsync(ipAddress, timeOut, buffer, options)
progress.Report((sequence, reply))
Catch pex As PingException
If TypeOf pex.InnerException Is SocketException Then
Dim socEx = DirectCast(pex.InnerException, SocketException)
progress.Report((sequence, socEx.SocketErrorCode))
End If
Finally
ping.Dispose()
End Try
End Function

VB.NET Convert USB as RS232

I have a hardware with USB for communicate between computer to hardware. The vendor not giving any APIs to connect to the device. They give me a protocol. But the protocol is serve for RS232 mode. I ask the vendor whether this protocol can be apply to the USB, they said 'YES'.. So, I'm thirst of idea how to use this protocol. Does anyone know? My old friend said yes I can use the USB and treat is as COM which I need to create an object. Create instance of the object which declare as a serialport as below. But it still can't get the status.
Public Sub New(ByVal intComNumber As Integer, ByVal lngBaudRate As Long, ByVal intDataLng As Integer, ByVal intStopBit As Integer, ByVal intParity As Integer)
Try
objUPSPort = New SerialPort
With objUPSPort
.PortName = ("COM" & intComNumber)
.BaudRate = lngBaudRate
.DataBits = intDataLng
.StopBits = intStopBit
.Parity = intParity
.Handshake = Handshake.None
End With
Catch ex As Exception
MsgBox("Error In Init UPSComm")
End Try
End Sub
Can someone help me identified this? This hardware is UPS. A simple command write to the port. But I get the error when get status. Below is the code to write to the UPS.
Public Function GetStatus() As String
Dim strRet As String
Dim strRecv As String
Dim byteRead() As Byte
Try
If Not IsNothing(objUPSPort) Then
objUPSPort.Open()
objUPSPort.WriteLine("Command will be here" & vbCrLf)
For i = 0 To 100000
If objUPSPort.BytesToRead >= 45 Then
Exit For
End If
Next
ReDim byteRead(objUPSPort.BytesToRead)
objUPSPort.Read(byteRead, 0, objUPSPort.BytesToRead)
strRecv = String.Empty
For i = 0 To byteRead.Length - 1
strRecv = strRecv & Chr(byteRead(i))
Next
If byteRead(38) = 48 Then
MsgBox("Power OK")
ElseIf byteRead(38) = 49 Then
MsgBox("Power Off")
Else
MsgBox("Unknown")
End If
strRet = strRecv
Return strRecv
Else
MsgBox("Error In ComPort Object")
Return String.Empty
End If
Catch ex As Exception
MsgBox("Exception In ComPort Object - " & ex.Message)
Return String.Empty
Finally
objUPSPort.Close()
End Try
End Function
I had few experiences in RS232 comm with USB, as nowadays laptops/pc they dont come with serial port no more. Serial ports usually emulated by USB, using [TTL-to-RS232 transistor, MAX like] some common supplier would use prolific as driver to emulate USB-to-RS232. First you need to know the data type, a simple string or binary.
SerialPorts is event driven, data coming thru the ports can trigger events. I assume to get UPS to send you status, first, you need to send command, as such [some pseudo];
objUPSPort.WriteLine("Command will be here" & vbCrLf)
There two ways to get the data:
Using data receive event driven :
Private Sub objUPSPort_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles objUPSPort.DataReceived
'call ReceiveData()
End Sub
Create a pooling thread to read data periodically
Private Sub threadUPSReceive()
Do
data = objUPSPort.ReadLine() 'for string
'process the data here or call ReceiveData()
Loop
End Sub
If data stream to be read is binary (similar like yours):
Private Function ReceiveData()
Dim bRead As Integer
Dim returnStr As String = vbEmpty
bRead = objUPSPort.BytesToRead 'Number of Bytes to read
Dim cData(bRead - 1) As Byte
For Each b As Byte In cData
returnStr += Chr(b) 'put data stream in readable ascii
Next
Return returnStr
End Sub
One more thing, make sure the baudrate/stopbit/databit is set correctly.
Hope this help.

Why won't my variable set to an instance of an object?

The program is a booking system (amongst other things) for a holiday letting company. I am working on the screen where you can see properties and ammend them or add more (etc)
Okay so It works fine in my other cases, but this one it just doesn't want to accept...I expect it's something stupid. Basically In the initial loading of the entire program I filled the Data Tables with the relevant info and then accessed them when needs be, in this case I am in the Form Properties and want to access Bookings (Which were made in FrmBookings) to see when the property is next booked to have guests in.
Dim Intcounter As Integer = 0
Dim NumberBookingRecords As Integer = BookingsNumRecs
Dim PropertyName As String
Dim PropertyFromBookings As String
Do
PropertyName = DTProperties(Intcounter)("Property Name").ToString
PropertyFromBookings = (DTBookings(NumberBookingRecords)("Property").ToString)
If PropertyName = PropertyFromBookings Then
lblDateOfArrival.Text = (DTBookings(NumberBookingRecords)("Arrival").ToString)
Intcounter = Intcounter + 1
Else
If Not NumberBookingRecords = 0 Then
NumberBookingRecords = NumberBookingRecords - 1
Else
End If
End If
Loop Until Intcounter >= intNumPropertyRecs
However when I get to PropertyFromBookings = (DTBookings(NumberBookingRecords)("Property").ToString)
it tells me that it could not be set to an instance of an object...no matter what I try an access from DTBookings I get the same response.
This is in the initial load form at the opening of the program
Dim FSBookings As New FileStream(strFileNameBookings, FileMode.OpenOrCreate, FileAccess.Read)
Application.DoEvents()
If FileLen(strFileNameBookings) > 0 Then
DTBookings.ReadXmlSchema(strFileNameBookings)
DTBookings.ReadXml(strFileNameBookings)
BookingsNumRecs = DTBookings.Rows.Count
intCurrRec = 1
Else
End If
FSBookings.Close()
blnStopAuto = True
blnStopAuto = False
Based on your code sample, DTBookings() is a function call. There are two possibilties here. Either:
The result of that function is Nothing, and when you try to use a Nothing as if there were an actual object there, (in this case, when trying to look up the ("Property") indexer) you'll get that exception, or ...
The result of the ("Property") index returns Nothing, in which case you'll get that exception when you try to call the .ToString() method.

Check proxy's from listbox

So i have a listbox that i have there for a list of proxies. I have 4 buttons pertaining to it. they are find, load, save and check
I have the first 3 finished and working but i haven't found anything useful pertaining to checking the proxies, the only one that i found took like 6 seconds per proxy so it took a lot of time for a decent sized list.
So how could i make it that on the press of that button, it checks all of the proxies in the listbox and it deletes the slow ones and the ones that flat out do not work. and does this at a decent pace(so it would probably be multi threaded)
and since i can not figure this out i have no code pertaining to this except for the sub for the button click i do not feel there is a need to post code
My suggestion for you is :
1)use a timer control and set it's Tick property to an appropriate value such 500;
2) create an array of BackGroudWorkers for example BackGroudWorker[20];
3)when your app start run all BackGroudWorkers in array and in tick event of Timer check if any of this BackGroudWorker completed or not.If completed and you have other item in list then run it with new Item.Do this until all list Items checked
Public Shared Function CheckProxy(ByVal Proxy As String) As Boolean
Dim prx As Uri = Nothing
If Uri.TryCreate(Proxy, UriKind.Absolute, prx) Then
Return CheckProxy(prx)
ElseIf Uri.TryCreate("http://" & Proxy, UriKind.Absolute, prx) Then
Return CheckProxy(prx)
Else
Return False
End If
End Function
Public Shared Function CheckProxy(ByVal Proxy As Uri) As Boolean
Dim iProxy As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
iProxy.ReceiveTimeout = 500 : iProxy.SendTimeout = 500
Try
'' Connect using a timeout (1/2 second)
Dim result As IAsyncResult = iProxy.BeginConnect(Proxy.Host, Proxy.Port, Nothing, Nothing)
Dim success As Boolean = result.AsyncWaitHandle.WaitOne(500, True)
If (Not success) Then
iProxy.Close() : Return False
End If
Catch ex As Exception
Return False
End Try
Dim bytData() As Byte, strData As String
Dim iDataLen As Integer = 1024
strData = String.Format("CONNECT {0}:{1} HTTP/1.0{2}{2}", "www.google.com", 80, vbNewLine)
bytData = System.Text.ASCIIEncoding.ASCII.GetBytes(strData)
If iProxy.Connected Then
iProxy.Send(bytData, bytData.Length, SocketFlags.None)
ReDim bytData(1024)
Do
Try
iDataLen = iProxy.Receive(bytData, bytData.Length, SocketFlags.None)
Catch ex As Exception
iProxy.Close() : Return False
End Try
If iDataLen > 0 Then
strData = System.Text.ASCIIEncoding.ASCII.GetString(bytData)
Exit Do
End If
Loop
Else
Return False
End If
iProxy.Close()
Dim strAttribs() As String
strAttribs = strData.Split(" "c)
If strAttribs(1).Equals("200") Then
Return True
Else
Return False
End If
End Function
You should manage your code for threads etc, as suggested by #Nima for your proxy checking problem I have 2 methods here One asks proxy string and tries to connect it.
e.g.
ProxyStatus = CheckProxy("http://192.168.1.1:8080/")
ProxyStatus is True/False depending on if proxy works or Not