Pages: [1]   Go Down
Author Topic: Arduino + AutoHotKey > Serial Connection  (Read 2013 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
No time to learn
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm trying to implement a serial connection between my arduino and an autohotkey script.

I am using files from:
http://arduino.cc/forum/index.php/topic,55429.0.html

To test, I am trying to turn on an LED via a (string) serial command.

I know that the arduino code pretty much works, because I can type "on" into the arduino serial monitor, and the LED turns on..

The arduino is locked to COM10 on my computer.

My test AHK script:
Code:
;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Windows XP +??
; Author:         ahklerner / kruzan
;
; Script Function:
; Arduino GUI Example
;
; set some defaults
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
; serial settings
ARDUINO_Port     = COM10
ARDUINO_Baud     = 9600
ARDUINO_Parity   = N
ARDUINO_Data     = 8
ARDUINO_Stop     = 1
; setup arduino serial communication
arduino_setup(start_polling_serial:=false)
; *****************GUI CODE***************

^s::
arduino_send("on")

;*************************************************************************************
; *****Do not edit below this line unless you want to change the core functionality of the script*****
;*************************************************************************************
; called when the gui is closed
;also called when program exits
GuiClose:
OnExit:
; make sure to cleanly shut down serial port on exit
arduino_close()
; this is important!! or else theprogram does not end when closed
ExitApp
#include %A_ScriptDir%\include\Arduino.ahk

My test arduino sketch:
Code:
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
char serialIn[20];
char holder = -1;
// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(8, OUTPUT);   
 digitalWrite(8, LOW);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
 
  int index = 0;
 
  while (Serial.available() > 0)
  {
    if (index < 19)
    {
      holder = Serial.read();
      serialIn[index] = holder;
index++;
  serialIn[index] = '\0';
    }
  }
 
  Serial.println(serialIn);
 
  if (strcmp(serialIn, "on") == 0)
  { 
  digitalWrite(9, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  }
  else
  {
  digitalWrite(9, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
  }
}

Whenever I attempt to run the test.ahk script, I receive an error (attached screenshot).

I can't figure out what is causing this. I have set the Baud equal to the sketch, and the COM port is correct.

If anybody has some time to lend a hand, I'd appreciate it.

Thanks,
-BBX



* Untitled.png (16.47 KB, 374x242 - viewed 85 times.)
* test.ahk (1.27 KB - downloaded 30 times.)
* serialAHKtest.ino (1.06 KB - downloaded 25 times.)
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
No time to learn
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The arduino.ahk include file:
Code:
#include %A_ScriptDir%\include\Serial.ahk
; Arduino AHK Library
arduino_setup(start_polling_serial=true,ping_device=true){
global
ARDUINO_Settings = %ARDUINO_Port%:baud=%ARDUINO_Baud% parity=%ARDUINO_Parity% data=%ARDUINO_Data% stop=%ARDUINO_Stop% dtr=off ;to=off  xon=off odsr=off octs=off rts=off idsr=off
ARDUINO_Handle := Serial_Initialize(ARDUINO_Settings)
if ping_device {
arduino_send("")
}
if (arduino_poll_serial_enabled := start_polling_serial) {
SetTimer, arduino_poll_serial, -1
}
}

arduino_send(data){
global ARDUINO_Handle
Serial_Write(ARDUINO_Handle, data)
sleep, 50
return arduino_read_raw()
}

arduino_read(){
global ARDUINO_Handle
return Serial_Read(ARDUINO_Handle, "0xFF", Bytes_Received)
}

arduino_read_raw(){
global ARDUINO_Handle
return Serial_Read_Raw(ARDUINO_Handle, "0xFF", "raw",Bytes_Received)
}

arduino_close(){
global ARDUINO_Handle, arduino_poll_serial
; turn off timer if it is running
SetTimer, arduino_poll_serial, Off
; wait a bit for timer to finished if it happened
Sleep, 100
Serial_Close(ARDUINO_Handle)
}

arduino_poll_serial:
if (IsFunc(f:="OnSerialData")&&(arduino_poll_serial_enabled == true)){
SerialData := arduino_read_raw()
if SerialData {
%f%(SerialData)
}
SetTimer, arduino_poll_serial, -1
}else{
MsgBox, OnSerialData function not defined.
SetTimer, arduino_poll_serial, Off
arduino_poll_serial_enabled = false
}
return


* Arduino.ahk (1.44 KB - downloaded 31 times.)
* Serial.ahk (10.94 KB - downloaded 37 times.)
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
No time to learn
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

For what it's worth, I believe I've found the solution to the error.
This arduino serial code apparently does not support COM channels greater than 9.
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
No time to learn
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Since my last post, I've found another issue to be a USB cord that was too long (9ft). I've replaced the cable with a shorter one.
I'm still having issues with the serial connection, it randomly stops working after a few hours.

I've also switched from AutoHotKey to C++.. No luck, the connection randomly crashes.
Logged

Chile
Offline Offline
Edison Member
*
Karma: 35
Posts: 1251
Arduino rocks?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is the arduino working normally with the normal serial monitor from the Arduino IDE?
Logged

My website: http://ried.cl

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
No time to learn
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well... Yes. I haven't left the serial monitor open long enough to see any problems working with it.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

anything new with this? I've been searching high and low for a solution to talk to an Arduino (in my case and EL SEQUENCER) with an autohotkey script.
I'm still having no luck at all. Any one out there who can help?
Logged

0
Offline Offline
Jr. Member
**
Karma: 0
Posts: 58
No time to learn
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I regret to report that there is not. I ended up wimping out and using a program called minicom in Ubuntu to pass ALL keyboard and card data to the arduino, and let it shuffle though the unlock codes.

Also the project was terminated at the request of one of my roommates to stop playing around with the door.
Logged

Pages: [1]   Go Up
Jump to: