Jump to content

Com Port Redirector


Recommended Posts

Has anyone come across autoit code to take the input from a physical com port and redirect it out of a second physical com port. I have a terminal server that I want to take GPS data in on one port and then distribute it out of 8 additional serial ports. I need to be able to remotely connect and disconnect the GPS data for some tests I want to run.

I have a purchased piece of software that claims to be able to do this. I can input on Com 3 and redirect to Com 11 and see both sets of data on Hyperterminal, but nothing physically comes out Com 11, so the data is being passed to the application layer and not down to the physical layer. I also have a second piece of purchased software that allows redirection to virtual Com ports which is absolutely no use to me.

I saw a Serial Port UDF and was contemplating playing around with it, but if someone has something already, that would be a fantastic start

Link to comment
Share on other sites

Has anyone come across autoit code to take the input from a physical com port and redirect it out of a second physical com port. I have a terminal server that I want to take GPS data in on one port and then distribute it out of 8 additional serial ports. I need to be able to remotely connect and disconnect the GPS data for some tests I want to run.

I have a purchased piece of software that claims to be able to do this. I can input on Com 3 and redirect to Com 11 and see both sets of data on Hyperterminal, but nothing physically comes out Com 11, so the data is being passed to the application layer and not down to the physical layer. I also have a second piece of purchased software that allows redirection to virtual Com ports which is absolutely no use to me.

I saw a Serial Port UDF and was contemplating playing around with it, but if someone has something already, that would be a fantastic start

What you describe sounds fairly easy and it could be done in less than 20 lines of code I would guess. You simply have to read whatever coms in on COM 3 and send it straight out on COM 11, and anything that comes in on COM11 gets sent out on COM3. You could do that with the udf in my signature. If the speed is fast and synchronisation is very important then it might be difficult, but I would expect that for a gps device you should have no problems.

Are you sure COM 11 operates correctly? (Remeber that above COM 9 you might need to address the port as "\\COM DD" not "COM DD" depending on the software you are using.)

DaleHohm also has a serial coms udf here; I don't know which one would be easier to use for your application.

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

What you describe sounds fairly easy and it could be done in less than 20 lines of code I would guess. You simply have to read whatever coms in on COM 3 and send it straight out on COM 11, and anything that comes in on COM11 gets sent out on COM3. You could do that with the udf in my signature. If the speed is fast and synchronisation is very important then it might be difficult, but I would expect that for a gps device you should have no problems.

Are you sure COM 11 operates correctly? (Remeber that above COM 9 you might need to address the port as "\\COM DD" not "COM DD" depending on the software you are using.)

DaleHohm also has a serial coms udf here; I don't know which one would be easier to use for your application.

Hmmm, having analyzed this a little further it may not be as simple as you describe. Coms 3 through 18 are on a Terminal server that communicate with the target PC via IP. So I would have to do the turn around at the transport layer perhaps, (TCP???). The ports appear to function as physical ports, in that if I pass the GPS data in on Port 1 through 16 of the Port Server (Com 3 through 18), I see it in Hyperterminal/Procomm.

