Serial com-port communication with autoIt

December 7, 2008 at 5:12 pm 13 comments

This weekend I found out how to read from / write to a com port with autoIt code. I created a file with some functions that read/write single characters or lines. To communicate with a com port, I use the CreateFile function from Kernel32.dll. This is my first script where I call dll functions from within autoIt, so perhaps the code can be cleaned up some more. 

First, we’ll create a handle to the CreateFile function to open the com port (com2 in this example):

#include <WinAPI.au3>

;init DLL function, we need handle to call the function
$h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM2", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0)
$handle=$h[0]

To write a single character, I created the writeChar function:

;write a single char
func writeChar($handle,$c)
    $stString       = DLLStructCreate("char str")
    $lpNumberOfBytesWritten = 0
    DllStructSetData($stString, 1, $c)
    $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,                                      $lpNumberOfBytesWritten)
    if ($res<>true) then
       ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF)
    EndIf
EndFunc

A struct that contains a single character is created first. On the third line the character is passed to this struct. Then the _WinAPI_WriteFile function is called with the struct as argument. If everything is ok, then the character is written to the com port! The other functions work in a similar manner, and can be found in the attachment. 

This code is very usefull to communicate with microcontrollers, like a PIC uC for example. I was already able to communicate with Matlab (see my other article) which is very usefull for plotting sensor values. The autoIt communication is usefull for remote controlling Windows applications. A simple demo app to illustrate this:

autoIt Code:

#include "pcSide2.au3"
While 1
     $data = readLineBlock($handle)
     ConsoleWrite(">" & $data)
     switch $data
          case "L" & @LF
               send("{MEDIA_PREV}")
          case "R" & @LF
               send("{MEDIA_NEXT}")
          case "U" & @LF
               send("{VOLUME_UP}")
          case "D" & @LF
               send("{VOLUME_DOWN}")
          case "C" & @LF
               send("{MEDIA_PLAY_PAUSE}")
     EndSwitch
Wend

PIC code:

while (1) {
     if (!(BUTTON_CENTER)) {	//BUTTON_CENTER pressed
          fprintf (_H_USART, "C\n");
          while(!(BUTTON_CENTER));	//wait till button released
     }
     if (!(BUTTON_UP)) {	//BUTTON_UP pressed
          fprintf (_H_USART, "U\n");
          while(!(BUTTON_UP));	//wait till button released
     }
     ... 
}

So the autoIt code just waits for characters to be sent. If one of the characters U,D,L,R or C is sent (the characters come from Up,Down,Left,Right,Center in case you’re wondering), autoIt will emulate a keypress of one of the Media functions.

The PIC code  will send one of the characters when a certain button is pressed. So we are able to remote controll our media player (e.g. Windows Media Player) with our PIC uC!

Attachments:

com functions for autoIt ] 2kB 
demo Application (autoIt code) ] 0kB

Advertisements

Entry filed under: Uncategorized. Tags: , , , , , .

PIC18F4455 USB Bootloader Using Eclipse for multi-module Maven2 projects (part 2)

13 Comments Add your own

  • […] Serial com-port communication with autoIt « Kr3l’s Turf […]

    Reply
  • 2. jchd  |  December 13, 2008 at 1:53 pm

    Thank you very much for sharing your solution. It just sounds like this is the simple and efficient way to perform simple tasks using serial links from AutoIt.

    I have been delaying starting up on a little “project” (an overkill name for such small thing) involving some protocol transform between low-cost shipping scales and a proprietary shipping software. I’ve even been tempted to have a little box with some PIC inside to perform the task transparently, but it’ll really much simpler to perform an equivalent task with some AutoIt script doing the conversion on the fly and stuffing weights at the right place in the input form.

    So thank you again, I bet your code will do wonders.

    Reply
  • 3. kr3l  |  December 17, 2008 at 11:47 am

    Thanks for your comment 🙂

    Reply
  • 4. praveen  |  July 8, 2009 at 7:03 am

    Hi,
    i cant able to run your code,
    I get the error mesage $GENERIC_READ,$GENERIC_WRITE are not defined I dont know which value we want to give for those variables.
    i want to read some sensor outputs from serial port(COM3)[rs232].I would like to plot that values in autoit gui.please give me any idea to resolve this problum.my mail id is pravymon_at_gmail.com

    Reply
  • 5. Karel  |  July 8, 2009 at 7:22 am

    Hi,

    did you include WinAPI.au3 in your code?

    Karel

    Reply
  • 6. pixeldoc  |  May 31, 2010 at 12:56 am

    Nice example!

    Sadly the Attachment links are dead. Could you please fix them?

    Thanks.

    Reply
  • 7. bob.mcnair  |  August 20, 2010 at 9:58 am

    Dear Karel

    Unfortunately the links above don’t work.
    Can you please send me a working example of the pcside.au3?

    Thanks for your support!

    Cheers,
    bob.

    Reply
    • 8. Karel  |  August 20, 2010 at 10:32 am

      Hi Bob,

      I’m sorry the links don’t work anymore. I’ll check this evening if I can find the code back and place it online.

      Regards,
      Karel

      Reply
    • 9. Karel  |  August 20, 2010 at 9:15 pm

      Hi again Bob,

      Turned out I had to renew my account with Freehostia, but the files were still there. So the links work again!

      Greets,
      Karel

      Reply
  • 10. koby  |  May 4, 2011 at 2:02 pm

    How do you control COM settings such as BAUD rate and parity?

    Thanks,
    Koby

    Reply
  • 11. Trevor  |  June 21, 2013 at 8:12 pm

    Understand this is old. But you have what I was looking for. Any chance you could re-post your code library?

    Reply
  • 12. Adina  |  April 20, 2016 at 3:02 pm

    Hello! Is it possible to attach again the files from links: com functions for autoIt and demo Application (autoIt code)? Thank you

    Reply
  • 13. Marc Elser  |  April 22, 2016 at 6:11 am

    downloads have gone. Can you please repost?

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Feeds

Articles to be written…

Twitter – kr3l

my del.icio.us

RSS Google Reader Shared Stuff

  • An error has occurred; the feed is probably down. Try again later.

RSS Listening to..

  • An error has occurred; the feed is probably down. Try again later.

%d bloggers like this: