USBSerialPort

I have written this DIY in http://expertcore.org/ too, 
However I would like to repeat it here with more freedom and customization that offered to me by google[Thanks google].

So Today's tutorial is how to blink an led using a serial port.
In my case it's a USB based serial port.Many of the people believe that you don't have that much of control from USB 
serial ports or serial ports which are on win 7.They all myth believes. But one thing have to mention, you will have a difficulty
of getting +25V -25V pure RS232 level outputs from USB based ones. That's the reason why you need to supply power
externally when you using programmers connected with USB Serial converter.


So, In brief you don't need to buy expensive hardware, yes you could still use your old JDM programmer!
The basic principle of JDM programmer is generating ICSP signals using serial port handshaking lines.
Serial port got 2 input and 2 output handshake lines. 

 DTR      Data Terminal Ready. This was send by the computer to the device to indicate that computer is present.  
We could use this as a output.   
 DSR             Data Set Ready.Indicates that DCE (device) is ready to receive command or data. Simply this indicates that presence of the device.    
We could use this as a input    
 RTS      Request to send. The data terminal device(computer) is requesting to send data to the DCE(device).
We could use this as a output    
 CTS  Clear to send.When the DTE(computer) sends RTS command,device indicates whether he is ready to receive data or not.
We could use this as a input.


In MS-DOS it's just setting some ports. But in the windows world how the hell I could access those ports and write
to device registers? Well the answer is you don't have to.You have win32 API's to do the same thing.

When we are programming top of a operating system, we could see everything as a file. So simply we could open 
the "COM2" file and write to it. High levelly speaking writing to that file means we just output some data through
the port. And reading means just we getting input through the file. But there could be many attributes associated 
with a file. In a such case a high level operating system kernel supports functions to access and modify them.
[in linux you may heard about ioctl]. 

So when it's a modem device , you could simply use EscapeCommFunc API call to change or read those modem
status.

So that's all what we need to do.

The bellow code is the program to blink LED which is connected to RTS pin.[Note that as I already mentioned RTS
pin is a output].

/* compile: $gcc -o serial_port_led_blink.exe serial_port_led_blink.c */
#ifndef __UNICODE__
#define __UNICODE__
#endif
#ifndef UNICODE
#define UNICODE
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#include 
#include 

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);
}



And this is the simple circuit that you should construct:







And here is how it's working with my laptop computer and USBSerial Converter.

YouTube Video



Thanks for reading and Happy Coding.

ċ
serial_port.rar
(35k)
sandun dhammika Perera,
Dec 8, 2012, 2:11 PM
Comments