Sign in to follow this  
Followers 0
johns420

Serial issues

8 posts in this topic

I am trying to create a program that will basically act as a terminal for a serial connection. Kind of like hyperterminal, but not hyperterminal.

What I am havinga an issue with is that I cannot send any data out the port or recieve any in. My code is below. Any help would be greatly appreciated.

CODE
#include <GUIConstants.au3>

#include 'CommMG.au3'

#include <GuiEdit.au3>

#include <GuiComboBox.au3>

#include <ScrollBarConstants.au3>

$Main2 = GUICreate("Port Settings", 200, 100)

$cmboPortsAvailable = GUICtrlCreateCombo("",25, 20, 75, 25)

$CmBoBaud = GUICtrlCreateCombo("", 110, 20, 75, 25)

GUICtrlSetData(-1, "50|75|110|150|600|1200|1800|2000|2400|3600|4800", "4800") ; |7200|9600|10400|14400|15625|19200|28800|38400|56000|57600|115200|128000|256000

$btnEnter = GUICtrlCreateButton("Enter", 65, 50, 75, 25)

FindPorts()

GUISetState(@SW_SHOW, $Main2)

$Main1 = GUICreate("8086 SDK Board Programmer", 400, 400)

$listIN = GUICtrlCreateList("", 25, 50, 250, 250)

$txtOut = GUICtrlCreateInput("", 25, 315, 200, 25)

$btnSend = GUICtrlCreateButton("Send", 225,315, 50, 25)

$lblBaud = GUICtrlCreateLabel("", 75,10, 50, 25)

$lblPort = GUICtrlCreateLabel("", 25,10, 50, 25)

While 1

$msg = GUIGetMsg()

If $msg = $GUI_EVENT_CLOSE Then

Exit

EndIf

If $msg = $btnEnter Then

StartCom()

; If $comstart = 1 Then ;Working on Com Recieving

$instr = _CommGetline(@CR,20,200)

; $scroll = StringInStr($instr,@CR)

If $instr <> '' Then;if we got something

$charcount = StringLen($instr)

GUICtrlSetData($listIN, $instr)

MsgBox(0, "RECIEVED", $charcout & " - " & $instr)

EndIf

; EndIf

EndIf

If $msg = $btnSend Then

_CommSendString(GUICtrlRead($txtOut) & @CR)

IF @error = 0 Then

MsgBox(0, "ERROR", "ERROR SENDING DATA - ")

EndIf

EndIf

WEnd

Func StartCom() ;Open com port

Local $sportSetError

$baud = GUICtrlRead($CmBoBaud)

$Port = GUICtrlRead($cmboPortsAvailable)

GUISetState(@SW_HIDE, $Main2)

GUICtrlSetData($lblBaud, $baud & " bps")

GUICtrlSetData($lblPort, $Port)

GUISetState(@SW_SHOW, $Main1)

$setport = StringReplace(GUICtrlRead($CmboPortsAvailable),'COM','')

_CommSetPort($setport,$sportSetError,$baud,8,0,2)

if @error Then MsgBox(0,'Setport error = ',$sportSetError)

$commportconnectin = _CommPortConnection()

EndFunc

Func FindPorts() ;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

For $pl = 1 To $portlist[0]

GUICtrlSetData($CmboPortsAvailable,$portlist[$pl]);_CommListPorts())

Next

GUICtrlSetData($CmboPortsAvailable,$portlist[1]);show the first port found

EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

For best response you need to use event mode send. Something like this

#include <GUIConstants.au3>
#include 'CommMG.au3'
#include <GuiEdit.au3>
#include <GuiComboBox.au3>
#include <ScrollBarConstants.au3>

$Main2 = GUICreate("Port Settings", 200, 100)
$cmboPortsAvailable = GUICtrlCreateCombo("", 25, 20, 75, 25)
$CmBoBaud = GUICtrlCreateCombo("", 110, 20, 75, 25)
GUICtrlSetData(-1, "50|75|110|150|600|1200|1800|2000|2400|3600|4800", "4800"); |7200|9600|10400|14400|15625|19200|28800|38400|56000|57600|115200|128000|256000
$btnEnter = GUICtrlCreateButton("Enter", 65, 50, 75, 25)

GUISetState(@SW_SHOW, $Main2)

