AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Serial ( COM ) Port Console Script
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
aobrien



Joined: 14 Feb 2008
Posts: 70
Location: Santa Clara, CA

PostPosted: Wed Mar 17, 2010 10:23 pm    Post subject: Reply with quote

Hi haichen,

You provide some very good input. Hopefully, within the next week or two I will get some time to make the changes.

aobrien
Back to top
View user's profile Send private message
Jinxx
Guest





PostPosted: Tue Mar 23, 2010 5:18 am    Post subject: Reply with quote

Hi Everyone,

I am a total noob when it comes to AHK and COM stuff in general.
What I was thinking I could do with this script was send data out on pin on pin 3 which should increase the voltage on that pin.
From there is would trigger a relay that opens a cash drawer.

When I copy and paste the ahk script from the 1st post (the not port.dll one) and run it I get this error after it saves the notepad file:

There is a problem with Serial Port communication.
Failed Dll CreateFile, COM_FileHandle=-1
The Script Will Now Exit.

I'm not sure what to do to fix this, but as I said once I have worked my way through this fantastic script I will simply make a hotkey (say F12) to send data to COM1 increasing the voltage on pin 3 and triggering a cash drawer.

Can anyone help me out with the error?
Or perhaps pointers on where/what I can cut down on in the script?

Thanks in advance
~ Jinxx
Back to top
aobrien



Joined: 14 Feb 2008
Posts: 70
Location: Santa Clara, CA

PostPosted: Tue Mar 30, 2010 9:39 pm    Post subject: Reply with quote

Hi Jinxx,

That error means that the Initialize_COM() subroutine failed to connect to the serial port.

