Jump to content

Serial Port /COM Port UDF


martin
 Share

Recommended Posts

New version uploaded.

Added _CommReadByteArray and _CommSendByteArray.

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

One more,

how do I send the escape character to the COM port?

Gert

You just need to know the ASCII code for Escape. It's 0x1B, or dec 27.

Any of these should work, but if not let me know.

_CommSendByte(27,0)

or

_CommSendByte(0x1B,0)

or

_CommSendString(Chr(27),0)

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 could definitely use multiple com ports to control multiple loaders (cd duplicators). And I also really appreciate the new flow control setting, very useful for the loaders as they typically only have 2 or 3 wires wired up to the serial port.

Martin,

Technically, I'm not using the multiple port feature here (instead, I have multiple invocations of my exe, each with separate copies of the dll - one per robot), but I thought you'd be amused to see the harnessing of your library in action. The video quality is poor (recorded in a dimly lit room with my photo camera in video mode after a too much caffeine coding session), but it's fun anyway (for me at least):

http://www.youtube.com/watch?v=5YYfQiV9OQA

Thanks again, martin!

-brendan

Link to comment
Share on other sites

Martin,

Technically, I'm not using the multiple port feature here (instead, I have multiple invocations of my exe, each with separate copies of the dll - one per robot), but I thought you'd be amused to see the harnessing of your library in action. The video quality is poor (recorded in a dimly lit room with my photo camera in video mode after a too much caffeine coding session), but it's fun anyway (for me at least):

http://www.youtube.com/watch?v=5YYfQiV9OQA

Thanks again, martin!

-brendan

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.

If the CD's used USB then you could in theory have one cable fom the PC to control both the robot and the CD writer by have a USB to serial interface.

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

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

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.

Yup, the robots no nothing about the drives, other than locations. I have to manage tray open/close stuff separately (though my "driver" co-ordinates it all). You could put a couple of correct-height external optical writers facing either robot and double the number of drives each serves, for example.

The left-side (stage right) robot is a "multi-bank" robot, different commands to feed the two drives. Some multi-bank units have 2-6 drives per bank, with 2-4 banks. That keeps the robotic arm pretty busy. Trying to code to handle everything. :P

If the CD's used USB then you could in theory have one cable fom the PC to control both the robot and the CD writer by have a USB to serial interface.

Some use USB, some use firewire. The sad truth is that most firewire bridges handle the complex CDBs much better than the average USB bridge. Most pro duplicator machines use firewire. ATAPI and SATA still have that windows driver problem where the ATA driver drops down to PIO mode on lots of IO errors (assuming it's a cable problem). OK for hard drives, sucks for optical drives which give IO errors on scratches.

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

Yes, there's not much action going on there. Again, it was spur of the moment, just wanted to share my sense of victory after a coding session of trying to work around what happens when some drives don't recognize that a disc is in the drive. Using my SPTI autoit code, if it takes > a normal amount of time for the disctype to get reported as something other than 00 (unknown), I assume there's a disc that needs immediate rejection. I have some data CD-Rs that my pioneer DVD drive do not recognize and, to some calling programs, that appears to look like an empty drive. Not good, ends up causing double loading, so needed a way to preempt that.

Thanks again. :)

-brendan

Link to comment
Share on other sites

  • 1 month later...

Ok, I got to do some testing and this does work with GPS and 64 bit windows.

How would you suggest checking to see if a com port still exists? like when the gps gets unplugged without getting turned off. Maybe do a comport listing first?

Right now I am doing this. (this is still a very rough draft...adapted from my old netcomm function)

Func _GetGPS()
    $LatTest = 0
    $timeout = TimerInit()
    $return = 1
    $maxtime = GUICtrlRead($sleeptime) * 0.8
    If $maxtime < 800 Then $maxtime = 800

    While 1
        $dataline = ''
        If _CommPortConnection() <> '' Then
            $dataline = StringStripWS(_CommGetLine(), 8)
        Else
            $return = 0
            _GpsToggle()
            SoundPlay($SoundDir & $error_sound, 0)
        EndIf
        ConsoleWrite($dataline & @CRLF)
        If StringInStr($dataline, "GPGGA") Then
            _GetGpsData($dataline)
            If $LatTest = 1 Then
                $refresh_gps = TimerInit()
                ExitLoop
            EndIf
        Else
            Sleep($maxtime / 100)
        EndIf
        If TimerDiff($timeout) > $maxtime Then ExitLoop
    WEnd
    If $LatTest = 0 Then GUICtrlSetData($msgdisplay, 'GPS Timeout')
    Return ($return)
EndFunc;==>_GetGPS

Func _GetGpsData($GpsData);Read GPS Receiver Data
    GUICtrlSetData($msgdisplay, $GpsData)
    $data = StringSplit($GpsData, ",")
    If $data[0] > 9 Then
        $lat = StringSplit($data[3], ".")
        $lon = StringSplit($data[5], ".")
        If _ArrayMax($lat, 0, 1) >= 2 And _ArrayMax($lat, 0, 1) >= 2 Then
