Jump to content
therealhanuta

CommAPI - Serial and parallel communication with Windows API

Recommended Posts

Hello,

I'm trying to communicate with a Agilent E3649A DC Power Supply. I have written some code using the CommAPI and so far I am only able to send commands, I cannot receive anything.  When I use the application that comes with the power supply everything is working as it should so that rules out the cable. Can someone look over my test code? I may have missed something...

#include <CommInterface.au3>
#include <MsgBoxConstants.au3>


Local Const $iPort = 1
Local Const $iBaud = 9600
Local Const $iParity = 0
Local Const $iByteSize = 8
Local Const $iStopBits = 2

Local $hFile =_CommAPI_OpenCOMPort($iPort, $iBaud, $iParity, $iByteSize, $iStopBits)
_CommAPI_ClearCommError($hFile)
_CommAPI_PurgeComm($hFile)

_CommAPI_TransmitString($hFile, "OUTP ON"&@LF) ;Turn output on
Sleep (1000)
_CommAPI_TransmitString($hFile, "VOLT 7.0"&@LF) ;Adjust voltage to 7 volts
Sleep (1000)
_CommAPI_TransmitString($hFile, "VOLT 0.0"&@LF) ;Adjust voltage to 0 volts
Sleep (1000)
_CommAPI_TransmitString($hFile, "OUTP OFF"&@LF) ;Turn output off
Sleep (1000)
_CommAPI_TransmitString($hFile, "*IDN?") ;Identify your-self

Local $sResult =_CommAPI_ReceiveString($hFile, 1, 0);Recieve string

_CommAPI_ClosePort($hFile)

MsgBox($MB_SYSTEMMODAL, "Title", $sResult, 5)

I know that the commands are being sent properly because I can see the output status change on the display of the power supply and my connected LED lights up and then goes out.  Also here is a link to the user manual http://cp.literature.agilent.com/litweb/pdf/E3646-90001.pdf

Thank you in advance!

Hello,

don't forget the @LF to your requet

_CommAPI_TransmitString($hFile, "*IDN?"&@LF) ;Identify your-self

you can try to change the timeout parameter like this

Local $sResult =_CommAPI_ReceiveString($hFile, 500);Recieve string

and for testing the port COM, you can use Realterm.exe 

 

 

 

 

Share this post


Link to post
Share on other sites

Hello, I am trying to use this UDF, and copied the script by  Terribletrux. Sending a string was ok for my device. Reading worked after I modified the timeout parameter, as suggested by M2C. However, I get only very strange characters in the reading. In which format is the data coming? Using commMG by Martin it came directly in Ascii and I had no problem with this. Thank you!

Share this post


Link to post
Share on other sites

Hello

 

I have a problem.

Now, I can use this source code to reboot DUT, but I would like to know how to display all logs and save log. My GUI only displays one log.

e.g. If I use tera term , it displays console log and we can save it.

 

How could I know response message whether correct or not.

e.g. If we transmitting string "username"  and  "password", we could login DUT by Autoit. 

       sometimes, we transmitting string "usename" and "password", it displays incorrect username and password. 

       how can I avoid this problem, it seems timer problem.

Edited by Englam

Share this post


Link to post
Share on other sites

I'm using AdlibRegister() to draw _CommAPI_ReceiveString() function at regular intervals but sleep and loop can delay this call and I need to call this no matter what the script function is doing, there is a way to do this?

EDITED:  It would be even better if the received data was treated as an event and _CommAPI_ReceiveString() only be performed when new data were received!

Edited by Belini

Share this post


Link to post
Share on other sites

@Chimp tried using _WinAPI_SetTimer() to draw _CommAPI_ReceiveString() function every 50 milliseconds but the script was too heavy and reached till even paralyze!

Share this post


Link to post
Share on other sites

perhaps _CommAPI_ReceiveString() takes too long to finish its task, causing a cycle too "tight" ...


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

@Chimp I think you are right, then the solution is to know when there is new data to draw _CommAPI_ReceiveString() function will be possible to do this monitor the COM port without using the _CommAPI_ReceiveString()?

Share this post


Link to post
Share on other sites

Maybe it can be setup a mechanism that should allow this udf to fire events on comm status changes, like already stated in post #51.
Unfortunatelly I've found no examples on how things has to be setup to accomplish that, and also I don't know how to do it myself.
Here 2 links where there is mention about something like this:

http://www.codeproject.com/Articles/2682/Serial-Communication-in-Windows (Search for WaitCommEvent)

WaitCommEvent (Waits for an event to occur for a specified communications device)

p.s.

if some smart mind with a good soul knows how to do it and could post an example, we could be very thankful... :)

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
Quote

Unfortunatelly I've found no examples on how things has to be setup to accomplish that, and also I don't know how to do it myself.

I also do not have enough knowledge to start a code of these!

 

Quote

if some smart mind with a good soul knows how to do it and could post an example, we could be very thankful... :)

I'm already grateful to you @Chimp for trying to help, now remains only hope that someone with greater knowledge can help!

