Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

Yes, they are usb virtual com ports.

I'm not sure how your DLL works. In netcomm ocx I did this. Basically if the port gets disconnected i just want to turn off the gps. i don't need it to work again automatically when the gps gets plugged back in. I havent gotten any bsods when unplugging the gps, the program just crashes

If $NetComm.InBufferCount Then
            $Buffer = $NetComm.InBufferCount
            If $Buffer > 100 And $LatTest = 0 And TimerDiff($timeout) < $maxtime Then
                $inputdata = $NetComm.inputdata
                $gps = StringSplit($inputdata, "$")
                For $readloop = 1 To $gps[0]
                    If StringInStr($gps[$readloop], "GPGGA") Then
                        _GetGpsData($gps[$readloop])
                        If $LatTest = 1 Then ExitLoop
                        If TimerDiff($timeout) > $maxtime Then ExitLoop
                    EndIf
                Next
                If $LatTest = 1 Then
                    $refresh_gps = TimerInit()
                    ExitLoop
                EndIf
            EndIf

            If TimerDiff($timeout) > $maxtime Then
                GUICtrlSetData($msgdisplay, 'GPS Timeout')
                ExitLoop
            EndIf
            Sleep($maxtime / 100)
            $disconnected_time = TimerInit() ;reset gps turn off timer
        Else
            If $disconnected_time = -1 Then $disconnected_time = TimerInit()
            If TimerDiff($disconnected_time) > 10000 Then ; If nothing has been found in the buffer for 5 seconds, turn off gps
                $disconnected_time = -1
                $return = 0
                _GpsToggle()
                SoundPlay($SoundDir & $error_sound, 0)
            EndIf
        EndIf

I am expecting (hoping) that I will be able to prevent a crash by improving the error checking, but I need to dedicate a couple of hours to it and I haven't been able to do that yet.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

  • 1 month later...

Any progress on the error handling?

I'm ashamed to say that there isn't any. I tried for a short time but didn't manage to improve it. Since it wasn't something that was stopping anyone using it I put it to one side for a while, and that's where it's stayed.

I doubt I'll get back to it until February, so give me another kick in a month if I haven't posted an update.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

newbie here!!

I wanna create a loop to send a command to serial port

receive the result

wait

and after nth time end the program

How can i do that??

thanks in advance

Welcome to the forums :D

I see that my down load for the latest version doesn't have the example with it, so I have added this as an extra attachment in the first post. It should work with the latest version of AutoIt.

Try the running the example and then if you have questions come back again.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Welcome to the forums :D

I see that my down load for the latest version doesn't have the example with it, so I have added this as an extra attachment in the first post. It should work with the latest version of AutoIt.

Try the running the example and then if you have questions come back again.

Hi martin. Here is a newbie too. First congratulations for your Serial Port UDF.

I had already looked at the Serial Port UDF but i couldn't make it work even becouse i'm a newbie, but now that you post an example i can get start...

One question, in your example you put:

#include <CommMG.au3>;or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'

Do i need to put the CommMG.au3 with the other autoit3 functions? And the commMg.dll, could i put in the /windows directory so i don't need to use the #include @SciptDir & '\commmg.dll' ?

Thanks.

Bruno

Link to comment
Share on other sites

Hi martin. Here is a newbie too. First congratulations for your Serial Port UDF.

I had already looked at the Serial Port UDF but i couldn't make it work even becouse i'm a newbie, but now that you post an example i can get start...

One question, in your example you put:

#include <CommMG.au3>;or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'

Do i need to put the CommMG.au3 with the other autoit3 functions? And the commMg.dll, could i put in the /windows directory so i don't need to use the #include @SciptDir & '\commmg.dll' ?

Thanks.

Bruno

You're right, I did say that but it's a mistake. When I've finished this reply I will correct it. What I meant to say was

#include <CommMG.au3>;or if you save the commMg.AU3 in the @scripdir use #include @SciptDir & '\commmg.AU3'

; or just #include 'commg.au3'

Now that you've made me think of it, I will also add

; the dll (commg.dll) must be in either the script directory or the Windows directory.

My apologies for the error.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