I noted that someone already has Server/Client code out there (can't remember who it was but will post their link when I find it), that takes data in from a serial port on one machine and then passes it via TCP/IP to another machine. I'm thinking that this approach may work, but compile the server client as a single entity and run on the same machine. Then work out how to turn the TCP layer back around to the physical on the new Com port. Thoughts???

Edited by eohalloran
Link to comment
Share on other sites

Hmmm, having analyzed this a little further it may not be as simple as you describe. Coms 3 through 18 are on a Terminal server that communicate with the target PC via IP. So I would have to do the turn around at the transport layer perhaps, (TCP???). The ports appear to function as physical ports, in that if I pass the GPS data in on Port 1 through 16 of the Port Server (Com 3 through 18), I see it in Hyperterminal/Procomm.

I noted that someone already has Server/Client code out there (can't remember who it was but will post their link when I find it), that takes data in from a serial port on one machine and then passes it via TCP/IP to another machine. I'm thinking that this approach may work, but compile the server client as a single entity and run on the same machine. Then work out how to turn the TCP layer back around to the physical on the new Com port. Thoughts???

If the ports function as physical ports on the local machine then I don't see that there should be a problem. I don't see that you need to worry about the fact that they are really sending data by TCPIP. It's the same with a USB to RS232 converter. You communicate with it as a normal COM port but the data is actually going over USB but it doesn't matter.

If you look in control panel| System|hardware|Device Manager then Ports (COM and LPT) and you can see the COM ports (COM 3 through 18) then you should have no problem using a COM udf with any of those ports, providing of course that no other program is already using 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

Hi,

I think I can help you.

Sorry, this is quick and dirty code, but works for me.

This is the ini-File you have to adapt:

Filename: cfxbin2.ini (I cannot upload it)

[Comm]
CommPort1=1
CommPort2=2
CommBaud1=9600
CommBaud2=9600

;Parity 0=none, 1=odd, 2=even, 3=mark, 4=space
CommParity1=0 
CommParity2=0 
;Bits 4-8

CommBits1=8
CommBits2=8

;Stop 0=1, 1=1,5, 2=2
CommStop1=0
CommStop2=0

;Bitfield
CommCtrl1=0x0011
CommCtrl2=0x0011

LogDir=
Debug=0

cfxbin2.au3

cfxbin2test.au3

Link to comment
Share on other sites

Sorry, this is quick and dirty code, but works for me.

Very good Uwe; maybe some credit due to TABALtd's thread here?

But since the function _opencomm1 has the port as a parameter, you didn't need to duplicate it so that you could open a second port. And with the other duplicated functions, maybe it would have been better to improve them so that they were able to be used for any port. If you did that you could post improvements in TABALtd's thread and make a useful contribution to the code he started but which needs more attention IMO. (I'm especially guilty of being someone who could have added to it but hasn't.)

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

Thanks for the responses. The problem I am having now is having more than one Com Port open at the time. Looking in the Commg UDF, there is a _CommSwitch function that allows switching between com ports, but when I use this, it takes focus away from the original com port, so cosequently, when I set one port up and switch to the next, I am left with only one configured port.

I've uploaded what I have so far. I've icluded the commMG.au3 and the commg.dll needed

EDIT - As requested files removed and link to original UDF is:

http://www.autoitscript.com/forum/index.php?showtopic=45842

I'm interested in trying the ini approach that Uwe suggests, but I would need to edit it to contain 18 com ports. I'm also going to try to persevere with this script and if I get it to work, I'll add the 18 com port connections and maybe some method of removing used com ports

eo

Edit --- If you try to run this on a machine with only one com port, it'll throw an exception (something else I'll need to add!!!)

BTW, I put the second window in to monitor that I was getting input , so consequently as it stands now, if I send something in on the second com port it'll appear in the bottom window. If I comment out the _CommSwitch(2) stuff, I can see the input on the first com port, so I know data is getting to the application

Oh, and Martin you were perfectly correct, the com ports behave as though they are physically attached to the PC

com_port_redirector.au3

Edited by eohalloran
Link to comment
Share on other sites

Thanks for the responses. The problem I am having now is having more than one Com Port open at the time. Looking in the Commg UDF, there is a _CommSwitch function that allows switching between com ports, but when I use this, it takes focus away from the original com port, so cosequently, when I set one port up and switch to the next, I am left with only one configured port.

I've uploaded what I have so far. I've icluded the commMG.au3 and the commg.dll needed

I'm interested in trying the ini approach that Uwe suggests, but I would need to edit it to contain 18 com ports. I'm also going to try to persevere with this script and if I get it to work, I'll add the 18 com port connections and maybe some method of removing used com ports

eo

Edit --- If you try to run this on a machine with only one com port, it'll throw an exception (something else I'll need to add!!!)

