# Visual Studio USB problem

**URL:** <https://boards.straightdope.com/t/visual-studio-usb-problem/920205>\
**Category:** Factual Questions\
**Created:** [September 9, 2020, 9:02pm UTC](https://boards.straightdope.com/t/visual-studio-usb-problem/920205 "2020-09-09T21:02:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Crane](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/crane/32/3495_2.png) [@Crane](https://boards.straightdope.com/u/Crane)\
**Post date:** [September 9, 2020, 9:02pm UTC](https://boards.straightdope.com/t/visual-studio-usb-problem/920205/1 "2020-09-09T21:02:19Z")

</div>

I finally had to abandon VB6 and move to Visual Studio. Just downloaded Visual Studio Community and worked my way through some tutorials and now have reconstructed my present project. I have a MSP430f169 microcontroller programmed for data acquisition and a Visual Studio program that displays the immediate values in text boxes and accumulated data in charts.

Now I need to get the data from the micro into the PC for display. I got a USB interface board from Spark Fun and used it with VB6. But I’m stuck in Visual Studio. The code is:

Public Sub USBRcv()

```
    USBPort = New SerialPort()
    USBPort.PortName = "COM7"

    USBPort.Open()

    workString1 = USBPort.ReadLine()

    If workString1 = Not (Nothing) Then

        txtBoilerT.Text = workString1.Substring(0, 4)
        txtFunnelT.Text = workString1.Substring(4, 4)

        USBPort.Close()

    End If

End Sub

```

The program runs and displays the charts and text boxes with some initial data but stops where I try to read the USB port. It doesn’t throw an exception. It just stops. The data payload is 32 characters long. I want to parse out the first 8 characters to give me two values.

Any help appreciated cause I am stuck!

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [September 9, 2020, 10:10pm UTC](https://boards.straightdope.com/t/visual-studio-usb-problem/920205/2 "2020-09-09T22:10:13Z")

</div>

`SerialPort.ReadLine()` waits until it gets an EOL sequence, which by default for this object is VBLf or CHR(10). Does your device transmit data with that line ending? If not you’ll need one of the other `SerialPort.Read*()` varieties. See

> **[SerialPort.ReadLine Method (System.IO.Ports)](https://learn.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.readline?view=dotnet-plat-ext-3.1)**
>
> Reads up to the NewLine value in the input buffer.

I also find this line  
`If workString1 = Not (Nothing) Then`  
highly suspicious. It might compile but it probably doesn’t do what you think it does.  
I’ve not written [VB.Net](http://VB.Net) for a living since 2004, but at least back then the best idiom for null comparisons in VB was  
`If not workString1 Is Nothing Then`  
They later added the IsNot convenience operator so you can now write  
`If workString1 IsNot Nothing Then`

Last of all, you issue an `.Open()` but if you get Nothing back from your `ReadLine()` you don’t issue a corresponding `.Close()`. That’s a recipe for problems.

I’d use the try / finally pattern to ensure `.Close()` is always called. I’m frankly surprised the SerialPort class doesn’t implement IDisposable so you could use the `Using` syntax shortcut.

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [September 9, 2020, 10:21pm UTC](https://boards.straightdope.com/t/visual-studio-usb-problem/920205/3 "2020-09-09T22:21:35Z")

</div>

Late edit.

IF your device does transmit a reliable line ending but it isn’t VBLf = LineFeed, you can configure your `SerialPort` object to recognize that ending via the `SerialPort.NewLine` property.

Last of all, if this is some kind of instrumentation where you intend to read from the device repeatedly over the lifetime of the program execution, you don’t want to be creating and destroying `SerialPort` objects for each attempted read. Lots of ways for the OS to get unhappy about that.

Instead, make one `SerialPort` object at program start, open it once, read from it in whatever sort of loop or event triggerer or … then when the program is about to end you can `.Close()` it followed by letting it deallocate = fall out of scope.

---

<div class="post-metadata">

**Author:** ![LSLGuy](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lslguy/32/5813_2.png) [@LSLGuy](https://boards.straightdope.com/u/LSLGuy)\
**Post date:** [September 9, 2020, 10:33pm UTC](https://boards.straightdope.com/t/visual-studio-usb-problem/920205/4 "2020-09-09T22:33:55Z")

</div>

One more late edit then it’s time for dinner.

`.ReadLine()` returns a System.String. That is a sequence of Unicode characters in UTF-8. Does your device transmit data as UTF-8 or does it transmit raw bytes and you’ve fallen into the age-old trap of assuming byte = character and that a string is simply a sequence of bytes? Those are dangerously faulty assumptions for about 20 years now.

If it transmits bytes, use the `.Read(byte[], ...)` overload to obtain the number of bytes you want. If you do want to end up with Unicode characters you can then convert the byte array into a Unicode string using one of the text encoding methods.

---

<div class="post-metadata">

**Author:** ![jnglmassiv](https://avatars.discourse-cdn.com/v4/letter/j/f07891/32.png) [@jnglmassiv](https://boards.straightdope.com/u/jnglmassiv)\
**Post date:** [September 9, 2020, 11:48pm UTC](https://boards.straightdope.com/t/visual-studio-usb-problem/920205/5 "2020-09-09T23:48:57Z")

</div>

Sparkfun has a decent message board. The staff doesn’t promise to help with code (they sometimes do, though) but other users might chime in.

[https://forum.sparkfun.com](https://forum.sparkfun.com)

---

<div class="post-metadata">

**Author:** ![Crane](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/crane/32/3495_2.png) [@Crane](https://boards.straightdope.com/u/Crane)\
**Post date:** [September 10, 2020, 1:16pm UTC](https://boards.straightdope.com/t/visual-studio-usb-problem/920205/6 "2020-09-10T13:16:25Z")

</div>

Thanks!!

I examined the payload and find that I’m hot transmitting what I thought - don’t have EOF characters.

I’ll clean that up and work on the other changes you recommend above and report on the results.

You moved me off dead center.