You're right, I did say that but it's a mistake. When I've finished this reply I will correct it. What I meant to say was

#include <CommMG.au3>;or if you save the commMg.AU3 in the @scripdir use #include @SciptDir & '\commmg.AU3'

; or just #include 'commg.au3'

Now that you've made me think of it, I will also add

; the dll (commg.dll) must be in either the script directory or the Windows directory.

My apologies for the error.

Martin now it´s ok... :D

Now i can start to play with the Serial port...

Thanks Martin...

Bruno

Link to comment
Share on other sites

It's me again...

Martin, i'm trying to send Hex values thoght your UDF...

I'm using your example, but the hex values isn't working...

CODE
Func SendEvent();send the text in the inputand append CR

$msg = InputBox("Numero de bytes","Digite o numero de bytes que serao enviados")

$msg = Hex($msg,1)

_CommSendstring($msg);& GUICtrlRead($Input1))

GUICtrlSetData($Input1,'');clear the input

GUICtrlSetState($edit1,$GUI_FOCUS);sets the caret back in the terminal screen

EndFunc

I change your SendEvent() function for convert the $msg to hex but if i type 0 on the serial port it appers 0x0C.

Any tip???

Thanks.

Bruno

Link to comment
Share on other sites

It's me again...

Martin, i'm trying to send Hex values thoght your UDF...

I'm using your example, but the hex values isn't working...

CODE
Func SendEvent();send the text in the inputand append CR

$msg = InputBox("Numero de bytes","Digite o numero de bytes que serao enviados")

$msg = Hex($msg,1)

_CommSendstring($msg);& GUICtrlRead($Input1))

GUICtrlSetData($Input1,'');clear the input

GUICtrlSetState($edit1,$GUI_FOCUS);sets the caret back in the terminal screen

EndFunc

I change your SendEvent() function for convert the $msg to hex but if i type 0 on the serial port it appers 0x0C.

Any tip???

Thanks.

Bruno

I don't understand that at the moment.

When you say "it appears" where does it appear? Is it on the device you are sending the data to?

What is the reason for sending Hex(value,1)? Hex(0,1) is '0' so you are just sending the character '0'.

If you use the example script does the text get sent as expected?

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I don't understand that at the moment.

When you say "it appears" where does it appear? Is it on the device you are sending the data to?

What is the reason for sending Hex(value,1)? Hex(0,1) is '0' so you are just sending the character '0'.

If you use the example script does the text get sent as expected?

It appers on the serial output, i´m using an osciloscpe to see it.

I´m want to send hex values becouse if you type '0' and send in your example it will appers on the serial output the 0x30 (ASCII value for 0) and @CR (i don´t remember the ASCII representation). So in the code that i posted i used the hex() to convert the value to hexadecimal but it didn´t output the right value, in this one sending '0' it sends 0x0C.

If you use the example script does the text get sent as expected?

It´s sented, but in the ASCII representation and i´m trying to change it and send hex or decimal values as it is written.

I hope you understend my explanation.

Thanks.

Bruno

Link to comment
Share on other sites

How about this Bruno, instead of using Hex use the Chr command.

Func SendEvent();send the text in the inputand append CR

$msg = InputBox("Numero de bytes","Digite o numero de bytes que serao enviados")

$msg = CHR($msg)

_CommSendstring($msg);& GUICtrlRead($Input1))

GUICtrlSetData($Input1,'');clear the input

GUICtrlSetState($edit1,$GUI_FOCUS);sets the caret back in the terminal screen

EndFunc

Am Also a fan of this UDF.

Link to comment
Share on other sites

How about this Bruno, instead of using Hex use the Chr command.

Func SendEvent();send the text in the inputand append CR

$msg = InputBox("Numero de bytes","Digite o numero de bytes que serao enviados")

$msg = CHR($msg)

_CommSendstring($msg);& GUICtrlRead($Input1))

GUICtrlSetData($Input1,'');clear the input

GUICtrlSetState($edit1,$GUI_FOCUS);sets the caret back in the terminal screen

EndFunc

Am Also a fan of this UDF.

Hi Smooth.

But this way will be the same.

If you type '0' it will send 0x30 or 48. I mean that i want to send 0x00.