BTW, I put the second window in to monitor that I was getting input , so consequently as it stands now, if I send something in on the second com port it'll appear in the bottom window. If I comment out the _CommSwitch(2) stuff, I can see the input on the first com port, so I know data is getting to teh application

Oh, and Martin you were perfectly correct, the com ports behave as though they are physically attached to the PC

I don't have time to study your code now so I'll look at it tonight (UK). But you are reading data from one com port and sending it back on the same port. Is that what you meant to do?

The udf only allows for up to 4 COM ports. You are wrong that you have only one configured port when you use comswitch, all opened ports should remain open and configured.

A small request eohalloran, any improvements and corrections I make to the udf in the future then the udf code and dll you attached will be out of date. Could you change those to a link to the udf thread?

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

Martin,

Yes I intend to take data in on one Com port and send it out on another. My purpose is that I want to be able to take 4 GPS signals in on 4 seperate Com ports and then send them to some devices on 4 other Com ports. I then want to be able to make and break the connections between the Com ports to simulate GPS information being lost, so my devices automatically switch over to SNTP. So ultimately I will have a second radio button to disconnect the connectios as well. I'm a complete Novice at this Autoit stuff, but I am keen to learn and I have dug around on previous postings to get bits and pieces of code. In fact it's your UDF that started me on this path.

As requested the link to the UDF is http://www.autoitscript.com/forum/index.php?showtopic=45842

Best Rgds

eo

Edited by eohalloran
Link to comment
Share on other sites

OK, so it turns out I made a typo and a few small errors. I now have this working where I can take input on one Com port and output it on a second Com port and vice versa (i.e. it is now bidirectional). There are 2 things I want to do now. Firstly get the Disconnect button working; this is key because my goal is to be able to connect and disconnect at will. I have a disconnect function but I can't call it from an appropriate place for it to do something. I've tried placing it within the various loops, the obvious one was where I am sending data out, but that didn't work so for now it is in my Events function until I can figure that out.

Then ultimately I want to have multiple connection/disconnections.

Here's what I have so far"

#include <GUIConstants.au3>
#include <CommMG.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>

