Jump to content

Recommended Posts

Hi everyone,

This is my special pet project.

It is an example of how u can use autoit to control external devices.

this script is made to comunicate with the MAX335 chip  using the SPI protocol via the LPT(printer) port

the beauty of the MAX335 chip is that the Clock, Data_In and the Chip_Select pins can be directly connected to the LPT port without any external components, and the 12V and 5V directly from an ATX PC power suply.

aditionaly i made a custom GUI with CommandFusion instaled on an Android Tablet that sends TCP commands to an Autoit TCP server that controls three dasy chained MAX335 chips that totals 24 independent NO switches

this script works perfectly for me

i just finished this project and i think i will make an UDF including more SPI devices

$DLLFileAndPath = @ScriptDir & "/inpout32.dll"

Global $335_device_number=3 ;number of daisy chained chips
Global $335_clock_bit=0 ;bit number for LPT pin 1 in the control Register of the LPT port(where i connected the CLK pin on te MAX335)
Global $335_cs_bit=4;bit number for LPT pin 6 in the data Register of the LPT port(where i connected the CS pin on te MAX335)
Global $335_data_in_bit=7;bit number for LPT pin 9 in the data Register of the LPT port(where i connected the DI pin on te MAX335)
Global $clock_delay=1;this limits the clock speed but it works fine with 0


;the ini file will be created and will keep the MAX335 pins statuses
For $i=0 To $335_device_number*8-1 Step 1
IniWrite("355_buffer.ini","present_data",$i,"0")
Next

set_max335_output(2,3,1) ; this will activate switch 2 on device 3 on the daisy chain

Func set_max335_output($output_no,$device_no,$status)
    $bit_no=($device_no-1)*8+$output_no-1 ; get the exact bit number of the switch
    IniWrite("355_buffer.ini","present_data",$bit_no,$status); whrite it to the buffer
;this part is where the SPI protocol begins
            set_control_bit($335_clock_bit,1); drop CLK
            set_data_bit($335_cs_bit,0) ;activate CS
            Sleep($clock_delay)
        For $i=$335_device_number*8-1 To 0 Step -1; start writing from buffer MostSignificantByte first
            $data=IniRead("355_buffer.ini","present_data",$i,"0")
            set_data_bit($335_data_in_bit,$data) ; set data bit value
            set_control_bit($335_clock_bit,0) ;raise CLK
            Sleep($clock_delay)
            set_control_bit($335_clock_bit,1); drop CLK
            Sleep($clock_delay)
        Next
            set_data_bit($335_cs_bit,1);deactivate CS
EndFunc



Func set_data_bit($bit,$stat=-1) ; it will write the value of the bit in the data reg of the LPT port
    $y= DllCall($DLLFileAndPath, "int", "Inp32", "int", "0x378")
    $bits=dec_to_bin($y[0])
    If $stat = -1 Then 
        If $bits[$bit]=0 Then
            $bits[$bit]=1
        Else
            $bits[$bit]=0
        EndIf
            $bcd=bin_to_dec($bits)
            DllCall( $DLLFileAndPath, "int", "Out32", "int", "0x378", "int", $bcd)
            Return 1
    Else
        If $bits[$bit]<>$stat  Then
            $bits[$bit]=$stat
            $bcd=bin_to_dec($bits)
            DllCall( $DLLFileAndPath, "int", "Out32", "int", "0x378", "int", $bcd)
            Return 1
        Else
            Return 2
        EndIf
    EndIf
EndFunc

Func set_control_bit($bit,$stat=-1); it will write the value of the bit in the control reg of the LPT port
    $y= DllCall($DLLFileAndPath, "int", "Inp32", "int", "0x37a")
    $bits=dec_to_bin($y[0])
    If $stat = -1 Then
        If $bits[$bit]=0 Then
            $bits[$bit]=1
        Else
            $bits[$bit]=0
        EndIf
            $bcd=bin_to_dec($bits)
            DllCall( $DLLFileAndPath, "int", "Out32", "int", "0x37a", "int", $bcd)
            Return 1
    Else
        If $bits[$bit]<>$stat  Then
            $bits[$bit]=$stat
            $bcd=bin_to_dec($bits)
            DllCall( $DLLFileAndPath, "int", "Out32", "int", "0x37a", "int", $bcd)
            Return 1
        Else
            Return 2
        EndIf
    EndIf
