Jump to content

[Solved]_Serial port communication example usage help


 Share

Recommended Posts

Edit:

This topic was solved, see >Post #15 for the codes and setup that worked for me.

 

Hi

I downloaded the COMMGvv2.zip & CommgExample.au3 From >this topic.

Ran the example GUI with default parametres, in the Send text field entered number 1 & pressed Send button. Sending 1 as char worked, the LED on my Arduino Leonardo board went on, then I sent 0 & led went off, just the way I programmed the board.

But I can not figure out how to make a custom code, a  little help would be appreciated. Please.

my port: COM 25
baud: 9600
data bits: 8
stop bits: 1
parity: 1
flow control: 0

MY code so far: (consolewrite returns 0)

#include <GUIConstants.au3>
#include 'CommMG.au3';or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'

$iPort = 25             ; COM 25
$sErr = 'errormsg'
$iBaud = 9600
$iBits = 8
$iPar = 0
$iStop = 1
$iFlow = 0
$RTSMode = 0
$DTRMode = 0

$resOpen = _CommSetPort($iPort, $sErr, $iBaud, $iBits, $iPar, $iStop, $iFlow, $RTSMode, $DTRMode)
ConsoleWrite($resOpen & @LF)
_CommSendstring('1')
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Run your code again, but set ConsoleWrite to:

ConsoleWrite($resOpen & "@error returns: " & @error)

This will give you a more detailed message on what the problem was on the failure of _CommSetport(). :)

Post it, and I'm sure we'll be able to help further. ;)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Run your code again, but set ConsoleWrite to:

ConsoleWrite($resOpen & "@error returns: " & @error)

This will give you a more detailed message on what the problem was on the failure of _CommSetport(). :)

Post it, and I'm sure we'll be able to help further. ;)

 

In not even sure Im using the code correctly. Its my first time programming serial communication. But hire is the return.

ConsoleWrite($resOpen & " @error returns: " & @error & @CRLF)
0 @error returns: 2
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

The error is happening with the $iStop as shown in the Returns from this UDF function _CommSetport():

Returns;  on success - returns 1 and sets $sErr to ''
;           on failure - returns 0 and with the error message in $sErr, and sets @error as follows
;                           @error             meaning error with
;                             1               dll call failed
;                             2               dll was not open and could not be opened
;                            -1               $iBaud
;                            -2               $iStop
;                            -4               $iBits
;                            -8               $iPort = 0 not allowed
;                           -16               $iPort not found
;                           -32               $iPort access denied (in use?)
;                           -64               unknown error

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

2               dll was not open and could not be opened

Yes I read that, but the question is why, I mean the example works & I copied the include code from there. So it should work....unless im missing code to make the communication work, Am I?

Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

It has to do with this line in the UDF that is returning an error of -2:

$vDllAns = DllCall($hDll, 'int', 'SetPort', 'int', $iPort, 'int', $iBaud, 'int', $iBits, 'int', $iPar, 'int', $iStop, 'int', $iFlow, 'int', $RTSMode, 'int', $DTRMode)

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Looked at the udf & it says You cannot set the same COM port on more than one channel. Does it mean I need to create channel & then set port to the channel? 

confused.

My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

you can always try changing channels with _CommSwitch() like so:

_commSwitch(2)

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

This makes no sense, I started stripping code from example & if I drag & drop it into AutoIt3.exe, the example is working, but if I try to F5 run it GUI runs, but sending chars does not work. 

Weird things going on, + I also get this error, so I went & deleted byref in front of the

_CommSetPort($iPort, ByRef $sErr,

And guess what if I drag & drop it into autoit exe it works, but if I F5 it it does not work...

ERROR: _CommSetPort() called with Const or expression on ByRef-param(s).

EDIT: This issue with drag & dropping is solved, outdated autoit version was used to try to run this, working of final send code, will post it if it will work:

Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

 

This makes no sense, I started stripping code from example & if I drag & drop it into AutoIt3.exe, the example is working, but if I try to F5 run it GUI runs, but sending chars does not work. 

Weird things going on, + I also get

ERROR: _CommSetPort() called with Const or expression on ByRef-param(s).

Please post the script you used to get this error.

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Thank you for support Breathe. Got everything working, guess all the problems were happening because of the outdated Autoit version.

Hire is the code that almost works for me, turns on the led by sending 1, then sleeping for 2 seconds & turning off the led by sending 0. It also returns the data that is transmitted back.

But it is still a bit bugged, if I look at the output data there should be : (Any ideas why that my be?)

Decimal value: 48

not 

Decimal value: 13

The code:

#include 'CommMG.au3';or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'

$resOpen = _CommSetPort(25, 0, 9600, 8, 0, 1, 0)
_CommSendstring(1 & @CR) ; Turn on led

While 1

    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed
    $instr = _commGetLine(@CR, 20, 200);_CommGetString()

    If $instr <> '' Then;if we got something
        ;$instr = StringReplace($instr, @CR, @CRLF)
        ConsoleWrite($instr)
    Else
        Sleep(2000) ;MichaelXMike
        ExitLoop
    EndIf

WEnd

_CommSendstring(0 & @CR) ; turn off led
_Commcloseport(true)

Outputs Data that is returned back:

Decimal value: 49
Integer value: 1
Led is On
Decimal value: 13  // this should be 48 not 13, because 48 == 0 & in inputting 0
Integer value: -35
Invalid input, enter 1 or 0 to control led on pin 13!

Proper output that I expext to see:

Decimal value: 49
Integer value: 1
Led is On
Decimal value: 48
Integer value: 0
Led is Off
Edited by goldenix
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Glad you got it figured out, my pleasure. ;)

That seems like it would be something that is inside the arduino that would have to be configured.

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Glad you got it figured out, my pleasure. ;)

