Go Down

Topic: Arduino + AutoHotKey > Serial Connection (Read 2 times) previous topic - next topic

BBX

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: [Select]

;
; 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: [Select]

/*
  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


BBX

The arduino.ahk include file:
Code: [Select]

#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


BBX

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.

BBX

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.

eried

Is the arduino working normally with the normal serial monitor from the Arduino IDE?
My website: http://ried.cl

BBX

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

Randomlydunn

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?

BBX

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.

Go Up