Jump to content
Sign in to follow this  
frank10

C code from autoit or hardware sensor calls from a program

Recommended Posts

frank10

I have some temperature sensors in a 1-wire system connected to the USB port of PC.

And I use some progs I found on the net.

But I want to make a customized program that reads the data from the sensors and then make a graph or other.

So, the first thing is how to interface with them.

There are two sdk for 1-wire network. Inside them you can find some code examples in delphi or C++.

For example, this:

Required on the command line is the 1-Wire port name:

example: "COM1"     (Win32 DS2480B)
    "/dev/cua0"     (Linux DS2480B)
    "1"     (Win32 TMEX)
    "\\.\DS2490-1"  (Win32 USB DS2490)
    "{1,5}"     (Win32 DS2480B multi build)
    "{1,6}"     (Win32 USB DS2490 multi build)
    "{1,2}"     (Win32 DS1410E multi build)

This application uses the 1-Wire Public Domain API. 
Implementations of this API can be found in the '\lib' folder.

// temp.c - Application to find and read the 1-Wire Net
//  DS1920/DS1820/DS18S20 - temperature measurement.
//
//  This application uses the files from the 'Public Domain'
//  1-Wire Net libraries ('general' and 'userial').
//
//
// Version: 2.00
//

#include <stdlib.h>
#include <stdio.h>
#include "ownet.h"
#include "temp10.h"
#include "findtype.h"

// defines
#define MAXDEVICES  20

// global serial numbers
uchar FamilySN[MAXDEVICES][8];

// variables
int family_code;

//----------------------------------------------------------------------
// Main Test for DS1920/DS1820 temperature measurement
//
int main(int argc, char **argv)
{
 float current_temp;
 int i = 0;
 int NumDevices=0;
 int portnum = 0;

 //----------------------------------------
 // Introduction header
 printf("\n/---------------------------------------------\n");
 printf(" Temperature application DS1920/DS1820 - Version 1.00 \n"
    " The following is a test to excersize a DS1920/DS1820.\n"
    " Temperature Find and Read from a: \n"
    " DS1920/DS1820 (at least 1)\n\n");

 printf(" Press any CTRL-C to stop this program.\n\n");
 printf(" Output [Serial Number(s) ........ Temp1(F)] \n\n");

 // check for required port name
 if (argc != 2)
 {
    printf("1-Wire Net name required on command line!\n"
    " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
    "(Linux DS2480),\"{1,5}\" (Win32 TMEX)\n");
    exit(1);
 }


 // attempt to acquire the 1-Wire Net
 if((portnum = owAcquireEx(argv[1])) < 0)
 {
    OWERROR_DUMP(stdout);
    exit(1);
 }

 // success
 printf("Port opened: %s\n",argv[1]);

 // Find the device(s)
 NumDevices = FindDevices(portnum, &FamilySN[0], 0x10, MAXDEVICES);
 if (NumDevices>0)
 {
    printf("\n");
    printf("Device(s) Found: \n");
    for (i = 0; i < NumDevices; i++)
    {
    PrintSerialNum(FamilySN[i]);
    printf("\n");
    }
    printf("\n\n");

    // (stops on CTRL-C)
    do
    {
    // read the temperature and print serial number and temperature
    for (i = 0; i < NumDevices; i++)
    {

    if (ReadTemperature(portnum, FamilySN[i],&current_temp))
    {
    PrintSerialNum(FamilySN[i]);
    printf("    %5.1f \n", current_temp * 9 / 5 + 32);
    // converting temperature from Celsius to Fahrenheit
    }
    else
    printf("    Error reading temperature, verify device present:%d\n",
    (int)owVerify(portnum, FALSE));
    }
    printf("\n");
    }
    while (!key_abort());
 }
 else
    printf("\n\n\nERROR, device DS1920/DS1820 not found!\n");

 // release the 1-Wire Net
 owRelease(portnum);
 printf("Closing port %s.\n", argv[1]);
 exit(0);

 return 0;
}

My question is:

