Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

@Martin, i am getting correct data on hyper terminal, say 12345 from a micro-controller,  when using this script, on the same serial port settings that i had set on hyper terminal,  i am getting raw data like &^&*^#| instead of getting 12345, my serial port settings are, 2400 buad rate, com 1, no parity, no hardware flow control, data bits 8, stop bit 1. any help? 

Link to comment
Share on other sites

  • 2 weeks later...

Hi BernyHsu,

Can you tell me the settings for the serial port? The number of data bits, parity etc.

Hi Martin

Thanks for your reply.

My setting on CommgExample.au3 is the same as it is in SecureCRT:

Port: COM16

Baud rate: 57600

Data bits: 8

Parity: None

Stop bits: 1

I have done some test which changed the data bits from 8 to 7. The data from my device to the text box is fine. But the commands I inputted cannot reach the device. After changing back to 8. I can input command to my device but the return message is garbled again. Is there any advice? Thank you!!

Data bit: 7

LkhbKR.png

Data bit: 8

I6wogU.png

Edited by BernyHsu
Link to comment
Share on other sites

  • 3 weeks later...

Hi Martin,

I think that I have found the issue in CommgExample.au3 of the occurrence of garbled messages. After few days reviewing the code. I got the point at line 25:

Dim $ParityType[5] = ["odd", "even", "none", "Mark", "Space"]

If  we choose parity for "none", the return parity number would be 2 instead of 0, this is how it causes the issue. According the description in CommgMG.au3:

 

 $iParity - integer: 0=None,1=Odd,2=Even,3=Mark,4=Space

So I corrected line 25 as following:

Dim $ParityType[5] = ["none", "odd", "even", "Mark", "Space"]

When we choose "none", the return parity number would be 0, everything is fine!

Thank you very much.

Edited by BernyHsu
Link to comment
Share on other sites

@Martin, i am getting correct data on hyper terminal, say 12345 from a micro-controller,  when using this script, on the same serial port settings that i had set on hyper terminal,  i am getting raw data like &^&*^#| instead of getting 12345, my serial port settings are, 2400 buad rate, com 1, no parity, no hardware flow control, data bits 8, stop bit 1. any help? 

Hey saloo4pro,

Were you using CommgExample.au3 to test? Please refer to my last post in this topic. Maybe it could give you a help.

Edited by BernyHsu
Link to comment
Share on other sites

  • 1 month later...
  • 1 month later...

I am trying to use this UDF on Windows 7 64 bit without success. Is it because of the DLL being a 32 bit one?

[size="2"][font="arial, verdana, tahoma, sans-serif"]ProtectData - A Data Protection software for floppies[/font][/size] [size="2"][hr][/size][size="2"]Sessionchange - A Windows service capable of tracking session change events[/size][size="2"][b][/b][/size]

Link to comment
Share on other sites

I am trying to use this UDF on Windows 7 64 bit without success. Is it because of the DLL being a 32 bit one?

compile your app for x86 not for x64.

Saludos

Link to comment
Share on other sites

  • 1 month later...

Hi,

Getting error upon running the commexample script

"....CommgExample.au3" (172) : ==> Subscript used on non-accessible variable.:
For $pl = 1 To $portlist[0]
For $pl = 1 To $portlist^ ERROR

Added a 

#AutoIt3Wrapper_UseX64=n

ontop on the script, because i'm running a x64 windows7, error still exists. 

 

Any suggestions ?

EDIT: running latest version of AutoIT3

Edited by JustSomeone
Link to comment
Share on other sites

  • 1 month later...

Hi,

Getting error upon running the commexample script

"....CommgExample.au3" (172) : ==> Subscript used on non-accessible variable.:
For $pl = 1 To $portlist[0]
For $pl = 1 To $portlist^ ERROR

Added a 

#AutoIt3Wrapper_UseX64=n

ontop on the script, because i'm running a x64 windows7, error still exists. 

 

Any suggestions ?

EDIT: running latest version of AutoIT3

Hi JustSomeone,

I have encountered the same issue before, plz check out this post:

Hope it really helps.

Link to comment
Share on other sites

  • 2 months later...

Can someone post a small script on how to send and receive hex bytes?

I am actually trying to connect to an external virtual com port and i am not getting proper acknowledge from the device.
I want to send 4 bytes and I should receive 2 bytes FF 00 acknowledge.

With a com monitor, i can see the 4 bytes are sent but the acknowledge is 0x5A.
Do I have bad timing? How can i solve it?  

#include <CommMG.au3>
$result=_CommSetDllPath("C:\Program Files (x86)\AutoIt3\Include\commg.dll")
if $result=-1 Then MsgBox(0, "Dll path", "Dll not found.")