$Main = GUICreate("Com Port Redirector - Com Port Configuration", 400, 150)
GUISetFont(10, 400, "Times New Roman")
$cmboPortsAvailable1 = GUICtrlCreateCombo("", 110, 75, 75, 25)
$lblPort1 = GUICtrlCreateLabel("Connect Port", 30, 78, 80, 25)
$cmboPortsAvailable2 = GUICtrlCreateCombo("", 280, 75, 75, 25)
$lblPort2 = GUICtrlCreateLabel("To Port", 230, 78, 50, 25)
$lblBaud = GUICtrlCreateLabel("Baud Rate for both Ports", 124, 10, 160, 25)
$CmBoBaud = GUICtrlCreateCombo("", 160, 30, 70, 25)
GUICtrlSetData(-1, "50|75|110|150|600|1200|1800|2000|2400|3600|4800|7200|9600|10400|14400|15625|19200|28800|38400|56


000|57600|115200|", "4800")
$btnConnect = GUICtrlCreateButton("Connect Com Ports", 130, 120, 140, 25)

GUISetState(@SW_SHOW, $Main)

GUISetOnEvent($GUI_EVENT_CLOSE, "Bye")
$Main1 = GUICreate("Com Port Redirector - Monitor Window", 600, 200)
$lblBaud1 = GUICtrlCreateLabel("", 50, 10, 150, 25)
$lblPort3 = GUICtrlCreateLabel(GUICtrlRead($cmboPortsAvailable1), 10, 10, 35, 25)
$monitor1 = GUICtrlCreateEdit("", 10, 30, 550, 40, BitOR($WS_EX_WINDOWEDGE, $ES_READONLY, $ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL))
$lblPort4 = GUICtrlCreateLabel(GUICtrlRead($cmboPortsAvailable2), 10, 90, 35, 25)
$lblBaud2 = GUICtrlCreateLabel("", 50, 90, 150, 25)
$monitor2 = GUICtrlCreateEdit("", 10, 110, 550, 40, BitOR($WS_EX_WINDOWEDGE, $ES_READONLY, $ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL))
$btnDisconnect = GUICtrlCreateButton("Disconnect Com Ports", 230, 160, 140, 25)

GetPorts()
SetupComPorts()

GUISetState(@SW_HIDE, $Main)
GUISetState(@SW_SHOW, $Main1)

Events()
_CommClearOutputBuffer()
_CommClearInputBuffer()

While 1
    _CommSwitch(1)
    $instr = _CommGetline(@CR, 1000, 200)
    GUICtrlSetData($monitor1, $instr)
    _CommSwitch(2)
    _CommSendString($instr)
    $instr = _CommGetline(@CR, 1000, 200)
    GUICtrlSetData($monitor2, $instr)
    _CommSwitch(1)
    _CommSendString($instr)

WEnd


Func Events()
    $msg = GUIGetMsg()
        If $msg = $btnDisconnect Then 
            Disconnect()
        EndIf
    Opt("GUIOnEventMode", 1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Bye")
EndFunc;==>Events


Func Bye()
    _Commcloseport()
    Exit
EndFunc;==>Bye

Func GetPorts();Find all com ports
  $portlist = _CommListPorts(0);find the available COM ports and write them into the ports combo
  If @error = 1 Then
    MsgBox(0, 'trouble getting portlist', 'Program will terminate!')
    Exit
  EndIf
  If IsArray($portlist) then
    For $pl = 1 To $portlist[0]
    GUICtrlSetData($cmboPortsAvailable1, $portlist[$pl]);_CommListPorts())
    GUICtrlSetData($cmboPortsAvailable2, $portlist[$pl]);_CommListPorts())
    Next
  Else
    MsgBox(262144,"ERROR", "No COM ports found on this PC.")
  EndIf
  EndFunc;==>GetPorts


Func SetupComPorts();Open com port
    Local $sportSetError
    $baud = GUICtrlRead($CmBoBaud)
    GUICtrlSetData($lblBaud1, $baud & " bps  -  Monitor 1")
    GUICtrlSetData($lblBaud2, $baud & " bps  -  Monitor 2")
    While 1
        Sleep(50)
        $msg = GUIGetMsg()
        If $msg = -3 Then; Exit button on GUI
            Exit
        EndIf
        If $msg = $btnConnect Then
            Sleep(50)
            _CommSwitch(1)
            $setport1 = StringReplace(GUICtrlRead($cmboPortsAvailable1), 'COM', '')
            _CommSetPort($setport1, $sportSetError, $baud, 8, 0, 1, 0)
            GUICtrlSetData($lblPort3, GUICtrlRead($cmboPortsAvailable1))
            _CommSwitch(2)
            $setport2 = StringReplace(GUICtrlRead($cmboPortsAvailable2), 'COM', '')
            _CommSetPort($setport2, $sportSetError, $baud, 8, 0, 1, 0)
            GUICtrlSetData($lblPort4, GUICtrlRead($cmboPortsAvailable2))
            If @error Then MsgBox(0, 'Setport error = ', $sportSetError)
                ExitLoop
        EndIf
    WEnd
EndFunc;==>SetupComPorts

Func Disconnect()
    _CommSwitch(1)
    _CommClosePort()
    _CommSwitch(2)
    _CommClosePort()
EndFunc;==>Disconnect

Rgds

eo

Edited by eohalloran
Link to comment
Share on other sites

