Jump to content

CommAPI - Serial and parallel communication with Windows API


therealhanuta
 Share

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 

 

 

 

 

Link to comment
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!

Link to comment
Share on other sites

  • 2 months later...

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
Link to comment
Share on other sites

  • 2 months later...

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
Link to comment
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!

Link to comment
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()?

Link to comment
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

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

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

Link to comment
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!

Link to comment
Share on other sites

  • 5 months later...
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.

Link to comment
Share on other sites

  • 1 month later...
  • 6 months later...
  • 3 months later...
  • 4 weeks later...

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
Link to comment
Share on other sites

  • 3 months later...
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.

Link to comment
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
 Share

×
×
  • Create New...