Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

Martin it works, i need to make some changes on the code. I use some part of your code.

The final code:

Func SendEvent();send the text in the inputand append CR
    dim $recbuf[3],$macadress[4],$i,$x,$y = 0

    For $x = 52 to 55
        _CommSendByte(255) 
        _CommSendByte(2)
        _CommSendByte(254)
        _CommSendByte($x)   
        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
            $recbuf[$i] = _CommReadByte(0)
        Next
        If $recbuf[0] = 6 Then 
            $macadress[$y] = Hex($recbuf[2],2)
            $y = $y + 1
        EndIf
    Next
    If @error <> 0 Or  $recbuf[0] = -1 Then
        MsgBox(0, 'Resultado', 'Erro')
    Else
        MsgBox(0, 'Resultado', 'Ok')
        $received = _ArrayToString ($macadress," ") 
        GUICtrlSetData ($valor,$received)   
    EndIf
    
EndFunc

Taking out the _CommGetInputCount it needs to run two times to get the right values.

Thanks.

Bruno

Link to comment
Share on other sites

  • 2 weeks later...

The above sounded like the problem I'm having. Great UDF btw martin, thanks.

Anyways, here's what I'm experiencing:

Connect my serial device,

run your example script (it works),

close the example,

run it again and the communications do not work.

if I close the example, the devices' lights blink (telling me the connection was closed).

when I run the example again, it works.

It seems to only close the connection every other time.

I thought it was only my code but as I said, the same happens with your example script.

Have you heard anyone else having this problem? I have only tried this 1 device so I definately won't rule it out but when it works, (every other time) it works great.

I've tried adding a sleep to the _comcloseport function but it didn't help.

If it helps, here is the documentation:

http://www.pioneerelectronics.com/PUSA/Sup...0.Support?tab=E

Edit: I am able to get it to work every time I run the script IF I change the COM port to 2 then back to 1

Edited by Teldin
Link to comment
Share on other sites

The above sounded like the problem I'm having. Great UDF btw martin, thanks.

Anyways, here's what I'm experiencing:

Connect my serial device,

run your example script (it works),

close the example,

run it again and the communications do not work.

if I close the example, the devices' lights blink (telling me the connection was closed).

when I run the example again, it works.

It seems to only close the connection every other time.

I thought it was only my code but as I said, the same happens with your example script.

Have you heard anyone else having this problem? I have only tried this 1 device so I definately won't rule it out but when it works, (every other time) it works great.

I've tried adding a sleep to the _comcloseport function but it didn't help.

If it helps, here is the documentation:

http://www.pioneerelectronics.com/PUSA/Sup...0.Support?tab=E

I don't know why this happens with your device. I haven't ben able to download the documentation you gave a link for because so far, amybe the site is too busy.

Have you tried using Hyerterminal or some terminal emulator? If so what happens with then?

Unfortunately, I cannot think of an explanation for why it should work every other time.

Can you tell me what version of AutoIt you are using?

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'm using 3.2.10 but you're right, it also seems to exhibit similar behavior with hyperterminal.

After more observation here's what seems to happen:

lights are off on the device,