_CommSetport(2, $result, 19200, 8, 0, 2, 2)
_CommSendbyte(0x5A)
_CommSendbyte(0x02)
_CommSendbyte(0x00)
_CommSendbyte(0x01)

 

Link to comment
Share on other sites

This function is not part of the UDF, so why do you believe is should be there?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

  • 1 month later...

Hi Martin,

I am using this UDF for years and have written several complex scripts to control various hardware.

Now, with Windows 10 my scripts are not working anymore!

The available comports are listed as usual but when I try to open the port it says "port does not exist".

With Hyperterminal I am able to communicate to this port without problems, so it really seems to be a problem of the UDF or more liekely the dll.

 

can you help?

 

 

Link to comment
Share on other sites

  • 3 weeks later...

Hi Martin,

I want to thank the topic that has helped me a lot that is commMg.au3 and commg.dll, congratulations

I'm having problems CommMg.au3 on windows 10. In other Windows versions works perfectly.

perhaps others are having the same problem.

Thanks.

 

Edited by willindows
Link to comment
Share on other sites

I'm having a problem with _CommGetLine.  Throughout most of my program it works very well (thanks Martin), but it seems to choke on multi line responses.

I'm using it to communicate with two devices (a pressure or flow controller and a flow meter or pressure gauge).  The devices share a com port, wired in parallel, and only reply when the device is polled by its one letter identifier... send "D" from the script, for example, and the "D" device responds with a string containing current pressure of flow data, which I read using _CommGetLine.  Send "D" followed by something else and the device responds accordingly.  The "C" device ignores it until you query starting with a "C".  Where I'm having a problem is where the response from the device is multiple lines.  If I send "D??M*" the device responds with ten lines of device data, each terminated by a carriage return.  I loop through _CommGetLine until I see what I expect in the last line.

The problem is that I don't get the last line.  If I use hyperterminal to send D??m*, I get:

F  M00 ALICAT SCIENTIFIC
F  M01
F  M02 Ph   520-290-6060
F  M03 Fax  520-290-0109
F  M04 Model Number  M-50SCCM-D
F  M05 Serial Number 109023
F  M06 Date Manufactured 11/21/2014
F  M07 Date Calibrated   11/21/2014
F  M08 Calibrated By     JC
F  M09 Software Revision 5v04

IF, however, I use a series of _CommGetLine calls, I get all but the last line.  It's not just missing the carriage return; if I use a timeout time I don't get any characters at all from the "F M09..." line.  Now these devices are known to be flaky, which is why I have it retry 10 times if it doesn't get a response, but with hyperterminal I always get the last line but with autoit I never get the last line, i.e. I never see "Software Revision"; it always finishes with an error.  Now in actuality my script works because I only actually need the model and serial number lines, but I'd like to understand what the problem is here.

Opt("trayicondebug", 1)
#include <CommMg.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <GUIConstants.au3>
#include <ButtonConstants.au3>
#include <WindowsConstants.au3>

#include <GraphGDIPlus.au3>

$FC_ID = "C" ; flow controller ID
$DP_ID = "D" ; pressure gage ID
$fmod = "Not Found"
$pmod = "Not Found"
$fserial = ""
$pserial = ""

Dim $sErr

$comport = 3
$combaud = 38400
$combits = 8
$comparity = 0
$comstop = 1
$comflow = 0
$set = _CommSetPort($comport, $sErr, $combaud, $combits, $comparity, $comstop, $comflow)
$comerr = @error
If ($comerr <> 0) Then
    SplashOff()
    $ret = MsgBox(4096 + 1 + 16, "Error", "Error " & $comerr & @CRLF & $sErr)
    If ($ret == 2) Then Exit
    $gages = 0
Else
    $gages = 1
EndIf