That seems like it would be something that is inside the arduino that would have to be configured.

 

With arduino software im getting proper values, with autoit not, But with autoit im sending:

_CommSendstring(1 & @CR) ; note the @cr

13 decimal is cr char

_CommSendstring("0") ; This does not work
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

Try

_CommSendString("0" & @CR)
_CommSendString(0 & @CR)

otherwise, someone with more knowledge is going to have to help you out with this one. I have reached my limit of comm knowledge. ^_^

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

I think this topic can be set as Solved. Hire are the results. Pretty neat actually, ill see if I can control a minirobot with hotkeys later.

Idea was to press the button, the led turns on & sends data to autoit script, press it again, led turns off (and sends data to autoit again.)

Btn went from off to on
Led is On
Btn went from off to on
Led is Off

Setup used: (that is a 10k resistor (a pulldown resistor), can be bigger)

IMG_0644_thumb.png

Arduino 1.0.6 code:

// this constant won't change:
const int  buttonPin = 2;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to

// Variables will change:
int ledState = 0;
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  delay(50);

  // compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      Serial.println("Btn went from off to on");


      if(ledState == 0){
        digitalWrite(ledPin, 1); // Turn led on
        ledState = 1;

        Serial.println("Led is On");

      }
      else{
        digitalWrite(ledPin, 0);
        ledState = 0;
        Serial.println("Led is Off");
      }

    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      //Serial.println("wend from on to off");
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;
}

Autoit stripped gui code (stripped version of the example in the first post):

#include <GUIConstants.au3>
#include 'CommMG.au3';or if you save the commMg.dll in the @scripdir use #include @SciptDir & '\commmg.dll'
#include <GuiEdit.au3>
#include <windowsconstants.au3>
#include <buttonconstants.au3>

HotKeySet("{ESC}", "alldone")

#Region ### START Koda GUI section ### Form=d:\my documents\miscdelphi\commg\ExampleComm.kxf
$Form2 = GUICreate("COMMG Example", 473, 349, 339, 333, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SIZEBOX, $WS_THICKFRAME, $WS_SYSMENU, $WS_CAPTION, $WS_OVERLAPPEDWINDOW, $WS_TILEDWINDOW, $WS_POPUP, $WS_POPUPWINDOW, $WS_GROUP, $WS_TABSTOP, $WS_BORDER, $WS_CLIPSIBLINGS))
$Edit1 = GUICtrlCreateEdit("", 10, 25, 449, 223, BitOR($ES_AUTOVSCROLL, $ES_AUTOHSCROLL, $ES_READONLY, $ES_WANTRETURN, $WS_HSCROLL, $WS_VSCROLL))
$BtnSend = GUICtrlCreateButton("Send", 380, 273, 53, 30, $BS_FLAT)
$Input1 = GUICtrlCreateInput("1", 18, 279, 361, 21)
$Checkbox1 = GUICtrlCreateCheckbox("Add LF to incomming CR", 273, 4, 145, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetResizing(-1, $GUI_DOCKAUTO)
$Label11 = GUICtrlCreateLabel("Text to send", 24, 261, 63, 17)
$BtnSetPort = GUICtrlCreateButton("Set Port", 16, 312, 73, 30, $BS_FLAT)
$Label21 = GUICtrlCreateLabel("Received text", 34, 6, 70, 17)
$Label31 = GUICtrlCreateLabel("commg.dll version unknown", 272, 328, 135, 17)
GUICtrlSetColor(-1, 0x008080)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

   $resOpen = _CommSetPort(25, 0, 9600, 8, 0, 1, 0)
   _CommSetXonXoffProperties(11, 13, 100, 100)

    Opt("GUIOnEventMode", 1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit")
    GUICtrlSetOnEvent($BtnSend, "SendEvent")

While 1
    ;sleep(40)
    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed
    $instr = _commGetLine(@CR, 20, 200);_CommGetString()

    If $instr <> '' Then;if we got something
        If GUICtrlRead($Checkbox1) = $GUI_CHECKED Then $instr = StringReplace($instr, @CR, @CRLF)
        GUICtrlSetData($Edit1, $instr, 1)
    Else
        Sleep(20) ;MichaelXMike
    EndIf

WEnd


Func _exit()
    Exit
EndFunc   ;==>justgo

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

NoGui example:

#include 'CommMG.au3'

$resOpen = _CommSetPort(25, 0, 9600, 8, 0, 1, 0)
_CommSendstring("1")
sleep(2000)
_CommSendstring("0")

And No gui port listener example: (this will give you the gui is off or on messages)

#include 'CommMG.au3'

$resOpen = _CommSetPort(25, 0, 9600, 8, 0, 1, 0)

While 1
    ;gets characters received returning when one of these conditions is met:
    ;receive @CR, received 20 characters or 200ms has elapsed
    $instr = _commGetLine(@CR, 20, 200);_CommGetString()

    If $instr <> '' Then;if we got something
        ConsoleWrite($instr)
    Else
        Sleep(20) ;MichaelXMike
    EndIf
WEnd
My Projects:[list][*]Guide - ytube step by step tut for reading memory with autoitscript + samples[*]WinHide - tool to show hide windows, Skinned With GDI+[*]Virtualdub batch job list maker - Batch Process all files with same settings[*]Exp calc - Exp calculator for online games[*]Automated Microsoft SQL Server 2000 installer[*]Image sorter helper for IrfanView - 1 click opens img & move ur mouse to close opened img[/list]
Link to comment
Share on other sites

  • 3 years later...

Someone please can explain how this generally works?

Im lookin for an quick way to reconnect the connected systems by deaktivate/activate USB Port + Com Port

i found the func _CommSetXonXoffProperties but is this also valid for my case?

 

and btw is there a samplescript for it?

 

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