OK, so it turns out I made a typo and a few small errors. I now have this working where I can take input on one Com port and output it on a second Com port and vice versa (i.e. it is now bidirectional). There are 2 things I want to do now. Firstly get the Disconnect button working; this is key because my goal is to be able to connect and disconnect at will. I have a disconnect function but I can't call it from an appropriate place for it to do something. I've tried placing it within the various loops, the obvious one was where I am sending data out, but that didn't work so for now it is in my Events function until I can figure that out.

Then ultimately I want to have multiple connection/disconnections.

Here's what I have so far"

#include <GUIConstants.au3>
 #include <CommMG.au3>
 #include <GuiEdit.au3>
 #include <WindowsConstants.au3>
 
 $Main = GUICreate("Com Port Redirector - Com Port Configuration", 400, 150)
 GUISetFont(10, 400, "Times New Roman")
 $cmboPortsAvailable1 = GUICtrlCreateCombo("", 110, 75, 75, 25)
 $lblPort1 = GUICtrlCreateLabel("Connect Port", 30, 78, 80, 25)
 $cmboPortsAvailable2 = GUICtrlCreateCombo("", 280, 75, 75, 25)
 $lblPort2 = GUICtrlCreateLabel("To Port", 230, 78, 50, 25)
 $lblBaud = GUICtrlCreateLabel("Baud Rate for both Ports", 124, 10, 160, 25)
 $CmBoBaud = GUICtrlCreateCombo("", 160, 30, 70, 25)
 GUICtrlSetData(-1, "50|75|110|150|600|1200|1800|2000|2400|3600|4800|7200|9600|10400|14400|15625|19200|28800|38400|56


 
 000|57600|115200|", "4800")
 $btnConnect = GUICtrlCreateButton("Connect Com Ports", 130, 120, 140, 25)
 
 GUISetState(@SW_SHOW, $Main)
 
 GUISetOnEvent($GUI_EVENT_CLOSE, "Bye")
 $Main1 = GUICreate("Com Port Redirector - Monitor Window", 600, 200)
 $lblBaud1 = GUICtrlCreateLabel("", 50, 10, 150, 25)
 $lblPort3 = GUICtrlCreateLabel(GUICtrlRead($cmboPortsAvailable1), 10, 10, 35, 25)
 $monitor1 = GUICtrlCreateEdit("", 10, 30, 550, 40, BitOR($WS_EX_WINDOWEDGE, $ES_READONLY, $ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL))
 $lblPort4 = GUICtrlCreateLabel(GUICtrlRead($cmboPortsAvailable2), 10, 90, 35, 25)
 $lblBaud2 = GUICtrlCreateLabel("", 50, 90, 150, 25)
 $monitor2 = GUICtrlCreateEdit("", 10, 110, 550, 40, BitOR($WS_EX_WINDOWEDGE, $ES_READONLY, $ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL))
 $btnDisconnect = GUICtrlCreateButton("Disconnect Com Ports", 230, 160, 140, 25)
 
 GetPorts()
 SetupComPorts()
 
 GUISetState(@SW_HIDE, $Main)
 GUISetState(@SW_SHOW, $Main1)
 
 Events()
 _CommClearOutputBuffer()
 _CommClearInputBuffer()
 
 While 1
     _CommSwitch(1)
     $instr = _CommGetline(@CR, 1000, 200)
     GUICtrlSetData($monitor1, $instr)
     _CommSwitch(2)
     _CommSendString($instr)
     $instr = _CommGetline(@CR, 1000, 200)
     GUICtrlSetData($monitor2, $instr)
     _CommSwitch(1)
     _CommSendString($instr)
 
 WEnd
 
 
 Func Events()
     $msg = GUIGetMsg()
         If $msg = $btnDisconnect Then 
             Disconnect()
         EndIf
     Opt("GUIOnEventMode", 1)
     GUISetOnEvent($GUI_EVENT_CLOSE, "Bye")
 EndFunc;==>Events
 
 
 Func Bye()
     _Commcloseport()
     Exit
 EndFunc;==>Bye
 
 Func GetPorts();Find all com ports
   $portlist = _CommListPorts(0);find the available COM ports and write them into the ports combo
   If @error = 1 Then
     MsgBox(0, 'trouble getting portlist', 'Program will terminate!')
     Exit
   EndIf
   If IsArray($portlist) then
     For $pl = 1 To $portlist[0]
     GUICtrlSetData($cmboPortsAvailable1, $portlist[$pl]);_CommListPorts())
     GUICtrlSetData($cmboPortsAvailable2, $portlist[$pl]);_CommListPorts())
     Next
   Else
     MsgBox(262144,"ERROR", "No COM ports found on this PC.")
   EndIf
   EndFunc;==>GetPorts
 
 
 Func SetupComPorts();Open com port
     Local $sportSetError
     $baud = GUICtrlRead($CmBoBaud)
     GUICtrlSetData($lblBaud1, $baud & " bps  -  Monitor 1")
     GUICtrlSetData($lblBaud2, $baud & " bps  -  Monitor 2")
     While 1
         Sleep(50)
         $msg = GUIGetMsg()
         If $msg = -3 Then; Exit button on GUI
             Exit
         EndIf
         If $msg = $btnConnect Then
             Sleep(50)
             _CommSwitch(1)
             $setport1 = StringReplace(GUICtrlRead($cmboPortsAvailable1), 'COM', '')
             _CommSetPort($setport1, $sportSetError, $baud, 8, 0, 1, 0)
             GUICtrlSetData($lblPort3, GUICtrlRead($cmboPortsAvailable1))
             _CommSwitch(2)
             $setport2 = StringReplace(GUICtrlRead($cmboPortsAvailable2), 'COM', '')
             _CommSetPort($setport2, $sportSetError, $baud, 8, 0, 1, 0)
             GUICtrlSetData($lblPort4, GUICtrlRead($cmboPortsAvailable2))
             If @error Then MsgBox(0, 'Setport error = ', $sportSetError)
                 ExitLoop
         EndIf
     WEnd
 EndFunc;==>SetupComPorts
 
 Func Disconnect()
     _CommSwitch(1)
     _CommClosePort()
     _CommSwitch(2)
     _CommClosePort()
 EndFunc;==>Disconnect