;Convert to Min/Sec to Decimal Latitude
            $latD = StringMid((StringRight($lat[1], 2) & "." & StringRight($lat[2], 4)) / 60, 2, 10)
;Convert to Min/Sec to Decimal Longitude
            $lonD = StringMid((StringRight($lon[1], 2) & "." & StringRight($lon[2], 4)) / 60, 2, 10)
;Set Location Variables
            $ufLat = StringLeft($lat[1], StringLen($lat[1]) - 2) & $latD
            $ufLon = StringLeft($lon[1], StringLen($lon[1]) - 2) & $lonD
            $Latitude = $data[4] & ' ' & StringFormat('%0.7f', $ufLat);set latitude
            $Longitude = $data[6] & ' ' & StringFormat('%0.7f', $ufLon);set longitude
            If $Latitude <> '' Then $LatTest = 1
        EndIf
    EndIf
EndFunc;==>_GetGpsData

But autoit is crashing with the following error when I unplug the GPS. (not sure if this error means anything to you, but here it is anyway)

Problem signature:

Problem Event Name: APPCRASH

Application Name: autoit3.exe

Application Version: 3.2.9.11

Application Timestamp: 473c3c12

Fault Module Name: kernel32.dll

Fault Module Version: 6.0.6000.16386

Fault Module Timestamp: 4549be94

Exception Code: 0eedfade

Exception Offset: 00023843

OS Version: 6.0.6000.2.0.0.256.4

Locale ID: 1033

Additional Information 1: 8d13

Additional Information 2: cdca9b1d21d12b77d84f02df48e34311

Additional Information 3: 8d13

Additional Information 4: cdca9b1d21d12b77d84f02df48e34311

Read our privacy statement:

http://go.microsoft.com/fwlink/?linkid=501...mp;clcid=0x0409

Edited by ACalcutt

Andrew Calcutt

Http://www.Vistumbler.net

Http://www.TechIdiots.net

Its not an error, its a undocumented feature

Link to comment
Share on other sites

Ok, I got to do some testing and this does work with GPS and 64 bit windows.

.

.

.

.

But autoit is crashing with the following error when I unplug the GPS. (not shure if this error means anything to you, but here it is anyway)

The dll doesn't check that the com port still exists after it has opened it and I hadn't thought about the situation. So yes, you could check that the com port exists with _CommListPorts but I doubt this will really help because the dll could be in the middle of reading data when the COM port is lost.

I will have a look at the dll, but not few a few days, to see how I can prevent a crash if the COM port disappears or fails. I anticipate that I just need to make a better job of handling errors. Meanwhile I would recommend you simply stop your script if possible before the gps is unplugged.

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 have little problem. How sms send with AT commands and commMG.au3 ?

I don't know now how send <ctrl-z> in _CommSendString

Command at send sms.

HyperTerminal.exe

at+cmgs="number"<CR>

message<ctrl-z><CR>

AutoIt

$AT_1 = ('AT+CMGS="')
$num_1 = ('+48666777888"'&@CR)
$part_0 = (&$AT_1""&$num_1)
$part_1 = ("autoIt"&@CR);without <ctrl-z> ?
$all = (&$part_0""&$part_1)
$time_1 = 1

;MsgBox(0,"",""&$all)
_CommSendString($all,$time_1)

Feed Polish children: pajacyk - just go to this page and close it. Thanks. World website: free rice.

Link to comment
Share on other sites

The dll doesn't check that the com port still exists after it has opened it and I hadn't thought about the situation. So yes, you could check that the com port exists with _CommListPorts but I doubt this will really help because the dll could be in the middle of reading data when the COM port is lost.

I will have a look at the dll, but not few a few days, to see how I can prevent a crash if the COM port disappears or fails. I anticipate that I just need to make a better job of handling errors. Meanwhile I would recommend you simply stop your script if possible before the gps is unplugged.

I tried to check for avalible com ports first but it didn't seem to help :-(. I look forward to an update if it is possible to fix :-). Great work so far on this. Just having x64 support is great right now.

I agree with your temporary work around, but sometimes having a crash can be annoying...turning off gps is an easy thing to forget

I made a test script if you want it, This program just writes information to the console in scite.

GPS_DATA.au3

Edited by ACalcutt

Andrew Calcutt

Http://www.Vistumbler.net

Http://www.TechIdiots.net

Its not an error, its a undocumented feature

Link to comment
Share on other sites

I have little problem. How sms send with AT commands and commMG.au3 ?

I don't know now how send <ctrl-z> in _CommSendString

Command at send sms.

HyperTerminal.exe

at+cmgs="number"<CR>

message<ctrl-z><CR>

AutoIt