$Main1 = GUICreate("8086 SDK Board Programmer", 400, 400)
$listIN = GUICtrlCreateList("", 25, 50, 250, 250)
$txtOut = GUICtrlCreateInput("", 25, 315, 200, 25)
$btnSend = GUICtrlCreateButton("Send", 225, 315, 50, 25)
$lblBaud = GUICtrlCreateLabel("", 75, 10, 50, 25)
$lblPort = GUICtrlCreateLabel("", 25, 10, 50, 25)

FindPorts()
StartCom()
GUISetState(@SW_HIDE, $Main2)
GUISetState(@SW_SHOW, $Main1)

Events()
While 1
    Sleep(50)
    $instr = _CommGetline(@CR, 20, 200)
; $scroll = StringInStr($instr,@CR)
    If $instr <> '' Then;if we got something
        $charcount = StringLen($instr)
        GUICtrlSetData($listIN, $instr)
;MsgBox(0, "RECIEVED", $charcount & " - " & $instr)
    EndIf
WEnd

Func Events()
    Opt("GUIOnEventMode", 1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "Bye")
    GUICtrlSetOnEvent($btnSend, "SendEvent")
EndFunc;==>Events

Func SendEvent()
    _CommSendString(GUICtrlRead($txtOut) & @CR)
    If @error <> 0 Then
        MsgBox(0, "ERROR" & @error, "ERROR SENDING DATA - " & GUICtrlRead($txtOut))
    EndIf
EndFunc;==>SendEvent

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

Func StartCom();Open com port
    Local $sportSetError
    $baud = GUICtrlRead($CmBoBaud)
    $Port = GUICtrlRead($cmboPortsAvailable)

    GUICtrlSetData($lblBaud, $baud & " bps")
    GUICtrlSetData($lblPort, $Port)
    While 1
        Sleep(50)
        $msg = GUIGetMsg()
        If $msg = $btnEnter Then
            $setport = StringReplace(GUICtrlRead($cmboPortsAvailable), 'COM', '')
            _CommSetPort($setport, $sportSetError, $baud, 8, 0, 2)
            If @error Then MsgBox(0, 'Setport error = ', $sportSetError)
            ExitLoop
        EndIf
    WEnd
EndFunc;==>StartCom

Func FindPorts();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
    For $pl = 1 To $portlist[0]
        GUICtrlSetData($cmboPortsAvailable, $portlist[$pl]);_CommListPorts())
    Next
    GUICtrlSetData($cmboPortsAvailable, $portlist[1]);show the first port found
EndFunc;==>FindPorts

Edited by picaxe

Share this post


Link to post
Share on other sites

Thanks a lot for the response, it helped greatly. I was curious though, is there anyway to have the list window that i recieve the data into act more like that of a terminal window? Instead of having it sort it alphabetically and then only rewrite itself over and over, is there anyway to have it display everything that comes in?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Thanks a lot for the response, it helped greatly. I was curious though, is there anyway to have the list window that i recieve the data into act more like that of a terminal window? Instead of having it sort it alphabetically and then only rewrite itself over and over, is there anyway to have it display everything that comes in?

You could use an edit instead of a list view. You need to use

#include <GuiEdit.au3>
_GUICtrlEdit_AppendText($Edit, $sText)

edit - removed meaningless comment.

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.

Share this post


Link to post
Share on other sites

That works great! thank you for all of the help. I just need to do some fine tweaking and add a few more functions in and it will be all done. Thank you very much!

Share this post


Link to post
Share on other sites

I'm trying to access a comport that is already open and send data too it. Is this possible with the comm udf ??? If not what I have found is if I get a program that makes virtual com ports and redirect that data back and forth between com 1 and 4. I connect to 4 and send it data which in turn sends it to com 1.. How can I get aroudn this and just send it to com 1 ????

Share this post


Link to post
Share on other sites

I'm trying to access a comport that is already open and send data too it. Is this possible with the comm udf ??? If not what I have found is if I get a program that makes virtual com ports and redirect that data back and forth between com 1 and 4. I connect to 4 and send it data which in turn sends it to com 1.. How can I get aroudn this and just send it to com 1 ????

You cannot use the commg udf to access a port which is already open.


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.

Share this post


Link to post
Share on other sites

What about creating Virtual ports and sending the Data back and forth ?

Share this post


Link to post
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
Sign in to follow this  
Followers 0