Share this post


Link to post
Share on other sites
On 31.03.2014 at 3:27 AM, thoms said:

Hi adom,

I guess these comports are free : I'm using com0com to make 2 bridges (COM9<->COM10 and COM11<->COM12) and Realterm on each side (COM10 and COM12). I'm in the middle conditioning  data from COM9 to COM11 and vice-versa.

RealTerm can connect on any virtual comport, ie. COM10 or whatever, but NOT on busy ports.

Given that the first com0com bridge is between COM9 and COM10, I've to connect to COM9. If I try the other direction : RealTerm on COM9 and me on COM10, I get the error message. About the other side: COM11 or COM12, I'm really expecting something that works.

Is there something else I can check?

Thx

Thoms

Hi Thoms!

I used to work with com0com, good one but not enough some features time to time... Now I'm working withVirtual Null Modem (http://www.eltima.com/products/virtual-null-modem/), which is allowed more options for  users, like strict baudrate emulation and possibility to create ports for the current user session only. Sometimes it's really very helpful.

Share this post


Link to post
Share on other sites

Hello,

I am quite sure that I must be doing something wrong while using this UDF. Every time I ran the code (Example-1: https://www.autoitscript.com/wiki/CommAPI_Examples) I got the following error. Any suggestion in figuring out what I am doing wrong will be very helpful.

CommAPI_Error.png

Here is the example that I am trying to run:

Example-1: test_CommAPI_Example2.au3

The CommAPI Files that I am using are:

1. CommInterface.au3

2. CommUtilities.au3

3. CommAPIHelper.au3

4. CommAPI.au3

5. CommAPIConstants.au3

6. CommObsolete.au3

Here is my System Information:

  •  Autoit version: 3.3.14.2
  •  Architecture type: X64
  •  Opreating System build number: 9600
  •  Service pack info: 
  •  Opreating System Type: WIN32_NT
  •  Oprating System Version: WIN_81

I used the following function to generate this:

f_ShowSystemInfo(1)

Func f_ShowSystemInfo($iShowMessageBoxFlag = 1)
    Local $sInfo =  " Autoit version: " & @AutoItVersion & @LF & _
                    " Architecture type: " & @OSArch & @LF & _
                    " Opreating System build number: " & @OSBuild & @LF & _
                    " Service pack info: " & @OSServicePack & @LF & _
                    " Opreating System Type: " & @OSType & @LF & _
                    " Oprating System Version: " & @OSVersion & @LF

    ConsoleWrite("System Info" & @LF & $sInfo & @LF)
    If ($iShowMessageBoxFlag = 1) Then
        MsgBox(0,"Version Info", $sInfo)
    EndIf
EndFunc

 

 

Edited by techsray

Share this post


Link to post
Share on other sites
On 7/25/2017 at 8:55 PM, techsray said:

Hello,

I am quite sure that I must be doing something wrong while using this UDF. Every time I ran the code (Example-1: https://www.autoitscript.com/wiki/CommAPI_Examples) I got the following error. Any suggestion in figuring out what I am doing wrong will be very helpful.

CommAPI_Error.png

Here is the example that I am trying to run:

Example-1: test_CommAPI_Example2.au3

The CommAPI Files that I am using are:

1. CommInterface.au3

2. CommUtilities.au3

3. CommAPIHelper.au3

4. CommAPI.au3

5. CommAPIConstants.au3

6. CommObsolete.au3

Here is my System Information:

  •  Autoit version: 3.3.14.2
  •  Architecture type: X64
  •  Opreating System build number: 9600
  •  Service pack info: 
  •  Opreating System Type: WIN32_NT
  •  Oprating System Version: WIN_81

I used the following function to generate this:

f_ShowSystemInfo(1)

Func f_ShowSystemInfo($iShowMessageBoxFlag = 1)
    Local $sInfo =  " Autoit version: " & @AutoItVersion & @LF & _
                    " Architecture type: " & @OSArch & @LF & _
                    " Opreating System build number: " & @OSBuild & @LF & _
                    " Service pack info: " & @OSServicePack & @LF & _
                    " Opreating System Type: " & @OSType & @LF & _
                    " Oprating System Version: " & @OSVersion & @LF

    ConsoleWrite("System Info" & @LF & $sInfo & @LF)
    If ($iShowMessageBoxFlag = 1) Then
        MsgBox(0,"Version Info", $sInfo)
    EndIf
EndFunc

 

 

I figured it out. The scripts will work with a small modification at the top. You need to comment out "#NoAutoIt3Execute" from each of the scripts and replace it by the following.

;#NoAutoIt3Execute
; Refer to this forum post for the following change:
;   https://www.autoitscript.com/forum/topic/161004-autoit-v33102-runautoitexe-autoit3executeline-issue/?do=findComment&comment=1168605
#pragma compile(AutoItExecuteAllowed, true)

The fix mentioned above is necessary and in accordance with the "Change Log":

Quote

3.3.9.10 (21st July, 2013) (Beta)

AutoIt:

- Changed: #NoAutoIt3Execute option replaced with #pragma compile(AutoItExecuteAllowed, true). Default is false.

 

I tested one of the Utility Functions from CommUtilities.au3 and it ran perfectly. After you have made the change, try the following:

#include <CommUtilities.au3>


Local $sCommPorts = _CommAPI_GetCOMPorts()
ConsoleWrite("COM Ports:" & @LF & $sCommPorts & @LF)

I hope this helps. The scripts were copied from here: https://www.autoitscript.com/wiki/CommAPI and then corrected.

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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By cheeroke
      Hi all,
      I got this code and would like to be able to change Baud Rate and instead of sending character by character i would like to be able (if possible) to send whole string. But i don't know how to change it.
      I am taking input from file and processing whole line (this is done in FilesHandling.au3).
      To execute this i am just calling SendData("FileName", int) in "main" script.
      Any help very appreciated.
      #include <WinAPI.au3> #include <Array.au3> #include "FilesHandling.au3" ;init DLL function, we need handle to call the function $h = DllCall("Kernel32.dll", "hwnd", "CreateFile", "str", "\\.\COM19", "int", BitOR($GENERIC_READ,$GENERIC_WRITE), "int", 0, "ptr", 0, "int", $OPEN_EXISTING, "int", $FILE_ATTRIBUTE_NORMAL, "int", 0) $handle=$h[0] Func SendData($FileName, $LineNumber) ;string to be send $c = readFile($FileName, $LineNumber) $cLenght = StringLen($c) $aArray = StringSplit($c, "") ;_ArrayDisplay($aArray, "", Default, 64) For $i = 1 To $cLenght writeChar($handle, $aArray[$i], $cLenght) Next ;move to next line writeChar($handle, @CR,1) EndFunc ;write a single char func writeChar($handle,$c,) $stString = DLLStructCreate("char str") $lpNumberOfBytesWritten = 0 DllStructSetData($stString, 1, $c) $res = _WinAPI_WriteFile($handle, DllStructGetPtr($stString, "str"), 1,$lpNumberOfBytesWritten) if ($res<>true) then ConsoleWrite ( _WinAPI_GetLastErrorMessage() & @LF) EndIf EndFunc  
    • By MazeM
      Hi
      here's another UDF for the serial port. It is very similar to CommAPI using kernel32.dll, but all code is packed into a single file without any dependencies, not even using WinAPI.au3. It differs from existing UDF that it doesn't allow a timeout when reading, instead it always returns immediately, either with the requested amount ob bytes read or with a failure status. And of course there is a function provided to query the amount of available bytes in the receive buffer. The reason behind this design decision: You can do 1000 other things in the main loop while checking from time to time if enough data bytes arrived. There's no point to block the program waiting for the serial port.
      It is currently a work-in-progress, as I didn't test all functions yet. The code was developed and tested on Windows 7 64 bit.  The ComUDF-Tests.au3 shows some tests and basic usage of the UDF. Maybe there's no reason to use this UDF, given the existence of the others UDFs, but I did it to get to know DllCall better - I use structs no only to pass but also to get data back (I don't use the array returned by DllCall to read that data, unless required). You're welcome to test it on older and newer Windows versions.
      Here's a list of the implemented functions:
      ; _ComListPorts ; _ComOpenPort ; _ComSetTimeouts ; _ComClosePort ; ; _ComSetBreak ; _ComClearBreak ; _ComGetInputcount ; _ComGetOutputcount ; _ComClearOutputBuffer ; _ComClearInputBuffer ; ; _ComSendByte ; _ComReadByte ; _ComSendBinary ; _ComReadBinary ; ; _ComSendChar ; _ComReadChar ; _ComSendCharArray ; _ComReadCharArray ; _ComSendString ; _ComReadString ; ; __ComClearCommError ; __PurgeComm Maze
       
      ComUDF.au3
      ComUDF-Tests.au3
    • By OldNoob
      I'm attempting to call the winapi function EnumSystemFirmwareTables using DllCall "Kernel32.dll" without success. I am a total noob when it comes to this and could use some direction. Based on the documentation "Dealing with Dlls in AutoIt" by Andreas Karlsson, I have tried using the following code to obtain the buffersize of the Firmware Table Buffer.
      Thanks in advance for any help
      #include <WinAPI.au3> MsgBox(0, "ESFT BufferSize", "BufferSize = " & _EnumSystemFirmwareTables()) Func _EnumSystemFirmwareTables() $aRet = DllCall ("Kernel32.dll", "UINT", "EnumSystemFirmwareTables", "DWORD", "ACPI", "PVOID" ,Null , "DWORD" ,Null) if @error Then MsgBox (0,"Error","An error ocurred with the DLLCALL, error returned = " & @error &@CRLF & "GetLastError = " & _WinAPI_GetLastError ( ),0) Exit else Return $aRet endif EndFunc  
×
×
  • Create New...