run the script, no connection (commands don't work),

quit script which turn the lights on!?!,

run script again, commands work fine.

quit the script, lights turn off,

start at the beginning.

Weird. Why would it turn on only when I quit the script?

Here is the direct link to the pdf if you want to try it:

http://www.pioneerelectronics.com/pio/pe/i...0_RS232-CPM.pdf

Thanks for your time.

Link to comment
Share on other sites

I'm using 3.2.10 but you're right, it also seems to exhibit similar behavior with hyperterminal.

After more observation here's what seems to happen:

lights are off on the device,

run the script, no connection (commands don't work),

quit script which turn the lights on!?!,

run script again, commands work fine.

quit the script, lights turn off,

start at the beginning.

Weird. Why would it turn on only when I quit the script?

Here is the direct link to the pdf if you want to try it:

http://www.pioneerelectronics.com/pio/pe/i...0_RS232-CPM.pdf

Thanks for your time.

Have you tried sending some reset command to the player when it doesn't seem to respond? Of course if it isn't taking any notice of what is sent this won't help.

Have you tried connecting the cable as described in the manual here-

Some computers require the CTS port to be set to HIGH during communication. It is best to connect the CTS and DSR port on the computer to the DTR port on the player. During normal operation the players DTR is set to HIGH thus the unit is able to receive a command at any time.

I would not expect that this was needed if the handshaking is set to none, but it's worth trying, and there could always be an error in my dll, although I don't really see how it explains the every other time effect.

There is a function in my UDF called _CommSendBreak which was written for AutoitSteve but has never been tested. I would be interested to know if using this got communications working. Maybe _CommSendBreak(200,200), but this is clutching at straws because I don't see anything in the manual to suggest it will have any effect.

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

The player does not respond to any commands (obviously) when it's not communicating with the PC, and there seem to be no reset commands to try anyway.

I'm not sure what CTS/DSR stuff their talking about, the port on the player is a 15 pin serial "Interface Connector" connecting to my 9pin PC COM1 port. Maybe that is the problem, I'm using the cable from a Percon scanner. I am assuming (and hoping!) because it works fairly well, that's not the problem :)

Anyways,

I tried:

Func SendEvent();send the text in the input and append CR
    _CommSendBreak(200,200)
    _CommSendstring (GUICtrlRead($Input1) & @CR)
    ;GUICtrlSetData($Input1, '');clear the input
    GUICtrlSetState($Edit1, $GUI_FOCUS);sets the caret back in the terminal screen
EndFunc   ;==>SendEvent

But I just got E04 Errors without any command responces...(maybe a sleep was needed?)

What I did have some luck with is putting _CommClosePort() at the begging of the script. The device seems to behave a bit more predictably. it starts up and shuts down with the script. I still have to 'set port' twice, but I don't have to change the com port number anymore. I guess that's some progress.

Another interesting thing that may or may not be related,

When sending almost any command I first get a good responce, then an immediate error responce. See a log:

Command: ?R<CR> Response: E04 -----within 200ms - an error

Command: ?R<CR> Response: 01 ------correct responce to chapter request

Command: ?C<CR> Response: E04 -----within 200ms - an error

Command: ?C<CR> Response: 01 ------correct responce to title number request

Command: ?F<CR> Response: E04 -----within 200ms - an error

Command: ?F<CR> Response: 0007744 ----correct responce to frame number request

However, in reguards to a command which takes a second to two to complete (open, close the tray, etc.) I get the error first then I get an 'R' which means successful completion:

Command: OP<CR> Response: R

Command: OP<CR> Response: E04 ---immediately after sending OP command

According to the manual:

E04 - Feature not available - Non-Usable Function has been tried either the command mnemonic is wrong or or the command can not be used in this mode

I haven't taken a super close look at the UDF nor have I tried sniffing the COM port communications but could it be sending something the player does not recognize? If no then I guess the cable is the culprit...

Thanks again for all your help thusfar martin.

Edited by Teldin
Link to comment
Share on other sites

Well I found out why I was getting errors with each command. I was sending an additional <CR> with my command. I removed that and the errors are gone.

Also, is there a way to detect if any device is actually connected?

Edited by Teldin
Link to comment
Share on other sites

Well I found out why I was getting errors with each command. I was sending an additional <CR> with my command. I removed that and the errors are gone.

I'm glad there was a simple explanation.

Also, is there a way to detect if any device is actually connected?

If there is hardware handshaking you could send some characters and then check how many were still in the output buffer. Without hardware handshaking you could just send something and wait for a reasonable time for a reply. In your case if you send an invalid command you should get an error back. If no reply then no connection.

The level of some signals, such as DSR and CTS can indicate that a device is connected and ready, but I don't give you access to them in my dll.

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

It also requires GUIConstants.au3. How can i get it?

GuiConstants.au3 is in the includes for production 3.2.10.0 and Beta 3.2.11.1 AFAIK. It is planned to become redundant as described in the History section of the Help.

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

  • 2 weeks later...

That's great brendan! :) Thanks a lot for showing me the robots in action, I'm really pleased I've helped you to do that.

I assume the robots are independent of CD writers, is that correct? I like that you've got one robot feeding 2 devices. I suppose if you put them around in a circle one robot could feed maybe 8.

(I showed my wife but I had to prop her up because she kept falling asleep.)

Martin, just another update (though we talked about this a while back via email or PM, IIRC):

I distributed your DLL as part of an early version of my Universal Loader CLI a couple of months ago, which was released publicly as part of the the dbpoweramp R13 alpha/beta cycle (the code is in the driver set for the batch ripper modules). I've tested on some of my own 600 and 900 disc robots with very good success. Some other folks out there on the interwebs are now ripping 1000s of CDs hands-free with your little gem (and dbpoweramp, of course).

Meanwhile, I have to finish the work on the ULCLI, primarily to make it handle errors/problems encountered by the hardware and to enhance cooperation amongst processes serving a single robot. Then I'll unleash it on my own arena of robots.

Pperhaps I will call it...Ripperdome. :)

-brendan

Link to comment
Share on other sites

Martin, just another update (though we talked about this a while back via email or PM, IIRC):

I distributed your DLL as part of an early version of my Universal Loader CLI a couple of months ago, which was released publicly as part of the the dbpoweramp R13 alpha/beta cycle (the code is in the driver set for the batch ripper modules). I've tested on some of my own 600 and 900 disc robots with very good success. Some other folks out there on the interwebs are now ripping 1000s of CDs hands-free with your little gem (and dbpoweramp, of course).

Meanwhile, I have to finish the work on the ULCLI, primarily to make it handle errors/problems encountered by the hardware and to enhance cooperation amongst processes serving a single robot. Then I'll unleash it on my own arena of robots.

Pperhaps I will call it...Ripperdome. :)

