2

Author Topic: Using UART in C or C++  (Read 10525 times)

0 Members and 2 Guests are viewing this topic.

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 147
  • Helpful? 6
Using UART in C or C++
« 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.

Offline adanvasco

  • Full Member
  • ***
  • Posts: 107
  • Helpful? 6
  • Necessity is the mother of invention.
Re: Using UART in C or C++
« Reply #1 on: March 31, 2012, 08:49:59 PM »
Would this help at all?: http://www.pjrc.com/teensy/uart.html
Knowledge does not weigh.

Offline joe61

  • Supreme Robot
  • *****
  • Posts: 417
  • Helpful? 16
Re: Using UART in C or C++
« Reply #2 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/

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

Joe

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 147
  • Helpful? 6
Re: Using UART in C or C++
« Reply #3 on: April 02, 2012, 10:57:42 AM »
Would this help at all?: 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.

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 147
  • Helpful? 6
Re: Using UART in C or C++
« Reply #4 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?

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: Using UART in C or C++
« Reply #5 on: April 03, 2012, 12:51:00 AM »
Hi  :)

Did You have a look at this thread --> Atmega16 USART works incorect Help Please.?

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)).
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 147
  • Helpful? 6
Re: Using UART in C or C++
« Reply #6 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.

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: Using UART in C or C++
« Reply #7 on: April 06, 2012, 11:36:00 AM »
Here's source code I promised :)
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

Offline VegaObscuraTopic starter

  • Robot Overlord
  • ****
  • Posts: 147
  • Helpful? 6
Re: Using UART in C or C++
« Reply #8 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:




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.

 


Get Your Ad Here

data_list