For beginner.
#include <GuiConstantsEx.au3>
#include <CommMG.au3>
#include <file.au3>
#include <Date.au3>
#include <WindowsConstants.au3>
;You can see what send to port com. You must download PortMon from Microsoft.
GuiCreate("ConnectToPort by SZYJA"&Chr(153), 400, 220)
GUISetFont (10, 400, 0)
$Edit = GUICtrlCreateEdit("",0,120,400,100,$WS_VSCROLL)
GUISetFont (8.5, 400, 0)
;----------------------------------------------------------------------
$list_ports = _CommlistPorts()
GUICtrlCreateLabel("Choose port: ", 10, 30, 70, 20)
$available_port = GuiCtrlCreatecombo("",80, 26, 60,20)
GUICtrlSetData(-1,""&$list_ports)
$connect = GUICtrlCreateButton("Connect", 155,17,110,40)
$disconnect = GUICtrlCreateButton("Disconnect", 280,17,110,40)
GUICtrlSetState ($disconnect, $gui_disable)
$send_string = GUICtrlCreateButton("Send String", 155,70,110,40)
$clear_edit = GUICtrlCreateButton("Clear edit", 280,70,110,40)
;----------------------------------------------------------------------
$HW = "Hello World";string to send
guisetstate ()
while 1;start main loop
;----------------------------------------------------------------------
;----------------------------------------------------------------------
$open_port = 0
$status_port = 0
while $open_port <= 0 ;start connect loop
$msg_1 = GUIGetMsg()
Select
case $msg_1 = $connect ;connect to port
$file = Fileopen("Log.txt", 1);Create a log file
;----------------------------------------------------------------------
local $result
$baud = 115200
$bits = 8
$par = 0
$stop = 1
$flow = 0
sleep(100)
;----------------------------------------------------------------------
$_available_port = GUICtrlRead($available_port)
$nr_port = StringReplace(""&$_available_port, "COM", "")
$status_port = _CommSetPort($nr_port,$result,$baud,$bits,$par,$stop,$flow)
if $status_port = 1 Then
FileWriteLine($file, "" &_NowTime() &" Connect to port: " &$_available_port)
sleep(400)
Else
MsgBox(0,"Error","Port "&$nr_port &" " &$result)
EndIf
$open_port = $status_port
case $msg_1 = $gui_event_close
exit
EndSelect
wend;end connect loop. We connected.
;----------------------------------------------------------------------
;----------------------------------------------------------------------
if $open_port = 1 Then
GUICtrlSetState ($connect, $gui_disable)
GUICtrlSetState ($disconnect, $gui_enable)
GUICtrlSetState ($available_port, $gui_disable)
EndIf
while 1;start program loop
$msg = GUIGetMsg()
Select
case $msg = $send_string
_CommSendString($HW,10)
FileWriteLine($file, "" &_NowTime() &" " &$HW)
;sometimes need sleep before use _Commgetstring()
;sleep(1500)
$rec = _Commgetstring()
GUICtrlSetData($Edit, $rec)
Case $msg = $clear_edit
GUICtrlSetData($Edit, "")
case $msg = $disconnect
_CommClosePort()
sleep(400)
GUICtrlSetState ($connect, $gui_enable)
GUICtrlSetState ($disconnect, $gui_disable)
GUICtrlSetState ($available_port, $gui_hide)
$list_ports = _CommlistPorts()
$available_port = GuiCtrlCreatecombo("",80, 26, 60,20)
GUICtrlSetData(-1,""&$list_ports)
FileWriteLine($file, "" &_NowTime() &" Disconnected")
FileClose($file)
exitloop;escape to start main loop
case $msg = $gui_event_close
_CommClosePort()
exit
EndSelect
wend;end start program loop
;----------------------------------------------------------------------
wend;end main loop
;----------------------------------------------------------------------