Blink LED serial port (USB Serial)

General Discussions
Post Reply
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Blink LED serial port (USB Serial)

Post by SemiconductorCat » Wed Dec 05, 2012 11:15 pm

Hi all , after a long time I'm writing a DIY.

Many people thinks that you could not do general purpose IO using the serial port. But it's
wrong. Serial port itself have 2 input pins and 2 output pins.

And many people believe that you can't do it with USB serial ports. And some believe that
you couldn't do on windows 7. All those fearful alarms were wrong ! YOU COULD.


Here we need a very simple circuit.
Image

And here is how you should connect pins to.
Image

And here is the source code,

Code: Select all

#ifndef __UNICODE__
#define __UNICODE__
#endif
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include <windows.h>
#include <stdio.h>

TCHAR * serial_port = L"COM2";

void BlinkLED(HANDLE hfile)
{
  while(TRUE){
    EscapeCommFunction(hfile,SETRTS);
    sleep(1);
    EscapeCommFunction(hfile,CLRRTS);
    sleep(1);
  }
}

int main(int argc,char** argv)
{
  HANDLE hfile = CreateFile(serial_port ,GENERIC_READ |GENERIC_WRITE ,\
                            0,
							0,
							OPEN_EXISTING,
							FILE_ATTRIBUTE_NORMAL,
							0);
	if (hfile==INVALID_HANDLE_VALUE)
	{
	  MessageBox(NULL,TEXT("Serial Port Does not Exist!"),TEXT("LED ON"),NULL);
	  exit(0);
	 }
	 
	 BlinkLED(hfile);
	 
	 CloseHandle(hfile);
}


Note that , if you are not belongs to the administrator group then program may require you to run that
program as administrator.

x64 win7 compiled binary file and source file have been attached.
serial_port.rar
serial_port_led_blink
(34.84 KiB) Downloaded 433 times
Additional pictures:
https://picasaweb.google.com/1165913223 ... ember52012#


[if admin wants to move this discussion into electronic engineering or other section , please
allow other users to comment and add new things into the topic too]

--Thanks for Reading--
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: Blink LED serial port (USB Serial)

Post by SemiconductorCat » Wed Dec 05, 2012 11:34 pm

Video:

[media]http://www.youtube.com/watch?v=qGoM_ydAtBc[/media]
Post Reply

Return to “General Discussions”