Rgds

eo

Looks good to me!

I've messed around with it a bit but haven't fully tested

#include <GUIConstants.au3>
#include <CommMG.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

$Main = GUICreate("Com Port Redirector - Com Port Configuration", 400, 150)
GUISetFont(10, 400, "Times New Roman")
$cmboPortsAvailable1 = GUICtrlCreateCombo("", 110, 75, 75, 25)
$lblPort1 = GUICtrlCreateLabel("Connect Port", 30, 78, 80, 25)
$cmboPortsAvailable2 = GUICtrlCreateCombo("", 280, 75, 75, 25)
$lblPort2 = GUICtrlCreateLabel("To Port", 230, 78, 50, 25)
$lblBaud = GUICtrlCreateLabel("Baud Rate for both Ports", 124, 10, 160, 25)
$CmBoBaud = GUICtrlCreateCombo("", 160, 30, 70, 25)
GUICtrlSetData(-1, "50|75|110|150|600|1200|1800|2000|2400|3600|4800|7200|9600|10400|14400|15625|19200|28800|38400|56

000|57600|115200|", "4800")
$btnConnect = GUICtrlCreateButton("Connect Com Ports", 130, 120, 140, 25)
GUICtrlSetOnEvent(-1, "SetupComPorts")
GUISetState(@SW_SHOW, $Main)
GUISetOnEvent($GUI_EVENT_CLOSE, "Bye")
GetPorts()
Global $setPorts = False

While Not $setPorts
    Sleep(30)
WEnd

$baudrate = GUICtrlRead($CmBoBaud)
$port1 = GUICtrlRead($cmboPortsAvailable1)
$port2 = GUICtrlRead($cmboPortsAvailable2)

GUIDelete($Main)

$Main1 = GUICreate("Com Port Redirector - Monitor Window", 600, 200)
$lblBaud1 = GUICtrlCreateLabel("", 50, 10, 150, 25)
$lblPort3 = GUICtrlCreateLabel(GUICtrlRead($cmboPortsAvailable1), 10, 10, 35, 25)
$monitor1 = GUICtrlCreateEdit("", 10, 30, 550, 40, BitOR($WS_EX_WINDOWEDGE, $ES_READONLY, $ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL))
$lblPort4 = GUICtrlCreateLabel(GUICtrlRead($cmboPortsAvailable2), 10, 90, 35, 25)
$lblBaud2 = GUICtrlCreateLabel("", 50, 90, 150, 25)
$monitor2 = GUICtrlCreateEdit("", 10, 110, 550, 40, BitOR($WS_EX_WINDOWEDGE, $ES_READONLY, $ES_AUTOVSCROLL, $ES_MULTILINE, $WS_VSCROLL))
$btnDisconnect = GUICtrlCreateButton("Disconnect Com Ports", 230, 160, 140, 25)
GUICtrlSetOnEvent(-1, "Disconnect")
GUISetOnEvent($GUI_EVENT_CLOSE, "Bye")

GUISetState(@SW_SHOW, $Main1)
SetupComPorts2($baudrate, $port1, $port2)

Global $inStr = ''

While 1
    $inStr = PushPull(1, $inStr)
    GUICtrlSetData($monitor1, $inStr)

    $inStr = PushPull(2, $inStr)
    GUICtrlSetData($monitor2, $inStr)

WEnd

Func PushPull($chan, $tosend)
    _CommSwitch($chan)
    if $tosend <> '' then _CommSendString($tosend)
    Return _CommGetline(@CR, 1000, 200)
EndFunc  ;==>PushPull


Func Bye()
    Disconnect()
  _ComClosePort()
    Exit
EndFunc  ;==>Bye

Func GetPorts();Find all com ports
    $portlist = _CommListPorts(0);find the available COM ports and write them into the ports combo
    If @error = 1 Then
        MsgBox(0, 'trouble getting portlist', 'Program will terminate!')
        Exit
    EndIf
    If IsArray($portlist) Then
        For $pl = 1 To $portlist[0]
            GUICtrlSetData($cmboPortsAvailable1, $portlist[$pl]);_CommListPorts())
            GUICtrlSetData($cmboPortsAvailable2, $portlist[$pl]);_CommListPorts())
        Next
    Else
        MsgBox(262144, "ERROR", "No COM ports found on this PC.")
    EndIf