What you should do is uncomment line 298 shown below so that you can see the COM_Settings that are being sent to the routine.
Code:
  ;8/10/09 A BIG Thanks to trenton_xavier for figuring out how to make COM Ports greater than 9 work for USB-Serial Dongles.
  StringTrimLeft, COM_Settings, COM_Settings, COM_Port_Temp1_Len+1 ;Remove the COM number (+1 for the semicolon) for BuildCommDCB.
  ;MsgBox, COM_Port=%COM_Port% `nCOM_Settings=%COM_Settings% ;<==Uncomment this line


In my system I get a message box with the following information:
Code:
COM_Port=COM1
COM_Settings=Baud=115200 parity=N data=8 stop=1 dtr=Off


You can ususally see where the problem is with this information. You will also get the error if another application is already using that particular COM port.

On my laptop, when there is no data being sent i get -5V on the Tx pin. When I send 0x00 the Tx pin goes to +5V for about 80us then the stop bit goes low for a couple of micro seconds (don't remembed the exact number). If you send a long set of 0x00 and use an R-C filter to remove the stop bit this should work, however, I doubt that the Tx pin has the abiliby to source the amount of current that a relay would require... you will probably want to have the Tx pin turn ON/OFF a transistor which will supply the current to the relay.

Regards,
aobrien.
Back to top
View user's profile Send private message
Brater
Guest





PostPosted: Wed Mar 31, 2010 9:59 pm    Post subject: Reply with quote

Hi!
Many microcontrollers can communicate via (virtual) COM-ports with the PC (UART interface).
A very easy but famous open source (!) microcontroller project called Arduino (this is a small advert for this project because i really like it very much!!).

Back to topic: the Arduino project serves a so called "Serial Proxy". It allows accessing the COM Port as network connection.
You can find the download here: http://arduino.cc/en/Main/Software (scroll down until the end). I did not test this tool but maybe makes your problems easier and bypass the baud rate limitation.

Brater
Back to top
newbynewb
Guest





PostPosted: Fri Apr 09, 2010 12:15 pm    Post subject: Reply with quote

I cant make heads or tails of this.... id like a script to listen to the com1 port and wait untill it sends... "done intitalizing" but i dont want it to stop the output to hyperterminal... basically I want to still use hyper terminal for everything except I cant get the text so I need this to make a desison when the specific text apears anyone wanna help?
Back to top
aobrien



Joined: 14 Feb 2008
Posts: 70
Location: Santa Clara, CA

PostPosted: Sun Apr 11, 2010 12:59 am    Post subject: Reply with quote

Hi newbynewb,

what you need to do is log your HyperTerminal traffic to a file. Do the following:

1) In HyperTerminal go to Transfer -> Capture Text -> then enter the path/file name of where you want to log the data.
2) Search the file until you see the desired text.
3) Use this function to search for the desired text. This functions basically causes your script to stall until the "Needle" is found.

Code:
;##############################################################################
;###### Search last "num_char_back" of "Path" for "Needle". Wait "seconds"
;######   before trying again.
;##############################################################################
Search_File_Last_Line(Path, Needle, seconds, num_char_back)
{
  Keep_Looking:

  ;FileGetTime, time, %Instek1_Log%, A  ;#last access time

  Sleep, %seconds%000

  ;##### Get the last characters of the file
  FileRead, File, %Path%  ;Load the entire file into the variable called "File"
  StringRight, last_chars, File, %num_char_back%  ;Store the "num_char_back" into "last_chars"
  StringReplace, last_chars, last_chars, `r`n, , All ;remove any newlines/carriage returns

  IfNotInString, last_chars, %Needle%  ;search for the magic word(s)
  {
    Goto, Keep_Looking
  }

  return
}


Regards,
aobrien
Back to top
View user's profile Send private message
aobrien



Joined: 14 Feb 2008
Posts: 70
Location: Santa Clara, CA

PostPosted: Tue Apr 20, 2010 9:29 pm    Post subject: Reply with quote

Hi All, I have updated the Serial (COM) Port Script to make it stdlib compatible as suggested by haichen. If for some reason someone should want the old non-stdlib version then send me a PM and I'll gladly send it to you.

Regards,
aobrien
Back to top
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 921
Location: Ploiesti, RO

PostPosted: Sat Sep 11, 2010 11:08 am    Post subject: Reply with quote

Hi, I'm interested in this script but haven't checked it out yet. Basically I'm posting mainly to get any further updates in this thread.

My only interest is a dual communication (Send/Receive) between an AHK application that I'm building and a Mitsubishi Alpha controller, using the RS232 interface. I'm new to it and haven't yet read the controller manual, but a simple yes/no/maybe answer to the question "could your script work as needed?" would suffice, for now. Thank you in advance.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
trenton_xavier



Joined: 16 Jun 2008
Posts: 82
Location: Pittsburgh, Pennsylvania, USA

PostPosted: Mon Sep 13, 2010 12:24 am    Post subject: Reply with quote

@Drugwash
I think it would. I don't know what communication protocol you're using, but I use a custom built version of this script everyday to communicate via Modbus to different controllers. Check out the specs and let us know what you're dealing with. Smile
Back to top
View user's profile Send private message AIM Address
Drugwash



Joined: 07 Sep 2008
Posts: 921
Location: Ploiesti, RO

PostPosted: Mon Sep 27, 2010 1:48 pm    Post subject: Reply with quote

The controller uses a simple RS232 protocol (8 data bits, no parity, 1 stop bit, DTR On, RTS On). I managed to build a custom script that communicates with an emulator - another AHK script that emulates the AL2 controller - but I stumbled into an odd issue: while trying to send a string of 105 bytes as a command, the emulator only receives 14 bytes, although the sender confirms all 105 bytes were sent. I'm not aware of any such limitation and couldn't find information on the web in this regard. Will keep digging but any help would be welcome. Thank you so far.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
aobrien



Joined: 14 Feb 2008
Posts: 70
Location: Santa Clara, CA

PostPosted: Fri Oct 01, 2010 4:54 pm    Post subject: Reply with quote

Hi Drugwash,

These AHK serial scripts will send all the bytes out in one shot. So if the receiver is not able to keep up then you will loose information.

I have never really used DTR/RTS. I always make sure that the receiver can handle the input. You would have to work some extra logic (DLL calls) in there to read the RTS bit every byte or something to make sure that it is OK to send data.

aobrien
Back to top
View user's profile Send private message
Drugwash



Joined: 07 Sep 2008
Posts: 921
Location: Ploiesti, RO

PostPosted: Tue Oct 12, 2010 11:04 am    Post subject: Reply with quote

By tinkering with the timeouts and file creation attributes I managed to get an almost OK pair of scripts. I say 'almost' because it still throws a few errors every now and then. My tests were performed between Win98SE and WinXP. I hoever failed to run either script under Win7; it just refuses to open the COM1 port. The port exists, is enabled, everything is OK in Device Manager.
I should mention that your script doesn't work either, under Win7. I've tried setting the AHK exe to run as administrator, UAC and other useless crap are all disabled - to no avail. If you find any help in this regard, please share the knowledge. I'll keep digging myself too.
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Michael@Oz



Joined: 08 Nov 2009
Posts: 233
Location: Canberra Oz

PostPosted: Thu Apr 21, 2011 12:45 am    Post subject: Reply with quote

Thanks aobrien, great script.

I needed one which returned errors rather than the MsgBox errors, so I have modified yours.

I also added;
RS232_Status:=RS232_GetCommModemStatus(RS232_FileHandle)
& RS232_EscapeCommFunction(RS232_FileHandle, nFunc)
which can get/set pins, see script for details.

I also changed the write function to take strings instead of the HEX values.

It's here with a couple of demo programs.

EDIT: bugs fixed

Perhaps you could consider a Script Variable such as RS232_ErrorMessages=true/false and add silent errors in your script?


Last edited by Michael@Oz on Sat Apr 30, 2011 6:43 am; edited 1 time in total
Back to top
View user's profile Send private message
Michael@Oz



Joined: 08 Nov 2009
Posts: 233
Location: Canberra Oz

PostPosted: Wed Apr 27, 2011 5:43 am    Post subject: Reply with quote

I used your (modified) COM code in my example programs to demo the Pololu Wixel wireless thing in my first Instructable. If you are interested in robotics or hobby electronics you may be interested.

I'll tidy up my changes to RS232 and edit the above post shorty.
Back to top
View user's profile Send private message
specter333



Joined: 15 Jan 2007
Posts: 527

PostPosted: Fri Apr 29, 2011 11:08 am    Post subject: Reply with quote

I've been trying to get this to work with my LG TV using the code from this post because it seems to have the codes I need already and converts them before sending to the com port,
http://www.autohotkey.com/forum/viewtopic.php?t=28703&postdays=0&postorder=asc&start=37

But so far no luck. I get communication using the LG app eZ-NetManager and the script no longer gives me error when starting after I finally got the com port issues worked out, but I don't have communication.

As far as I can tell the error comes at around line 357 " ;###### Read the data from the COM Port ######" . By the time it gets to 365
Code:
MsgBox, Read_Result=%Read_Result% `nBR=%Bytes_Received% ,`nData=%Data%
the Bytes_Received is 0 and the loop just below that,
Code:
Loop %Bytes_Received%
never runs.

I know this is a bad explanation and confusing to work through but maybe, just maybe someone will be able to spot what's wrong. Thanks for any help.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  Next
Page 7 of 9

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group