Society of Robots - Robot Forum

Software => Software => Topic started by: VegaObscura on March 30, 2012, 10:57:23 PM

Title: Using UART in C or C++
Post by: VegaObscura on March 30, 2012, 10:57:23 PM
I've done several projects using HyperTerminal to communicate with my circuit boards using UART through a serial port, and so far it has worked fine.  But I would like to be able to make a program that can send UART commands instead of using HyperTerminal.  I would like to be able to make a GUI with buttons that will send certain commands when clicked.  I would have no trouble making the GUI with buttons that perform functions when clicked.  What I don't know how to do is make a function that sends a certain UART command.

My preferred language is C++, as it is the language I have the most experience making GUIs with, but C will also work.  I would prefer to avoid C# and visual basic.  The intended operating system is Windows XP.  The microcontroller I tend to use is the atmega8 running at 1mhz.  The UART device is the RS232 shifter from SparkFun.

I have done a little googling on the topic in the past, but I'm not sure what terms I should be looking for.  Any help finding a tutorial or library dealing with sending commands similar to the way HyperTerminal does would be greatly appreciated.
Title: Re: Using UART in C or C++
Post by: adanvasco on March 31, 2012, 08:49:59 PM
Would this help at all?: http://www.pjrc.com/teensy/uart.html (http://www.pjrc.com/teensy/uart.html)
Title: Re: Using UART in C or C++
Post by: joe61 on March 31, 2012, 08:58:53 PM
I think he's talking about writing a program for Windows. This isn't a real good place for that since the focus here is writing code for microcontrollers.

Maybe something like this would help

http://www.programmersheaven.com/mb/windows/306436/306436/c-serial-port-programming-in-windows-using-visual-c++60/ (http://www.programmersheaven.com/mb/windows/306436/306436/c-serial-port-programming-in-windows-using-visual-c++60/)

I got that by checking with google, I don't do Windows personally.

Joe
Title: Re: Using UART in C or C++
Post by: VegaObscura on April 02, 2012, 10:57:42 AM
Would this help at all?: http://www.pjrc.com/teensy/uart.html (http://www.pjrc.com/teensy/uart.html)
Actually this looks like exactly what I was looking for.  So far I've only glanced over it, but it looks to be a library with simple and easy to use functions for sending UART commands.  This should do perfectly.  Thank you.
Title: Re: Using UART in C or C++
Post by: VegaObscura on April 02, 2012, 08:05:51 PM
After looking through the link adanvasco posted a little more, I can easily see how you would set baud rate, send data, and receive data, but I don't see any way of selecting which COM port to use.  Am I missing something here?
Title: Re: Using UART in C or C++
Post by: newInRobotics on April 03, 2012, 12:51:00 AM
Hi  :)

Did You have a look at this thread --> Atmega16 USART works incorect Help Please. (http://www.societyofrobots.com/robotforum/index.php?topic=15415.msg111221#msg111221)?

Thread above contains very simple Windows application (written in C#) that is used to check if USART on uC works correctly, if need be, I can post source code (whole project file (it needs Visual C# 2010 IDE to run)).
Title: Re: Using UART in C or C++
Post by: VegaObscura on April 03, 2012, 08:55:26 AM
It didn't occur to me before, but I think the link that adanvasco posted might be for code that gets uploaded to a microcontroller, instead of code for windows.

As for newInRobotics, yes I think that project file would be very helpful.
Title: Re: Using UART in C or C++
Post by: newInRobotics on April 06, 2012, 11:36:00 AM
Here's source code I promised :)
Title: Re: Using UART in C or C++
Post by: VegaObscura on April 10, 2012, 10:34:57 AM
I got it all working perfectly.  I now have a graphical program controlling the lights in my house.  I appreciate all the help everyone gave.

In case anyone wants to do something similar in the future, here's the c++ code I ended up using:

Code: [Select]
void uart(LPCVOID uartByte)
{
DCB dcb;
    DWORD BytesWritten;

    HANDLE hPort = CreateFile(
                    L"\\\\.\\COM2",
                    GENERIC_WRITE,
                    0,
                    NULL,
                    OPEN_EXISTING,
                    0,
                    NULL
    );

    dcb.BaudRate = CBR_4800;
    dcb.ByteSize = 8;
    dcb.Parity = NOPARITY;
    dcb.StopBits = ONESTOPBIT;

    bool retVal = WriteFile(hPort,uartByte,1,&BytesWritten,NULL);

    CloseHandle(hPort);

Be sure to use "#include <stdio>" in your includes.
With this function, you can just call uart(char); and it sends whatever character through COM2 to whatever microcontroller you have waiting.

Here's a picture of my finished program:

(http://i92.photobucket.com/albums/l34/VegaObscura3/screencap.jpg)


As you can see so far I only have it controlling 2 light sources, but adding additional sources won't be a problem.

On the hardware side, I have a cable going from the serial port in the back of my computer to an RS232 shifter from sparkfun.  That shifter is plugged into a breadboard with an atmega8.  Some of the pins on that atmega8 go to transistors.  Those transistors are connected to relays, and the relays are attached to the light sources.

So when my program sends a uart command, it goes to the atmega and tells it to turn a certain one of its pins low or high, which in turn turns the "transistor->relay->light" chain on or off.

If anyone wants a tutorial, source code, or more pictures, just post and let me know.