EndFunc  ;==>GetPorts


Func SetupComPorts();Open com port
    $setPorts = True
EndFunc  ;==>SetupComPorts

Func SetupComPorts2($baud, $ser1, $ser2);Open com ports
    Local $sportSetError

    GUICtrlSetData($lblBaud1, $baud & " bps  -  Monitor 1")
    GUICtrlSetData($lblBaud2, $baud & " bps  -  Monitor 2")

    _CommSwitch(1)
    $setport1 = StringReplace($ser1, 'COM', '')
    _CommSetPort($setport1, $sportSetError, $baud, 8, 0, 1, 0)
    GUICtrlSetData($lblPort3, $ser1)
    _CommSwitch(2)
    $setport2 = StringReplace($ser2, 'COM', '')
    _CommSetPort($setport2, $sportSetError, $baud, 8, 0, 1, 0)
    GUICtrlSetData($lblPort4, $ser2)
    If @error Then MsgBox(0, 'Setport error = ', $sportSetError)

    ConsoleWrite("ports set" & @CRLF)
EndFunc  ;==>SetupComPorts2

Func Disconnect()
    ConsoleWrite("Disconnecting" & @CRLF)
    For $n = 1 To 4;for all possible channels
        _CommSwitch($n)
        If _CommPortConnection() <> '' Then
            _CommClearOutputBuffer()
            _CommClearInputBuffer()
        EndIf
    Next