Don't worry, i take other way to solve the problem.

Thanks.

Bruno

Link to comment
Share on other sites

It appers on the serial output, i´m using an osciloscpe to see it.

I´m want to send hex values becouse if you type '0' and send in your example it will appers on the serial output the 0x30 (ASCII value for 0) and @CR (i don´t remember the ASCII representation). So in the code that i posted i used the hex() to convert the value to hexadecimal but it didn´t output the right value, in this one sending '0' it sends 0x0C.

If you use the example script does the text get sent as expected?

It´s sented, but in the ASCII representation and i´m trying to change it and send hex or decimal values as it is written.

I hope you understend my explanation.

Thanks.

Bruno

If you want to send the binary value, ie a byte, then use _CommSendByte($val) where $val is 0 minimum, 255 maximum.

eg in your example use

_CommSendByte(0)

See also _CommReadByte.

You cannot send a byte of value zero using the string functions because a byte of zero is used to indicate the end of a string. That is one the reason I developed the UDF, many other utilities can make sending binary data difficult.

If you need to deal with a lot of binary data then the functions

_CommSendByteArray and _CommReadByteArray

could be useful. To use these you need to deal with DllStucts.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

thanks for this udf. wish it could be blend in into the next version of autoit with parralel port udf.

great work.

I'm pleased to help icadea.

I've learned a bit since writing this udf and if I started again it would be different of course, but, even if it were written very much better, it's not the sort of thing that should to be included with AutoIt IMO. Those who want it can get it, but most have no need for it.

I have no plans to develop a parallel port udf, but I think you'll find examples in the forums. I think I've read about someone operating relays from a parallel port and something about a remote door control.(Vague memories.)

Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

If you want to send the binary value, ie a byte, then use _CommSendByte($val) where $val is 0 minimum, 255 maximum.

eg in your example use

_CommSendByte(0)

See also _CommReadByte.

You cannot send a byte of value zero using the string functions because a byte of zero is used to indicate the end of a string. That is one the reason I developed the UDF, many other utilities can make sending binary data difficult.

If you need to deal with a lot of binary data then the functions

_CommSendByteArray and _CommReadByteArray

could be useful. To use these you need to deal with DllStucts.

Thanks Martin, was this function that i'm looking for....

Bruno....

Link to comment
Share on other sites

Hi martin...

Now i'm working on the bytes functions of you UDF...

I don't know what is happen, if i'm doing something wrong or is the function...

dim $recbuf[3], $i
    _CommSendByte(255)  
    _CommSendByte(2)    
    _CommSendByte(254)  
    _CommSendByte(52)   
    For $i = 0 To 2
        $recbuf[$i] = _CommReadByte(0)          
    Next
    ;$recbuf = _CommReadByteArray(1,3,0)  ; le os 3 bytes
    If @error <> 0 Or  $recbuf[0] = -1 Then
        MsgBox(0, 'Resultado', 'Erro')
    Else
        MsgBox(0, 'Resultado', 'Ok')
        $received = _ArrayToString ($recbuf," ")    
        ;MsgBox(0, 'Resultado', $re)
        GUICtrlSetData ($valor,$received)   
    EndIf

The problem is that only in second time that this run that the _CommReadByte works, on the first time that this code run it gets @erros = 1 and run the MsgBox(0, 'Resultado', 'Erro') and the second, third... it runs ok...

My mistake??

Thanks.

Bruno

Link to comment
Share on other sites

Hi martin...

Now i'm working on the bytes functions of you UDF...

I don't know what is happen, if i'm doing something wrong or is the function...

dim $recbuf[3], $i
    _CommSendByte(255)  
    _CommSendByte(2)    
    _CommSendByte(254)  
    _CommSendByte(52)   
    For $i = 0 To 2
        $recbuf[$i] = _CommReadByte(0)          
    Next
    ;$recbuf = _CommReadByteArray(1,3,0)  ; le os 3 bytes
    If @error <> 0 Or  $recbuf[0] = -1 Then
        MsgBox(0, 'Resultado', 'Erro')
    Else
        MsgBox(0, 'Resultado', 'Ok')
        $received = _ArrayToString ($recbuf," ")    
        ;MsgBox(0, 'Resultado', $re)
        GUICtrlSetData ($valor,$received)   
    EndIf

