 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
evandevon
Joined: 22 Apr 2008 Posts: 18
|
Posted: Sun Jun 29, 2008 4:19 pm Post subject: yet another serial com port help request |
|
|
Hi,
Firstly, before anyone says "search the forum", I have so please be nice.
Secondly, if you've read this far, then thank you.
My problem:
I have this sort of keyboard thing that is made up of 16x8 touch switches (just a grid of switches) that connects to the serial port. Using the program "realterm" I found out which buttons sends particular data. All I want is something that captures the data being sent (preferably in hex). I've already got a script which I can then use to control the computer via the 16x8 keyboard. I've NEVER used the com port before now and know very little com port terminology so I don't really know what to look for even if it is in these forums.
These are the posts that have been the most helpful
http://www.autohotkey.com/forum/topic28703.html&highlight=com+port
http://www.autohotkey.com/forum/topic28873.html&highlight=com+port
I've used the script from this post
http://www.autohotkey.com/forum/topic26533.html&highlight=com+port
And can get a gui that I think sends data to the device, which I don't need (but I suspect it can do more). I only need to read the data being sent from the device and store it as a variable or a text file, or anything that I can work with!
For example the end result could be something like...
com1_variable = get_latest_com1_port_data_in_hex
if com1_variable == 06E0
then do whatever I want
if com1variable == 18E0
then do something else
I've thought about 'capturing' the text displayed in realterm but I need a script that will work on its own (as much as possible).
Any advice is very much appreciated and important as this is a project to use an old (I think 1990) programmable keyboard that kids with disabilities can use to access the computer and I am struggling with getting anywhere with it.
Thanks, Evs
EDIT:
I've just used comcap as advised in a different post and it says there is no data being sent to the pc but using realterm shows that data IS
http://www.autohotkey.com/forum/topic10664.html&highlight=serial _________________ Inventing problems that need solutions... |
|
| Back to top |
|
 |