EndFunc  ;==>Disconnect

I am making a version which lets you have up to 50 ports open at a time. When I am happy with ot I'll send you a copy to try.

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

Cheers Martin,

Great work on the dll. The new comMGaa.dll seems to be functioning just great. I have now modified my GUI to allow 8 connections (16 com ports in total) and so far absolutely no problems. I am still tinkering with the GUI and as soon as I've finished with it I'll load it on here. It's turned out to be pretty useful so far. I could see a further use to allow a connection between one port to many, but don't know how much work that would involve in your dll or if it is even possible. I get an error if I try to reuse a port, but I expected that!!!

Rgds

eo

Link to comment
Share on other sites

Cheers Martin,

Great work on the dll. The new comMGaa.dll seems to be functioning just great. I have now modified my GUI to allow 8 connections (16 com ports in total) and so far absolutely no problems. I am still tinkering with the GUI and as soon as I've finished with it I'll load it on here. It's turned out to be pretty useful so far. I could see a further use to allow a connection between one port to many, but don't know how much work that would involve in your dll or if it is even possible. I get an error if I try to reuse a port, but I expected that!!!

Rgds

eo

I'm glad it's working, I had my fingers crossed :)

One port to many can be done with the AutoIt script; I don't think there is any need to change the dll.

You can't reuse a port if by that you mean trying to open a port on a channel when the port is already being used on another channel. A COM port can be opened once to one application, but you could send data from one port to any number of other ports and you could send data from any number of ports to one port.

I probably overlooked the possibility of someone trying to open a port that is already open so I'll try to handle the error in the dll and make the udf function give a meaningfull result. Let me know of any other problems.

(The names of the dll and the udf I sent you to try are temporary names while the new versions are being tried out.)

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 might have been a little premature saying that it was all up and running OK. I can setup the ports fine and I have tested it right up to port 50, so the _CommSwitch and the _CommSetPort seem to work great. However when I try _CommGetline or _CommSendString then my script barfs with a return code of rc:250477278

I've attached my script for the 8 connection GUI. You will note that only the very top set of ports will call _CommGetline or _CommSendString in a function called Connect1(), until I can figure what the problem is. It may well be my mess up in the way I'm calling the function, but if you get the chance would you take a look to see if I have messed up??

Much appreciated

eo

com_port_redirector_8_connections.au3

Link to comment
Share on other sites

I might have been a little premature saying that it was all up and running OK. I can setup the ports fine and I have tested it right up to port 50, so the _CommSwitch and the _CommSetPort seem to work great. However when I try _CommGetline or _CommSendString then my script barfs with a return code of rc:250477278

I've attached my script for the 8 connection GUI. You will note that only the very top set of ports will call _CommGetline or _CommSendString in a function called Connect1(), until I can figure what the problem is. It may well be my mess up in the way I'm calling the function, but if you get the chance would you take a look to see if I have messed up??

Much appreciated

eo

I don't follow your script so can you explain some things to me?

In the function SetupComPorts you are opening ports for different com ports but without using _CommSwitch as far as I can see. Which means you are setting channel 1 each time, ie you only ever have one com port open. Then I don't see how you get out of SetupComPorts except by closing the gui which then exits the script.

I need to get past that part before I can go any further.

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

And there in probably lies my problem. I told you I was a Novice. Let me correct that and see how I get on. I knew you'd probably be able spot it without trying

Thanks for looking

eo

I don't follow your script so can you explain some things to me?

In the function SetupComPorts you are opening ports for different com ports but without using _CommSwitch as far as I can see. Which means you are setting channel 1 each time, ie you only ever have one com port open. Then I don't see how you get out of SetupComPorts except by closing the gui which then exits the script.

I need to get past that part before I can go any further.

Link to comment
Share on other sites

  • 2 years later...
  • 6 years later...

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...