$AT_1 = ('AT+CMGS="')
$num_1 = ('+48666777888"'&@CR)
$part_0 = (&$AT_1""&$num_1)
$part_1 = ("autoIt"&@CR);without <ctrl-z> ?
$all = (&$part_0""&$part_1)
$time_1 = 1

;MsgBox(0,"",""&$all)
_CommSendString($all,$time_1)
If you want to send Ctrl Z then there are at least 2 ways you can do it.

==========A================

$string = "something" & Chr(26) & @CR

_CommSendString($string,1)

==========B================

$string = "something"

_CommSendString($string)

_CommSendByte(26);ascii code for ctrl z

_CommSendString(@CR,1)

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 tried to check for avalible com ports first but it didn't seem to help :-(. I look forward to an update if it is possible to fix :-). Great work so far on this. Just having x64 support is great right now.

I agree with your temporary work around, but sometimes having a crash can be annoying...turning off gps is an easy thing to forget

I made a test script if you want it, This program just writes information to the console in scite.

Are these USB-connected virtual com ports? If so, I think this is substantially outside of the scope of the DLL and UDF. From my experience, Windows itself (or the driver(s) that XP SP2 shipped with) does not handle the loss/disconnect of an already-opened virtual com port well at all. I've had both BSODs and devices that failed to work when plugged back in (until the next reboot) in these situations.

e.g. how well does Hyperterminal handle the same situation that martin's code doesn't handle?

-brendan

Link to comment
Share on other sites

This udf seems to work perfectly for what I'm trying to do, but I'm very new to working with serial ports and I'm having a problem. I'm sending commands to a hardware device to toggle generator lines through a serial port instead of wired switches. So far I'm just writing a quick test to make sure I can get it to work. Here is what I have:

#include "CommMG.au3"

$error = ""

_CommSetPort(1, $error, 38400, 8, 0, 1, 1)
_CommSendString("setinp 8e", 1)

_CommCloseport()

I've tested to make sure I'm connecting to the port properly, I've just removed all of the unimportant code. When I send the command in hyperterminal, I immediately see the input LEDs on the hardware light up. When I send the same command using AutoIt, the lights remain unlit until I connect to hyperterminal and press the enter key. I've tried adding {ENTER} at the end of my input string, but that's not the problem. What is hyperterminal doing that my script is not?

Edited by orestes72
Link to comment
Share on other sites

This udf seems to work perfectly for what I'm trying to do, but I'm very new to working with serial ports and I'm having a problem. I'm sending commands to a hardware device to toggle generator lines through a serial port instead of wired switches. So far I'm just writing a quick test to make sure I can get it to work. Here is what I have:

#include "CommMG.au3"

$error = ""

_CommSetPort(1, $error, 38400, 8, 0, 1, 1)
_CommSendString("setinp 8e", 1)

_CommCloseport()

I've tested to make sure I'm connecting to the port properly, I've just removed all of the unimportant code. When I send the command in hyperterminal, I immediately see the input LEDs on the hardware light up. When I send the same command using AutoIt, the lights remain unlit until I connect to hyperterminal and press the enter key. I've tried adding {ENTER} at the end of my input string, but that's not the problem. What is hyperterminal doing that my script is not?

Try one of these two substitutions:

_CommSendString("setinp 8e" & @CR, 1)

or

_CommSendString("setinp 8e" & @CRLF, 1)

Probably the first one.

-brendan

Link to comment
Share on other sites

is there a way to see what baudrate etc your serial connection need to make sucsesful a connection to remote site somthing like a automatisc configure and connection tester?

Not automatically with my UDF I'm afraid.

If the remote connection echoes back what you send then you could write something to try a baud rate, send a character and compare with a reply if one comes within a certain time. Then you could try another baud rate and so on.

It's not just the baud rate which is important though. You need to get the number of data bits, (Usually 8) the number of stop bits (Usually 1) and the parity (usually none or odd), but I've used terminals set for 7 data bits, 2 stop bits and even parity so guessing is unlikely to work.

Might be easier to phone the remote site and ask them!

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

Are these USB-connected virtual com ports? If so, I think this is substantially outside of the scope of the DLL and UDF. From my experience, Windows itself (or the driver(s) that XP SP2 shipped with) does not handle the loss/disconnect of an already-opened virtual com port well at all. I've had both BSODs and devices that failed to work when plugged back in (until the next reboot) in these situations.

e.g. how well does Hyperterminal handle the same situation that martin's code doesn't handle?

-brendan

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 10 seconds, turn off gps
                $disconnected_time = -1
                $return = 0
                _GpsToggle()
                SoundPlay($SoundDir & $error_sound, 0)
            EndIf
        EndIf
Edited by ACalcutt

Andrew Calcutt

Http://www.Vistumbler.net

Http://www.TechIdiots.net

Its not an error, its a undocumented feature

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