The problem is that only in second time that this run that the _CommReadByte works, on the first time that this code run it gets @erros = 1 and run the MsgBox(0, 'Resultado', 'Erro') and the second, third... it runs ok...

My mistake??

Thanks.

Bruno

I don't know what the probelm is. You should check for errors immediately after calling the function, and _CommReadByte returns a string representation of the byte.

Can you try the code below and let me know what happens.

;assuming port set corectly at this stage
Dim $recbuf[3], $i
Global $Result = ''
_CommSendByteCheck(255)
_CommSendByteCheck(2)
_CommSendByteCheck(254)
_CommSendByteCheck(52)
For $i = 0 To 2
    If _CommGetInputCount() = '0' Then
        Sleep(300)
        If _CommGetInputCount() = '0' Then
            MsgBox(0, 'ERROR ', 'No data after 300mS')
            Exit
        EndIf
    Else   ;either an error or data available
        If @error = 1 Then
            MsgBox(0, 'Error', 'Failed to call _CommGetInputCount')
            Exit
        EndIf
    EndIf
    
;no error, data is available
    $Result &= _CommReadByte(0)
    Switch @error
        Case 1
            MsgBox(0, 'Error 1', 'No data to read, but data should be available.')
            Exit
        Case 2
            MsgBox(0, 'Error 2', 'Could not call function _CommReadByte.')
            Exit
    EndSwitch

    If $i < 2 Then $Result &= ', '
    
Next

MsgBox(0, 'Resultado', $Result)

Func _CommSendByteCheck($val)
    AdlibEnable("toolong", 2000)
;Will wait till byte sent. Could hang if problem so Adlib rescues
    If _CommSendByte($val, 1) = -1 Then
        MsgBox(0'Error', 'Could not send byte val ' & $val)
        Exit
    EndIf
    AdlibDisable()
EndFunc  ;==>_CommSendByteCheck

Func Toolong()
    AdlibDisable()
    MsgBox(0, 'ERROR', 'Could not send byte within 2 Seconds!')
    Exit
EndFunc  ;==>Toolong

martin

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

I don't know what the probelm is. You should check for errors immediately after calling the function, and _CommReadByte returns a string representation of the byte.

Can you try the code below and let me know what happens.

;assuming port set corectly at this stage
Dim $recbuf[3], $i
Global $Result = ''
_CommSendByteCheck(255)
_CommSendByteCheck(2)
_CommSendByteCheck(254)
_CommSendByteCheck(52)
For $i = 0 To 2
    If _CommGetInputCount() = '0' Then
        Sleep(300)
        If _CommGetInputCount() = '0' Then
            MsgBox(0, 'ERROR ', 'No data after 300mS')
            Exit
        EndIf
    Else  ;either an error or data available
        If @error = 1 Then
            MsgBox(0, 'Error', 'Failed to call _CommGetInputCount')
            Exit
        EndIf
    EndIf
    
;no error, data is available
    $Result &= _CommReadByte(0)
    Switch @error
        Case 1
            MsgBox(0, 'Error 1', 'No data to read, but data should be available.')
            Exit
        Case 2
            MsgBox(0, 'Error 2', 'Could not call function _CommReadByte.')
            Exit
    EndSwitch

    If $i < 2 Then $Result &= ', '
    
Next

MsgBox(0, 'Resultado', $Result)

Func _CommSendByteCheck($val)
    AdlibEnable("toolong", 2000)
;Will wait till byte sent. Could hang if problem so Adlib rescues
    If _CommSendByte($val, 1) = -1 Then
        MsgBox(0'Error', 'Could not send byte val ' & $val)
        Exit
    EndIf
    AdlibDisable()
EndFunc ;==>_CommSendByteCheck

Func Toolong()
    AdlibDisable()
    MsgBox(0, 'ERROR', 'Could not send byte within 2 Seconds!')
    Exit
EndFunc ;==>Toolong

martin

Martin, i will try it... Now i'm traveling... But when i came back i will make some tests and let you now...

Thanks..

Bruno

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