If $gages = 1 Then
    ;Determine whether it's a low pressure (ID "D") or high pressure (ID "E") gaguge
    If StringInStr(_Alicom($DP_ID), "Error") Then ; default D device not found
        $DP_ID = "E" ; assume it's E, we'll check it later anyway
    EndIf

    ;ConsoleWrite("zeroing flow controller...  ")
    _Alicom($FC_ID & "0")

    ;getPID parameters from flow controller
    $outstring = _Alicom($FC_ID & "R85")
    $outarray = StringSplit($outstring, " ")
    $PID_type = Number(($outarray[$outarray[0]]))

    $outstring = _Alicom($FC_ID & "R21")
    $outarray = StringSplit($outstring, " ")
    $PID_P = Number(($outarray[$outarray[0]]))

    $outstring = _Alicom($FC_ID & "R22")
    $outarray = StringSplit($outstring, " ")
    $PID_D = Number(($outarray[$outarray[0]]))

    $outstring = _Alicom($FC_ID & "R23")
    $outarray = StringSplit($outstring, " ")
    $PID_I = (Number($outarray[$outarray[0]]))

    ;get flow controller range
    ConsoleWrite(_Alicom($FC_ID & "??M*")) ; query for mfg data, first line isn't used
    Do
        $response = _Alicom("-") ; "-" just reads, doesn't send anything
        ConsoleWrite($response)
        If StringInStr($response, "Model Number") Then
            $frange = Number(StringMid($response, StringInStr($response, "-") + 1))
            ;ConsoleWrite("flow controller range is " & $frange & @CRLF)
            $fmod = StringStripCR(StringMid($response, StringInStr($response, "Model Number") + 14))
        ElseIf StringInStr($response, "Serial Number") Then
            $fserial = StringStripCR(StringMid($response, StringInStr($response, "Serial Number") + 14))
        Else
            ; Ignore string
        EndIf
    Until StringInStr($response, "Software Revision") Or StringInStr($response, "Error")
    ;ConsoleWrite(@CRLF & @CRLF & "range = " & $prange & " KPa" & @CRLF & @CRLF)

    _CommClearOutputBuffer()
    _CommClearInputBuffer()

    ;get pressure gage range
    ConsoleWrite(_Alicom($DP_ID & "??M*"))
    Do
        $response = _Alicom("-")
        ConsoleWrite($response)
        If StringInStr($response, "Model Number") Then
            $pmax = Number(StringMid($response, StringInStr($response, "-") + 1))
            $prange = $pmax
            $pmod = StringStripCR(StringMid($response, StringInStr($response, "Model Number") + 14))
        ElseIf StringInStr($response, "Serial Number") Then
            $pserial = StringStripCR(StringMid($response, StringInStr($response, "Serial Number") + 14))
        Else
            ; Ignore string

        EndIf
    Until StringInStr($response, "Software Revision") Or StringInStr($response, "Error")
    ConsoleWrite(@CRLF & @CRLF & "range = " & $prange & " KPa" & @CRLF & @CRLF)

    _CommClearOutputBuffer()
    _CommClearInputBuffer()


<snip, rest of program is irrelevant>

    
    Func _Alicom($sendstring)
    If ($sendstring <> "-") Then ; "-" only used when multiple line responses are expected
        $req = 0
        Do
            _CommSendString($sendstring & @CR, 1) ; send command
            $req = $req + 1
            If ($req = 10) Then
                MsgBox(48, "Comm Error", "Unable to send " & $sendstring & @CRLF & "Error code: " & @error & " Script line " & @ScriptLineNumber)
                ExitLoop
            EndIf
        Until @error = 0
    EndIf
    $tries = 0
    Do
        $rawstring = _CommGetLine(@CR, 0, 100) 
        $tries = $tries + 1
        If ($tries = 10) Then
            ConsoleWrite("No response from device" & @CRLF & "Sent: " & $sendstring & @CRLF & "Received: " & $rawstring & @CRLF & "Error code: " & @error & "  Script line " & @ScriptLineNumber & @CRLF )
            $rawstring = "Comm Error" & @CRLF
            _CommClearOutputBuffer()
            _CommClearInputBuffer()
            ExitLoop
        EndIf
    Until @error = 0
    If ($tries > 1) Then ConsoleWrite("Took " & $tries & " tries" & @CRLF)
    Return $rawstring
EndFunc   ;==>_Alicom

 

Edited by Dana
Link to comment
Share on other sites

Hi, I am new here, its my first post. I think my problem has not been discussed before.

A simple code tested with the newest versions of commg.dll and CommMG.au3 gives a strange result.

#INCLUDE <CommMG.au3>
Local $Err
_CommSwitch(3)
_CommSetPort(1, $Err, 9600, 6, 9, 1)
MsgBox( 0, 'TEST1', _CommPortConnection() )
_CommSwitch(3)
MsgBox( 0, 'TEST2', _CommPortConnection() )

First message shows "TEST1/COM1" and the second: "TEST2/None". And COM port does not work at this point indeed.

Removing te second _CommSwith function resolves the problem. But in my project, quite big as for Autoit, I use 3 COM ports and have to respond to many different events, so if there is something to do with any port, I simply tried to switch to corresponding channel because I don't know what channel was used before. If the channel was the same as current channel - port become closed. I wrote a wraparound function, which remembers the last channel, so the problem is solved by now.

Any chance to fix this bug?

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...