NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Project Help and Ideas » VB.net or Visual C++

January 20, 2011
by MortusSly
MortusSly's Avatar

Hey! First id like to say the kit is awesome and everything worked as expected! I give you thumbs up and will mention your kit.

I want to build a vb.net windows form or C++ to control with the atmega, basically stream in info.. maybe to the LCD or be able to turn on/off led's from the .exe or even in debug mode. Ive done some research and found very little explanation.. I dont need a step by step, just pointed right direction on how to stream?

January 22, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Hey MortusSly!

I am a little suprised no one else has chimed up on your thread yet, but here goes. I use VB.net 2005/8 (depending on which computer I am on) to do all my non-makefile computer interfacing with the nerdkit. For me the VB side is the easy part, while the C programming is what takes forever and leaves me tearing my hair out. I am not sure what your background is, but since others around here are WAY better at explaining the C programming of the nerdkit microcontroller, so I will try and go over stuff for the VB.net side of things.. Most of the VB stuff "should" apply to the C++, but I am not that familiar with C++.

Anyhow.. for pointing in the right direction about how to stream, here are a few of my suggestions.. planning thoughts first, then coding suggestions, and threads to read.

  1. know what you want the nerdkit to do first. so if you want the nerdkit to turn an LED on/off with a key from the computer, figure out how to code the nerdkit to do what you want by itself first. (ie. if you want the nerdkit to turn an LED on/off for 200ms three times, figure out the coding for that and get the nerdkit to do it by itself...)

  2. When trying to get the nerdkit to accept a "stream" from VB or C++, I would try to set the nerdkit to accept a single simple character to change things. It will save space on the chip and make the code/debug soo much easier..(ie. if you have 5 individual LEDs and you want to be able to turn them off and on at will individually, set up the nerdkit to accept "a", "b", "c" or "1", "2", "3" instead of a string like "LED1ON" from VB/C++..) 2a. Also I like to put a few debugging "goodies" in my code/breadboards, so I can figure out if things are working or not. Things like setting an extra LED to flash when the nerdkit gets sent anything over serial (ie TX/RX led idea), or leaving a spot in your code for testing stuff (ie 5 LED code, "a" turns on #1, "b" turns on #2, etc but "z" turns them all on for 50ms to test them)

  3. Most of the nerdkit's code comes with serial out parts to it, and swapping out to in is pretty easy, but covered in other threads. basically you just need to make sure your nerdkit's C code comes with the serial input/output sections in it..(ok a little redundant to mention it to some... but I spent 3 days cussing at my nerdkit when it wouldn't talk to my PC, all because I forgot to add the C serial in/out code into me C code..so I point it out again in hopes you learn from my own idiocy;)

  4. Getting VB to talk to the nerdkit, once the nerdkit is properly coded, is a piece of cake. VB.net has in the toolbox a "serial port" object which if dragged onto a form shows up in the "hinterland" below the form. It never actually shows up on your form, kinda like a timer, but it is still there. I belive it starts out named something like "serialportone" or something. I usually chance it to sptOne so I can keep track of it. In general, it acts like most normal controls in that it has code editable features and such. 4a. If you are not familiar with serial ports and serial communications relating to UARTs, it will be well worth it to figure out what all the settings and such mean. There are a few projects on here where the NK guys talk about them, and the "Society of Robots" has a few tutorials on serial communications as well.

  5. The ONLY really important thing to remember when playing with serial ports in VB is that they do access some of window's internals. This being said, two things: you need to make sure your code closes any serial port before the form closes or else you won't be able to use it until after you reboot, and second that if you chance a bunch of settings for COMPorts that don't go to the nerdkit windows can get real cranky..(can you say "windows blue screen of death" folks??) Most of my code has a serial port close section build into my close form on error and standard form closing code as a back up. Something you might want to consider.

  6. I don't know how your machines are setup, but on both of my machines when I use a USB-serial converter to talk to the nerdkit. Due to the way windows works with USB, even though you might be using the same physical USB port, the COM Port assigned to it in windows might be different. You can either spend a minute with device manager checking to make sure you have the right comport, or if you know your API calls you can hunt down the nerdkit with code... Anyhow, if you grab a comport and your mouse doesn't work but hops across the screen with every keystroke, you might have grabbed the wrong one..(Its hilarious to everyone else in the room when it happens)

  7. Sending or "streaming" to the nerdkit is really easy if you know what the nerdkit is expecting (points 1&2). The serial port has a send buffer but can also send short characters and such, but you need to make sure the NK is getting what it wants. (ie if it expects an ascii "a" send it that and not ascii "A" or something else.) As long as the nerdkit gets the info and is coded right, it works well. Receiving data from the nerdkit is a little trickier. VB is set up to get buffered data in from serial, which works great on big things like modems and other computers who have "big" buffers to send lots of info. But a nerdkit might only be sending a couple letters or a couple numbers which isn't usually enough to fill the buffer. There are several ways to see what the nerdkit sent back. The easier way that I prefer is to clear the receive buffer, set a VB timer for 250ms, "send" your data to the nerdkit, let the timer run for 4 iterations in a loop and at the end of each iteration dump the receive buffer to a string using the concatonate. After the 4 iterations the nerdkit should have responded and let your code dig out the response.. Not the fanciest way to do it, but it works well if you have slow reponses or noise in the buffer. I have a fancy german balance/scale at work that is HORRIBLE about sending slow, gibberish filled output, and this method works so well on it.

  8. I have rambled too much.. anyhow, nerdful's thread on "Simple Controlling Devices With PC" is a really good one for the nerdkit side of things.. This is where I started with my post nerdkit's guide projects.. He and the other folks have put a few examples of older and newer codes in there, but I don't know if you can read/use any of them.

anyhow.. hopefully that pointed in the right direction a bit.. I can post some of my code showing how to open and call serial ports if anyone wants to see it.

January 23, 2011
by Jalex
Jalex's Avatar

Hi 6ofhalfdozen Just a couple questions here. I love Visual Basic but I also know C pretty well. I don't see the advantage of using VB. Am I wrong? I usually write graphic programs with the API's in VB so I am don't have much experience using it with the com ports.

January 24, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Jalex,

Honestly, I don't think there is an advantage to using VB if you know and are comfortable with C or its variants. C and its offspring are much more powerfull programming languages that are more able to access low level components and the WINDOWS internal APIs. I prefer VB mostly because I am more comfortable and practiced with it. I have a hard time wrapping my head around some of C's quirks. For things like calling a com port and importing data from some RS232 device and dumping it into ACCESS or EXCEL through a C.O.M. object, VB is sufficient and relatively straight forward. I do a lot of that type of programming for work, pulling data off of balances or pressure transducers to document experiments or system runs. So, short answer, I don't see any obvious advantage to VB besides the personal reason of familiarity and "simplicity". I am actually kind of surprised to hear you use VB for accessing the graphics APIs. Several of the people I know who use graphics APIs strictly use C++/C# because they claim using the graphics APIs through VB are too buggy. Though honestly, I have only tried once to use the graphics APIs in VB, but didn't get too far.

January 24, 2011
by Jalex
Jalex's Avatar

I have had real good luch with VB and I find it a lot easier to debug than C++. Is VB 10 out yet? I like VB6 but it's time for an upgrade.

January 24, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Jalex,

Last I checked VB.net 2010 is out at least as download in full and express on microsoft's site. I am assuming that the pro or enterprise versions came out a little while ago. My brother teaches some advanced stuff (API, ASP, etc) in VB and C++ and he doesn't really like 2010 for either VB or C++. Though honestly, I am not sure if that is due to the "decreasing quality of MS's courseware" or the software itself. I haven't tried it as I am pretty happy with 2005 and 2008. Plus I am not a fan of getting rid of forms and replacing them with a semi-html based system(WPF).

January 25, 2011
by Jalex
Jalex's Avatar

Thanks I aggree with you. I will have to read up on it. That doesn't sound that good to me either. I guess I will just stay with C++ and the old VB. Thanks, Jim

January 26, 2011
by MortusSly
MortusSly's Avatar

6halfdozen,

Your post was extremely helpful and basically got me going good. Looks like you spent some time on it, so I hope it continues to help others.. Your LED activity and remembering to close ports saved me a ton of troubleshooting
January 28, 2011
by Jalex
Jalex's Avatar

Hi 6halfdozen I agree with MortusSly, you have been a lot of help. I would like to see some of your VB code as I don't have much experience sending Serial data or using then Com ports. I do have a lot of experience using the API's however. I use VB6.

January 28, 2011
by 6ofhalfdozen
6ofhalfdozen's Avatar

Jalex, Most of my code is for VB.net, and doesn't play well with VB6 code. The concepts are pretty much the same, though. But in typical MS fashion, everything gets renamed and moved around every other year so you gotta spend a lot of time or pay $$$$$ for figure out where they put it. Anyhow, nerdful's orginal VB6 code doesn't convert well into .net, so I mashed most of my code up from bits and pieces of the notepad version of it. anyhow.. here is some of my Vb.net 2008 code for serial port work with my NK, attempting to do basically what Nerdful did. As previously mentioned, if you code up your NK "well" the VB part really doesn't have much to do but make it a little easier to control over pushing a tiny button or such. I have more code, just not on this computer. I will post more usefull code when i get on that computer this weekend.

some info for understanding my coded gibberish:

  • I loaded a serial port onto the main form, called it "sptOne", on this computer I just set the properties to 115.2, 8N1, com4 (where it usually loads) On my other computer I have a version that tries to look for the comport with the nerdkit, though it doesn't always work.
  • I have a boolean called "blnConnected" as a way of keeping track of my comport's status
  • I have a button for connect/disconnect the comport called "cmdConnect" which changes color

this chunk of code basically opens the comport or closes it. My "crash recovery" code also redirects here to close out the port on crash. the code telling the port to write 6-9,0 tells the NK to turn off all my LEDs too.

Private Sub cmdConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnect.Click
        'testing to see if comport is already open or not
        If blnConnected = False Then 'comport is closed, so open it and toggle button
            sptOne.Open()
            cmdConnect.BackColor = Color.Green
            blnConnected = True
        Else 'comport open, so turn off LEDs and shut it down
            sptOne.Write(6)
            sptOne.Write(7)
            sptOne.Write(8)
            sptOne.Write(9)
            sptOne.Write(0)
            sptOne.Close()
            cmdConnect.BackColor = Color.Red
            blnConnected = False
        End If
    End Sub

My NK is set up with 5 LEDs as "outputs" so the next section basically turns them on or off depending on which button or key I press. Basically the specific key press or button click has the serial port send a number to the NK. I chose 1,2,3,4,5 as LED ons and 6,7,8,9,0 as LED off commands in the NK code, so sending a 1 then later sending 6 would turn the one led on then off later.

Private Sub cmdOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOne.Click
        sptOne.Write(1)

    End Sub

the code below is to make an LED sequence chaser or as I call it a "singer". For that I added a timer into Vb, called tmrTwo which is set for 500ms. A button turns tmrTwo on and code turns it off at the end of the "song". I created an integer array called intSongs and a position counter integer called intSing. Basically what happens is as the timer ticks along, every 500ms it "ticks" which steps through the integer array, turning one LED on and the previous one off. My loops and such are kinda messily coded, but it works well.

    Private Sub tmrTwo_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTwo.Tick
        If intSing = 0 Then
            sptOne.Write(intSongs(intSing))

        ElseIf intSing < intSongs.Length Then
            sptOne.Write(intSongs(intSing))
            off = intSing - 1
            Select Case intSongs(off)
                Case 1
                    sptOne.Write(6)
                Case 2
                    sptOne.Write(7)
                Case 3
                    sptOne.Write(8)
                Case 4
                    sptOne.Write(9)
                Case 5
                    sptOne.Write(0)
                Case Else

            End Select

        ElseIf intSing = (intSongs.Length) Then
            sptOne.Write((intSongs(intSing - 1)) + 5)
            tmrTwo.Enabled = False
            Exit Sub
        Else

        End If

        intSing += 1
    End Sub

    Anyhow, thats a little code. I don't know if it will help you much, as nerdful's code looked a bit more well done and on a similar version, but hopefully there are a few good ideas you can use.
January 29, 2011
by Jalex
Jalex's Avatar

Thanks 6ofhalfdozen That gives me something to look at anyway. I saw that VB6 has a special form for using the Com ports but I had never used it as I don't use them for any of my graphic programs. Most of my stuff is paint programs and picure puzzles and stuff like that.

Post a Reply

Please log in to post a reply.

Did you know that you can connect digital calipers to a microcontroller? Learn more...