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 equipment 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.
1. Sub SendSerialData(ByVal data As String)
2. ' Send strings to a serial port.
3.
4. Dim _continue As Boolean
5. Dim s As String
6. Dim sl As Integer
7. Dim rct As Integer
8. Dim CC As Integer
9. Dim incoming As String
10.
11. Dim J As Integer
12. J = 0
13. Dim jl As Integer
14. Dim bytes() As Byte
15.
16. ' Dim ascii As New ASCIIEncoding()
17.
18. ' Create two different encodings.
19. Dim ascii As Encoding = Encoding.UTF8
20.
21. Dim unicode As Encoding = Encoding.Unicode
22.
23. ' Convert the string into a byte array.
24. Dim unicodeBytes As Byte() = unicode.GetBytes(data)
25.
26.
27.
28. ' Perform the conversion from one encoding to the other.
29. Dim asciiBytes As Byte() = Encoding.Convert(unicode, ascii, unicodeBytes)
30.
31. ' Convert the new byte array into a char array and then into a string.
32. Dim asciiChars(ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length) - 1) As Char
33. ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0)
34. Dim asciiString As New String(asciiChars)
35.
36. Dim indata As String
37.
38. ' Send strings to a serial port.
39. ' sending one bit at a time. Have also tried whole line at a time.
40.
41. Using com1 As IO.Ports.SerialPort = My.Computer.Ports.OpenSerialPort("COM7", 115200, Parity.None, 8, StopBits.One)
42.
43. com1.NewLine = Chr(13)
44. jl = Len(data)
45. Dim b As Byte
46.
47. For J = 0 To Len(asciiChars) - 1
48. com1.Write(asciiChars(J))
49. Next j
50.
51. MsgBox(com1.IsOpen)
52.
53. _continue = True
54.
55. s = ""
56. sl = 0
57. rct = 0
58. '
59. While _continue
60. CC = 0
61. Try
62. While com1.BytesToRead = 0 And CC < 2000
63. CC = CC + 1
64. J = com1.BytesToRead
65. End While
66. Catch ex As Exception
67. _continue = False
68. End Try
69.
70. incoming = com1.ReadExisting
71.
72. rct = rct + 1
73. If rct > 100 Then _continue = False
74.
75. s = s & incoming
76.
77.
78. If Len(s) >= 1 Or rct > 1000 Then
79. _continue = False
80. sl = Len(s)
81.
82. Else
83.
84. ' MsgBox(Len(s) & " -" & s)
85. End If
86. End While
87. End Using
88.
89. MsgBox(rct & " - " & s & " Length " & sl)
90.
91. 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.