how can you insert C code in a autoit prog? Or how to have access directly to the hardware calls?

Or how to make a call to this piece of code from autoit? And how to pass the variables?

Edited by frank10

Share this post


Link to post
Share on other sites
PsaltyDS

You can't use C or C++ libraries directly in AutoIt. What you need is a C++ or .NET program compiled that implements the library as a DLL or COM interface. Then AutoIt could use COM objects or DllCall() to make use of it.

You might Google around on it some more. Maybe somebody already has a COM implementation out there for use with VBScript, or some such. AutoIt would be able to use the same.

:blink:

Edit: .NET DLL can't be called by AutoIt.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
frank10

Well, but I read here that it isn't possible to read .Net Dll...

EDIT:

Maybe, you are referring to native dll, but you say it's possible to make a C prog that compile the dll to access it further.

Is it the same with other languages, such as Delphi or VBasic?

Edited by frank10

Share this post


Link to post
Share on other sites
PsaltyDS

You're right about a DLL compiled by .NET not working with AutoIT. Edited my post above.

It needs to be done in a language that can make a DLL AutoIt can call, and for which your desired library is available. Looks like C++ is the choice.

:blink:

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
frank10

It needs to be done in a language that can make a DLL AutoIt can call, and for which your desired library is available. Looks like C++ is the choice.

So, I found a dll with the functions I need.

The initial two are:

long far pascal TMExtendedStartSession(

short PortNum, // Port number to get a session to

short PortType, // Port type number to get a session to

void far *EnhancedOptions // A pointer for enhanced options (an optional parameter)

);

and

short far pascal TMSetup(

long session_handle // session handle for the desired 1-Wire network

);

From C prog it works well: for example calling this works:

short PortType, PortNum;