evandevon
Joined: 22 Apr 2008 Posts: 18
|
Posted: Sun Jun 29, 2008 7:15 pm Post subject: Non-working solution |
|
|
This post is too big to be an edit...
Well after spending 3hours not sleeping I can get some text into an open notepad file.
Using the advice on
http://www.autohotkey.com/forum/topic28703.html&highlight=com+port
I've stripped down the script so that only receive data works and changed some settings and ended up with this (THANK YOU AOBRIEN!)
| Code: |
#SingleInstance Force
SetTitleMatchMode, 2
;========================================================================
;====== User Variables ==================================================
;========================================================================
COM_Port = COM1
COM_Baud = 9600
COM_Parity = N
COM_Data = 8
COM_Stop = 1
Console_Path = C:
;========================================================================
;====== Script Variables ================================================
;========================================================================
COM_Settings = %COM_Port%:%COM_Baud%,%COM_Parity%,%COM_Data%,%COM_Stop%
Console_File_Name= %COM_Port%COM_Port.txt
Console_Title = %Console_File_Name% - Notepad
;========================================================================
;====== Notepad Console Check ===========================================
;========================================================================
;Check for console, if there isn't already one, then open it.
IfWinNotExist, %Console_Title%
{
Run, Notepad
WinWait, Untitled - Notepad
Send, !fa ;file save as
WinWait, Save As
Clipboard = %Console_Path%\%Console_File_Name%
Send, ^v{ENTER}
}
;========================================================================
;====== Serial Port Receive =============================================
;========================================================================
;Quit_var is used to exit the COM port receive loop
Quit_var = 0
hModule := DllCall("LoadLibrary", "str", "Port.dll")
result := DllCall("Port.dll\OPENCOM", "str", COM_Settings)
if result = 0
{
MsgBox, AHK could not open %COM_Port%
return
}
MsgBox, AHK is now connected to %COM_Port%
SetFormat, integer, data
;COM port receive loop
data = 0
Loop
{
data := DllCall("Port.dll\READBYTE")
;This will send every byte that is received on
; the COM port to the Notepad Console
If data <> -1
{
Critical, On
WinWait, %Console_Title%,
IfWinNotActive, %Console_Title%, , WinActivate, %Console_Title%,
WinWaitActive, %Console_Title%,
;If you want to see the decimal data (as received by the serial port,
; then uncomment the line below.
Send, ^{END}data=%data%{ENTER}
;ASCII := chr(data)
Send, ^{END}%ASCII%
Critical, Off
}
if Quit_var = 1
Break
}
result := DllCall("c:\temp\Port.dll\CLOSECOM")
DllCall("FreeLibrary", "UInt", hModule)
MsgBox, AHK is now disconnected from %COM_Port%
return
;========================================================================
;====== Save & Reload and Minimize AHK Script ===========================
;========================================================================
^!w::
Send, ^s
Sleep, 200
Reload
Sleep, 1000
return
;========================================================================
;====== Exit Console Receive Loop =======================================
;========================================================================
^F1::
Quit_var = 1
return
|
(I've changed some variables in both these examples - note the 0->1 change under
";Set to 1 if you want to see the decimal data as received by the serial port."
So that the data comes in as follows...)
data=0
data=24
data=134
data=24
data=134
which I can work with BUT i still need to save the port.dll file to the system 32 folder which I don't think I'm able to do on any and every computer I want this keyboard to work on (Admin problems?).
The next script on that page which doesn't need port.dll doesn't seem to work for me. If I cut it down the same way I get
| Code: |
;########################################################################
MsgBox, Begin COM Test
#SingleInstance Force
SetTitleMatchMode, 2
;########################################################################
;###### User Variables ##################################################
;########################################################################
COM_Port = COM1
COM_Baud = 9600
COM_Parity = N
COM_Data = 8
COM_Stop = 1
Console_Path = C:
;########################################################################
;###### Script Variables ################################################
;########################################################################
COM_Settings = %COM_Port%:baud=%COM_Baud% parity=%COM_Parity% data=%COM_Data% stop=%COM_Stop% dtr=Off
Console_File_Name= %COM_Port%_Console.txt
Console_Title = %Console_File_Name% - Notepad
;########################################################################
;###### Notepad Console Check ###########################################
;########################################################################
;Check for console, if there isn't already one, then open it.
IfWinNotExist, %Console_Title%
{
Run, Notepad
WinWait, Untitled - Notepad
Send, !fa ;file save as
WinWait, Save As
Clipboard = %Console_Path%\%Console_File_Name%
Send, ^v{ENTER}
}
;########################################################################
;###### Serial Port Receive #############################################
;########################################################################
;Quit_var is used to exit the COM port receive loop
; 0=Don't Exit; 1=Exit; CTRL-F1 to set to 1 and exit script.
Quit_var = 0
Initialize_COM(COM_Settings)
;COM port receive loop
Loop
{
Read_Data := Read_from_COM("0xFF")
;msgbox, Bytes_Received=%Bytes_Received% ;Global variable that is set by Read_from_COM()
;0xFF in the above line basically sets the size of the read buffer.
; Use StringLeft to collect only the data received by the COM port.
; Bytes_Received is a global variable that is set during the Read_from_COM() routine.
;StringLeft, Read_Data, Read_Data, Bytes_Received ;5/22/08 This line was moved to Read_from_COM() subroutine.
;Process the data, if there is any.
If (Bytes_Received > 0)
{
;msgbox, Read_Data=%Read_Data%
;Prevent interruption during execution of this loop.
Critical, On
;If care is taken, you can comment out these WinActive lines for performance gains.
IfWinNotActive, %Console_Title%, , WinActivate, %Console_Title%,
WinWaitActive, %Console_Title%,
;Set to 1 if you want to see the decimal data as received by the serial port.
IF (1)
{
SetFormat, Integer, HEX
Binary_Data := NumGet(Read_Data, 0, "UInt")
SetFormat, Integer, DEC
Send, ^{END}data=%Binary_Data%{ENTER}
}
;Send the data that was received by the COM port-ASCII format
;Send, ^{END}%Read_Data%
Critical, Off
}
;CTRL-F1 sets Quit_var=1
if Quit_var = 1
Break
}
Close_COM(COM_FileHandle)
MsgBox, AHK is now disconnected from %COM_Port%
;ExitApp ;Exit Script
Return
;########################################################################
;###### Initialize COM Subroutine #######################################
;########################################################################
Initialize_COM(COM_Settings)
{
Global COM_FileHandle
;###### Build COM DCB ######
;Creates the structure that contains the COM Port number, baud rate,...
VarSetCapacity(DCB, 28)
BCD_Result := DllCall("BuildCommDCB"
,"str" , COM_Settings ;lpDef
,"UInt", &DCB) ;lpDCB
If (BCD_Result <> 1)
{
MsgBox, There is a problem with Serial Port communication. `nFailed Dll BuildCommDCB, BCD_Result=%BCD_Result% `nThe
Script Will Now Exit.
Exit
}
;###### Create COM File ######
;Creates the COM Port File Handle
StringLeft, COM_Port, COM_Settings, 4
COM_FileHandle := DllCall("CreateFile"
,"Str" , COM_Port ;File Name
,"UInt", 0xC0000000 ;Desired Access
,"UInt", 3 ;Safe Mode
,"UInt", 0 ;Security Attributes
,"UInt", 3 ;Creation Disposition
,"UInt", 0 ;Flags And Attributes
,"UInt", 0 ;Template File
,"Cdecl Int")
If (COM_FileHandle < 1)
{
MsgBox, There is a problem with Serial Port communication. `nFailed Dll CreateFile, COM_FileHandle=%COM_FileHandle%
`nThe Script Will Now Exit.
Exit
}
;###### Set COM State ######
;Sets the COM Port number, baud rate,...
SCS_Result := DllCall("SetCommState"
,"UInt", COM_FileHandle ;File Handle
,"UInt", &DCB) ;Pointer to DCB structure
If (SCS_Result <> 1)
{
MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCS_Result=%SCS_Result% `nThe
Script Will Now Exit.
Close_COM(COM_FileHandle)
Exit
}
;###### Create the SetCommTimeouts Structure ######
ReadIntervalTimeout = 0xffffffff
ReadTotalTimeoutMultiplier = 0x00000000
ReadTotalTimeoutConstant = 0x00000000
WriteTotalTimeoutMultiplier= 0x00000000
WriteTotalTimeoutConstant = 0x00000000
VarSetCapacity(Data, 20, 0) ; 5 * sizeof(DWORD)
NumPut(ReadIntervalTimeout, Data, 0, "UInt")
NumPut(ReadTotalTimeoutMultiplier, Data, 4, "UInt")
NumPut(ReadTotalTimeoutConstant, Data, 8, "UInt")
NumPut(WriteTotalTimeoutMultiplier, Data, 12, "UInt")
NumPut(WriteTotalTimeoutConstant, Data, 16, "UInt")
;###### Set the COM Timeouts ######
SCT_result := DllCall("SetCommTimeouts"
,"UInt", COM_FileHandle ;File Handle
,"UInt", &Data) ;Pointer to the data structure
If (SCT_result <> 1)
{
MsgBox, There is a problem with Serial Port communication. `nFailed Dll SetCommState, SCT_result=%SCT_result% `nThe
Script Will Now Exit.
Close_COM(COM_FileHandle)
Exit
}
Return %COM_FileHandle%
}
;########################################################################
;###### Close COM Subroutine ############################################
;########################################################################
Close_COM(COM_FileHandle)
{
;###### Close the COM File ######
CH_result := DllCall("CloseHandle", "UInt", COM_FileHandle)
If (CH_result <> 1)
MsgBox, Failed Dll CloseHandle CH_result=%CH_result%
Return
}
;########################################################################
;###### Read from COM Subroutines #######################################
;########################################################################
Read_from_COM(Num_Bytes)
{
Global COM_FileHandle
Global COM_Port
Global Bytes_Received
SetFormat, Integer, HEX
;Set the Data buffer size, prefill with 0x55 = ASCII character "U"
;VarSetCapacity won't assign anything less than 3 bytes. Meaning: If you
; tell it you want 1 or 2 byte size variable it will give you 3.
Data_Length := VarSetCapacity(Data, Num_Bytes, 0x55)
;msgbox, Data_Length=%Data_Length%
;###### Read the data from the COM Port ######
;msgbox, COM_FileHandle=%COM_FileHandle% `nNum_Bytes=%Num_Bytes%
Read_Result := DllCall("ReadFile"
,"UInt" , COM_FileHandle ; hFile
,"Str" , Data ; lpBuffer
,"Int" , Num_Bytes ; nNumberOfBytesToRead
,"UInt*", Bytes_Received ; lpNumberOfBytesReceived
,"Int" , 0) ; lpOverlapped
;MsgBox, Read_Result=%Read_Result% `nBR=%Bytes_Received% ,`nData=%Data%
If (Read_Result <> 1)
{
MsgBox, There is a problem with Serial Port communication. `nFailed Dll ReadFile on %COM_Port%, result=%Read_Result%
- The Script Will Now Exit.
Close_COM(COM_FileHandle)
Exit
}
SetFormat, Integer, DEC
;###### Format the received data ######
;Return only the bytes that were received.
If (Bytes_Received = 0)
Return ""
Else
;StringTrimRight, Data, Data, %Bytes_Received% ;5/22/08 This line is essentially useless and could potentially show
up as a bug.
StringLeft, Data, Data, Bytes_Received ;5/22/08 This is what the above line should have been
Return Data
}
;########################################################################
;###### Exit Console Receive Loop #######################################
;########################################################################
^F1::
Quit_var = 1
return
|
But I get no data being received at all - it opens the text file ready to receive but it never prints anything. The keyboard doesn't beep either when I press the button like it does in the previous example so something is definitely wrong.
EDIT = I think this is an error because the first data value is null?
My next option is to use serialterm (no install needed) and CMDret scripts (do a forum search) to do it.
I need sleep desperately so I'm going to post a link to this in the same post where I got the scripts from and see if that helps - then pass out.
Thanks, Evs. _________________ Inventing problems that need solutions... |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|