I am writing a VB.Net (VS 2015) application to control equipment in real time which is connected via USB ports. The equipment requires that text string, terminated by a chr(13) be sent. Most commands will get a response back. For example one command (set VALVE1 ON) will open a valve one on the equipment. When plugging the equipment in it appears as COM7 on my laptop, though I can change this via device manager, and have done so to ensure I don't get the problem just on COM7. the euipment then sends back a string to confirm valve has opened. A light also displays to show valve has opened.
When running my code the valve opens and about seven seconds later the coms port closes and the equipment locks up. If I use Hyperterminal or Docklight to send the string, then valve opens and string comes back.
I started using Framework 4.5 and the valve would not open. I now use Framework 4.6.1 and get the results described.
If I connect to another computer running Hyperterminal or Docklight then the sent string is displayed correctly and strings can be sent back to my application.
Sub SendSerialData(ByVal data As String) ' Send strings to a serial port. Dim _continue As Boolean Dim s As String Dim sl As Integer Dim rct As Integer Dim CC As Integer Dim incoming As String Dim J As Integer J = 0 Dim jl As Integer Dim bytes() As Byte ' Dim ascii As New ASCIIEncoding() ' Create two different encodings. Dim ascii As Encoding = Encoding.UTF8 Dim unicode As Encoding = Encoding.Unicode ' Convert the string into a byte array. Dim unicodeBytes As Byte() = unicode.GetBytes(data) ' Perform the conversion from one encoding to the other. Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes) ' Convert the new byte array into a char array and then into a string. Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length) - 1) As Char ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0) Dim asciiString As New String(asciiChars) Dim indata As String ' Send strings to a serial port. ' sending one bit at a time. Have also tried whole line at a time. Using com1 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM7", 115200, Parity.None, 8, StopBits.One) com1.NewLine = Chr(13) jl = Len(data) Dim b As Byte For J = 0 To Len(asciiChars) - 1 com1.Write(asciiChars(J)) Next j MsgBox(com1.IsOpen) _continue = True s = "" sl = 0 rct = 0 ' While _continue CC = 0 Try While com1.BytesToRead = 0 And CC < 2000 CC = CC + 1 J = com1.BytesToRead End While Catch ex As Exception _continue = False End Try incoming = com1.ReadExisting rct = rct + 1 If rct > 100 Then _continue = False s = s & incoming If Len(s) >= 1 Or rct > 1000 Then _continue = False sl = Len(s) Else ' MsgBox(Len(s) & " -" & s) End If End While End Using MsgBox(rct & " - " & s & " Length " & sl) End Sub
So has anyone had this problem and found a solution please ? I suspect it is something to do with Framework, because one version would not work and current one partially works. So far no strings returned.
Alternatively can any one point me to sending strings to USB without using Framework please ?