-brendan

Thanks for the update Brendan, and congratulations on your success.

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

  • 3 weeks later...

Hello!

Troble, then use function _CommGetString()

For example, com rx data "TEMP=.(." or hex (54 45 4d 50 3d 00 28 fe)

use $rx = _CommGetString()

$rx = "TEMP=" or hex (54 45 4d 50 3d)

all char after chr(0x00) don't get

Why I can get full string?

Help me, please.

Link to comment
Share on other sites

Hello!

Troble, then use function _CommGetString()

For example, com rx data "TEMP=.(." or hex (54 45 4d 50 3d 00 28 fe)

use $rx = _CommGetString()

$rx = "TEMP=" or hex (54 45 4d 50 3d)

all char after chr(0x00) don't get

Why I can get full string?

Help me, please.

You cannot read nulls using the CommGetString function because a null indicates the end of a string.

You need to use _CommGetByte or use the _CommReadByteArray function.

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

Could someone show an example of _CommSendByteArray? How do you write the array to memory and pass the pointer?

Thanks!

Here's a simple example

$Numbytes1 = 16;say we have 16 bytes

$Struct1 = DllStructCreate("byte[" & $Numbytes1 & "]")

For $n = 1 To 16;put some random numbers in the struct
    DllStructSetData($Struct1, 1, Random(0, 255, 1), $n)
Next

;send the bytes
_CommSendByteArray(DllStructGetPtr($Struct1), $Numbytes1, 1)
if @error then MsgBox(0, 'ERROR', '_CommSendByteArrray failed')
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 your help Martin!

I had to fiddle with code a bit to get it to work. One cool thing is that I can char as the array type and directly passing the entire string in one shot. It works pretty well, but I had to use the "align 1" command to get it to work. Without that it would output all kind of garbage on the serial line.

Thanks again. What a great UDF!

$LastPacketTx = $APIPacket
    $APIPacketLength = StringLen($APIPacket)
    
    
    _CommSwitch($Port)
    
    If _CommPortConnection() == '' Then
        Output("Error: Comm port error")
        Return
    EndIf   
    
    $ByteArray = DllStructCreate("align 1;char[" & string($APIPacketLength) & "]")              
    DllStructSetData($ByteArray, 1, $APIPacket)
    
;send the bytes
    _CommSendByteArray(DllStructGetPtr($ByteArray), $APIPacketLength,1)
    if @error then output('_CommSendByteArrray failed')
Link to comment
Share on other sites

Thanks for your help Martin!

I had to fiddle with code a bit to get it to work. One cool thing is that I can char as the array type and directly passing the entire string in one shot. It works pretty well, but I had to use the "align 1" command to get it to work. Without that it would output all kind of garbage on the serial line.

Thanks again. What a great UDF!

$LastPacketTx = $APIPacket
    $APIPacketLength = StringLen($APIPacket)
    
    
    _CommSwitch($Port)
    
    If _CommPortConnection() == '' Then
        Output("Error: Comm port error")
        Return
    EndIf   
    
    $ByteArray = DllStructCreate("align 1;char[" & string($APIPacketLength) & "]")              
    DllStructSetData($ByteArray, 1, $APIPacket)
    
;send the bytes
    _CommSendByteArray(DllStructGetPtr($ByteArray), $APIPacketLength,1)
    if @error then output('_CommSendByteArrray failed')
Glad you got that to work, and thanks for the note about the alignment. I don't recall having that problem when I wrote the functions and tested them, but I didn't actually try the example I gave you :D .

BTW, you can do this

$ByteArray = DllStructCreate("align 1;char[" & StringLen($APIPacket) & "]")

in AutoIt and it will create a string formatted correctly, although the more explicit way you did it is how you would do it if you were used to C I guess.

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

  • 3 weeks later...

Can anybody help me, I am not a coder but I see that this UDF could do what I need, let me explain.

I have a CarPC and a valentine one radar that is interfaced to my PC and I would like to add GPS Geofence functionality to the radar.

The software for the CarPC plays .wav files every time there is an alarm and I would like to replace it with a blank .wav file for example every time I am near a supermarket the alarm goes off so I would like to set a perimeter and when I enter it have the regular wav file be replaced by a blank one.

Lets say if I create a master .txt file with the information when I don't want the alarm to go off the file should contain

latitude, longitude, radius, lenght of alarm silenced and alarm type separated by commas

1250.5101,24718.5201,0.0010,10000 ,X,K,KA,N

The autoit script will read the GPS information and when it finds a match within the range replace the wav file for 10 secs or so.

If anybody can help I will greatly appreciate it and give you credit in the CarPC community.

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