EndFunc

Func dec_to_bin($dec)
    Local $bit_array[8]
    If $dec > 255 Then SetError(1,1,-1)
    If $dec < 0 Then SetError(2,1,-1)
    For $i=7 To 0 Step -1
        If $dec >= 2^$i Then
            $bit_array[$i] = 1
            $dec=$dec-2^$i
        Else
            $bit_array[$i] = 0
        EndIf
    Next
    Return $bit_array
EndFunc

Func bin_to_dec($bit_array)
    If IsArray($bit_array) Then
        If UBound($bit_array) = 8 Then
            $dec=0
            For $i=7 To 0 Step -1
                $dec = $bit_array[$i]*(2^$i)+$dec
            Next
        Else
            SetError(2,1,-1)
        EndIf
    Else
        SetError(1,1,-1)
    EndIf
    Return $dec
EndFunc
Edited by Blinky
Link to post
Share on other sites
  • 2 weeks later...
  • 5 months later...

thanks for this! looks like the most can be run in my program. using send bits is the important things in the real works.so we can build more auto machines!

but still hope autoit have its port api function groups may be can run it quickly.just like the LABVIEW. :zorro: 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Irios
      Below is a function w/ a working example showing how to access the AIDA64 shared memory.
      First enable shared memory here: Preferences > Hardware Monitoring > External Applications > Enable Shared Memory
      The length of the data depends on the number of active sensors and their content. As AIDA64 does not provide a buffer size value, we use _WinAPI_GetSystemInfo() <WinAPISys.au3> to get dwPageSize ($aArray[1]). This value is the used as an address offset, and we continue reading chunks of data until we encounter a 0x00 NUL character.
      Based on the Delphi example found in the AIDA64 documentation: https://www.aida64.co.uk/user-manual/external-applications
      Feedback appreciated, especially as all this shared memory stuff is not my ordinary cup of... cake. And regarding this whole ordeal of reading data without knowing the exact length; I'm suspecting my solution to read chunks of data like done below is not by the book, and I'm concerned what might happen if the final chunk is exactly 4096 bytes in length. Will there still be a NUL terminator there? Hmm.
       
      #NoTrayIcon #include <WinAPIFiles.au3> #include <WinAPISys.au3> ; #INDEX# =========================================================================================================================== ; Title .........: AIDA64 Shared Memory access for AutoIt3 ; Author(s) .....: demux4555 ; Reference .....: https://www.aida64.co.uk/user-manual/external-applications ; =================================================================================================================================== Global $vSharedmem_data ; The variable used to store the data we read from the shared memory Global $return = ExtApp_SharedMem_ReadBuffer_v2($vSharedmem_data) ; Now, let's see what happens when we run the function... If @error Then _Echo("! ExtApp_SharedMem_ReadBuffer_v2(): @errror = " & @error) If IsBinary($vSharedmem_data) Then ; Convert type Binary to actual human readable text. We also remove the excess of trailing 0x00 characters. $vSharedmem_data = StringStripWS(BinaryToString($vSharedmem_data), $STR_STRIPLEADING+$STR_STRIPTRAILING) EndIf _Echo() _Echo("> return = " & $return) ; The return value. Will be True if everything went ok. _Echo("> length = " & StringLen($vSharedmem_data)) ; The number of characters read from shared memory. _Echo("> data = " & $vSharedmem_data) ; The actual data. _Echo("> data(40) = " & "..." & StringRight($vSharedmem_data, 40)) ; shows the 40 right-most characters, and _should_ show the very end of the data at this point. _Echo() Exit ; #FUNCTION# ==================================================================================================================== ; Name ..........: ExtApp_SharedMem_ReadBuffer_v2 ; Description ...: AIDA64 Shared Memory Example for AutoIt3 ; Syntax ........: ExtApp_SharedMem_ReadBuffer_v2(Byref $_dOutput[, $_sSharedmemName = "AIDA64_SensorValues"]) ; Parameters ....: $_dOutput - [in/out] Variable to store the read data. ; $_sSharedmemName - [optional] Name of the AIDA64 shared memory. Default is "AIDA64_SensorValues". ; Return values .: Success: True. $_dOutput will be type Binary, containing a string of the XML values of the active sensors. ; Failure: False. ; Author ........: demux4555 ; Reference .....: https://www.aida64.co.uk/user-manual/external-applications ; =============================================================================================================================== Func ExtApp_SharedMem_ReadBuffer_v2(ByRef $_dOutput, $_sSharedmemName = "AIDA64_SensorValues") Local $_bReturn = False Local $_aGSI = _WinAPI_GetSystemInfo() ; Retrieves information about the current system... Ref: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo Local $_iPageSize = $_aGSI[1] ; ... the page size and the granularity of page protection and commitment. Usually it is 4096, but we read it anyway. Ref: https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/ns-sysinfoapi-system_info Local $_hMapping = _WinAPI_OpenFileMapping($_sSharedmemName, $FILE_MAP_READ) ; Opens a named file mapping object. Ref: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-openfilemappingw If Not IsPtr($_hMapping) Then Return SetError(-2, 0, $_bReturn) Local $_pMappedData = _WinAPI_MapViewOfFile($_hMapping, 0, 0, $FILE_MAP_READ) ; Pointer to the start address. Maps a view of a file mapping into the address space of a calling process. Ref: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile If @error Or Not IsPtr($_pMappedData) Then Return SetError(-2, 0, $_bReturn) ; Now we loop until we reach the end of the data. Local $_tData, $_dBuffer While 1 $_tData = DllStructCreate("BYTE[" & $_iPageSize & "]", $_pMappedData) ; Note: we use type BYTE[] (AutoIt type Binary) instead of CHAR[] (AutoIt type String). This allows us to look for value 0x00 (NUL termination of the data). If @error Then ExitLoop $_dBuffer = DllStructGetData($_tData, 1) ; The returned value is type Binary. If @error Or ($_dBuffer==0) Or (BinaryLen($_dBuffer)=0) Then ExitLoop ; Pretty sure $_dBuffer==0 can not happen, so just in case. $_dOutput = Binary($_dOutput & $_dBuffer) ; Add the read data to the end of the output variable. If StringRight($_dBuffer, 2)=="00" Then ExitLoop ; Look for NUL termination of the string data. $_pMappedData += $_iPageSize ; We change the address by using the page granularity value (i.e. 4096) as the offset. WEnd ; Quick cleanup $_bReturn = _WinAPI_UnmapViewOfFile($_pMappedData)=1 ; Unmaps a mapped view of a file from the calling process's address space. Ref: https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-unmapviewoffile _WinAPI_CloseHandle($_hMapping) Return $_bReturn EndFunc; Func _Echo($_data = "") ConsoleWrite($_data & @CRLF) EndFunc  
       
    • By tarretarretarre
      Version 2.x.x and 3.x.x has been moved to branch 3.x
      About Autoit-Socket-IO
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!
      Key features
      Simple API 99% data-type serialization thanks to Autoit-Serialize Can easily be extended with your own functionality thanks to Autoit-Events "Educational" examples Data encryption thanks to _<Crypt.au3> Limitations
      Speed. This UDF will sacrifice some speed for convenience Getting started
      Download the script from AutoIt or pull it from the official github repo git@github.com:tarreislam/Autoit-Socket-IO.git and checkout the tag 4.0.0-beta Check out the documentaion Take a look in the examples/ folder Changelog
      To see changes from 3.x.x and 2.x.x please checkout the 3.x branch
      Version 4.0.0-beta (This update break scripts.)
      Code base fully rewritten with Autoit-Events and decoupled to improve code quality and reduce bloat. The new UDF is very different from 3.x.x so please checkout the UPGRADE guide to fully understand all changes Added new documentation documentaion Success stories
      Since December 2017-now I have used version 1.5.0 in an production environment for 150+ clients with great success, the only downtime is planned windows updates and power outages.
       
      Newest version (2020-09-15!)

       
      Older versions (Not supported anymore)
      Autoit-Socket-IO-1.0.0.zip Autoit-Socket-IO-1.1.0.zip Autoit-Socket-IO-1.3.0.zip Autoit-Socket-IO-1.4.0.zip Autoit-Socket-IO-1.5.0.zip
      Autoit-Socket-IO-2.0.0.zip
    • By tarretarretarre
      Version 2.x.x and 3.x.x has been moved to branch 3.x
      About Autoit-Socket-IO
      Autoit-Socket-IO is a event driven TCP/IP wrapper heavily inspired from Socket.IO with focus on user friendliness and long term sustainability.
      I constantly want to make this UDF faster and better, so if you have any suggestions or questions (beginner and advanced) Do not hesitate to ask them, I will gladly help!
      Key features
      Simple API 99% data-type serialization thanks to Autoit-Serialize Can easily be extended with your own functionality thanks to Autoit-Events "Educational" examples Data encryption thanks to _<Crypt.au3> Limitations
      Speed. This UDF will sacrifice some speed for convenience Read more in the official thread
    • By mmoalem
      Hi there - creating a simple script to triger cctv software to turn on when my phone is not connected to home wifi. But having issues with my phone (samsung s7) not answering ping when sleeping (despite wifi set to work when sleep) - not sure the issue... and ideas? 
      Alternatively was wondering about pinging the bluetooth address of the phone - anyway to get this working in autoit?
    • By TheXman
      Purpose (from Microsoft's website)
      The HTTP Server API enables applications to communicate over HTTP without using Microsoft Internet Information Server (IIS). Applications can register to receive HTTP requests for particular URLs, receive HTTP requests, and send HTTP responses. The HTTP Server API includes SSL support so that applications can exchange data over secure HTTP connections without IIS.
      Description
      There have been several times in the past that I wanted to either retrieve information from one of my PCs or execute commands on one of my PCs, whether it was from across the world or sitting on my couch.  Since AutoIt is one of my favorite tools for automating just about anything on my PC, I looked for ways to make to make it happen.  Setting up a full blown IIS server seemed like overkill so I looked for lighter weight solutions.  I though about creating my own AutoIt UDP or TCP server but that just wasn't robust enough,  Then I found Microsoft's HTTP Server API and it looked very promising.  After doing a little research into the APIs, I found that it was flexible & robust enough to handle just about any of the tasks that I required now and in the future.  So a while back I decided to wrap the API functionality that I needed into an AutoIt UDF file to allow me to easily create the functionality I needed at the time.  It has come in very handy over the years.  Of course it wasn't all wrapped up with a nice little bow like it is now.  That only happened when I decided to share it with anyone else who could use it or learn from it.
      The example file that I included is a very granular example of the steps required to get a lightweight HTTP Server up and listening for GET requests.  The UDF is a wrapper for the Microsoft APIs.  That means to do anything over and above what I show in the example, one would probably have to have at least a general knowledge of APIs or the ability to figure out which APIs/functions to use, what structures and data is needed to be passed to them, and in what order.  However, the UDF gives a very solid foundation on which to build upon.  Of course, if anyone has questions about the UDF or how to implement any particular functionality, I would probably help to the extent that I could or point you in the right direction so that you can figure out how to implement your own solution.
      The APIs included in the UDF are the ones that I needed in the past to do what I needed to do.  If any additional APIs need to be added to the UDF file, please make those suggestions in the related forum topic.
      Being that this is basically an AutoIt wrapper for the Microsoft API functions, there's no need to create AutoIt-specific documentation.  All of the UDF functions, structures, constants, and enumerations are named after their Microsoft API counterparts.  Therefore, you can refer to Microsoft's extensive documentation of their HTTP Server API.  As stated earlier, if there is one or more APIs that you find yourself needing for your particular solution, please suggest it in the related Example Scripts forum topic.
      Related Links
      Microsoft HTTP Server API - Start Page
      Microsoft HTTP Server API - API v2 Reference
      Microsoft HTTP Server API - Programming Model
×
×
  • Create New...