void main(short argc, char **argv)
{
 long session_handle; 
 long session_handle1; 
 PortNum = 1;
 PortType = 6;

for (;;)
 {
    /* attempt to get a session */
    session_handle = TMExtendedStartSession(PortNum,PortType,NULL);
    printf("handle %d...\n\n",session_handle);

 if (session_handle > 0) 
    {
    /* setup the 1-Wire network */
 session_handle1 = TMSetup(session_handle);
    if (session_handle1 == 1)
    {
    printf("Searching for a DS1920/DS1820... %d \n\n", session_handle1);

 }
 }
 return;
    }

The session_handle gives 353 and the session_handle1 = 1 and it's ok.

From Autoit, I made this:

Global $session_handle, $temp 
Global $PortNum = 1, $PortType = 6 
ConsoleWrite("START" & @CRLF & @CRLF) 

$session_handle = DllCall("S:\___1-Wire\SDK\Lib\TMEX\IBUSB32.dll", "long", "TMExtendedStartSession","short",$PortNum,"short",$PortType,"short","NULL") 

ConsoleWrite("@error=" & @error & " Sess-handle= " & $session_handle[0] & @CRLF) 

$temp = DllCall("S:\___1-Wire\SDK\Lib\TMEX\IBUSB32.dll", "short", "TMSetup","long",$session_handle[0] ) 

ConsoleWrite("@error=" & @error & " Setup= " & $temp[0] & @CRLF) 

ConsoleWrite(@CRLF & @CRLF & "END") 
Exit

It gives session_handle = 353 , but temp is -200 that is:

(-200) INVALID_SESSION - session not valid..

What's wrong with my autoit code?

Maybe the type of NULL passed in the first func?

Share this post


Link to post
Share on other sites
frank10

I found also these definitions of types in that funcs:

far pascal far function using pascal calling convention

void far * generic far pointer

I think Pascal convention has to do with cdecl and stdcall.

So I tried :cdecl in the DllCall.

But I tried and the prog doesn't work at all: it gives me no output in the console and exits.

I tried also int or long in the DllCall of TMSetup, but it returns 65336 (?) instead of -200 (correct error code)...

Anyway I don't get it to work in Autoit.

Maybe the lib I used for the linker in GCC in the C prog is a little different respect the dll? But I don't think so... the func names are the same.

Has anyone else found some dll that doesn't work well in Autoit (apart the dll made C.NET)?

Share this post


Link to post
Share on other sites
frank10

I found some explanations of invalid sessions:

The session handle is good indefinitely if no other application attempts to start a new session on that 1-Wire network port. The session handle can become invalid under the following circumstances:

  • the session handle has not been used for at least 1 second And

    a request for the same 1-Wire network has been made

    And

    Session handle expiration is not disabled

  • the handle has been used in the last second And

    the handle is over 35 seconds old

    And

    a request for the same 1-Wire network has been made

    And

    Session handle expiration is not disabled

Maybe I'm on case 1, because DllCall takes more than a second?

So, I tryed to change the last parameter as:

This parameter is optional. If not needed, then pass a NULL pointer. EnhancedOptions is a pointer to a long (32 byte signed integer) with the following bit field options:

SESSION_INFINITE 0x0001 session does not expire

SESSION_RSRC_RELEASE 0x0002 port resources are released at each session

SESSION_NO_FORCE_REG_SPD 0x0004 regular speed is not forced on each session

So,

$session_handle = DllCall("S:\___1-Wire\SDK\Lib\TMEX\IBUSB32.dll", "long", "TMExtendedStartSession","short",$PortNum,"short",$PortType,"long*","0x0001")

But it changes nothing.

How can it be different respect this in C:

session_handle = TMExtendedStartSession(PortNum,PortType,NULL);

And the second call:

DllCall("S:\___1-Wire\SDK\Lib\TMEX\IBUSB32.dll", "short", "TMSetup","long", $session_handle[0] )

from C:

session_handle1 = TMSetup(session_handle);

Any ideas?

Is this an Autoit Dllcall Bug?

Share this post


Link to post
Share on other sites
PsaltyDS

So, I tryed to change the last parameter as:

This parameter is optional. If not needed, then pass a NULL pointer. EnhancedOptions is a pointer to a long (32 byte signed integer) with the following bit field options:

SESSION_INFINITE 0x0001 session does not expire

SESSION_RSRC_RELEASE 0x0002 port resources are released at each session

SESSION_NO_FORCE_REG_SPD 0x0004 regular speed is not forced on each session

So,

$session_handle = DllCall("S:\___1-Wire\SDK\Lib\TMEX\IBUSB32.dll", "long", "TMExtendedStartSession","short",$PortNum,"short",$PortType,"long*","0x0001")

But it changes nothing.

This is a string: "0x0001"

This is an integer: 0x0001

In many cases AutoIt will translate a string representation of an integer into the required type (internally, as if by Int(), for example). But that may not apply to DllCall(). Play it safe and use: ..., "long*", 0x0001) or just: ..., "long*", 1)

:blink:


Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
frank10

Thank you for the suggestion, but it failed too.

I don't know how to try at this point.

I solved anyway the problem calling from Autoit the C program and reading the stdout to pass the values.

But it's not so clean... and anyway programming in C is worse than Autoit for string, fast testing without compile, and to have all in one prog.

Edited by frank10

Share this post


Link to post
Share on other sites
AndyG

OT, but could be useful....

You're right about a DLL compiled by .NET not working with AutoIT. Edited my post above.

If you "recompile" a .NET-dll (tool is here) it is possible to use VB.NET and C#-dll´s with AutoIt.

.Net-Framework/Visual Studio (Express) is required. 

Edited by AndyG

Share this post


Link to post
Share on other sites
frank10

OT, but could be useful....

If you "recompile" a .NET-dll (tool is here) it is possible to use VB.NET and C#-dll´s with AutoIt.

.Net-Framework/Visual Studio (Express) is required.

Thank you, but link is wrong. It says Fatal error.

Share this post


Link to post
Share on other sites
frank10

Thank you, but link is wrong. It says Fatal error.

I think there is some problem with the entire site www.autoit.de, because also the root gives Fatal error.

Maybe the server is down, but your link could be good.

Edited by frank10

Share this post


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
Sign in to follow this  

×