Jump to content

Send nulls and any binary value via MSComm control


Recommended Posts

I've tried to send 4 byte message via MSComm control VB and in AutoIt.  Message is "*A<msb><lsb>".  This code works in VB with any value 0 or 1 in msb, and 0 to 255 in lsb.

  .Output = "*A" & Chr(0) & Chr(ix)

 

In AutoIt this works, but not if either byte is zero, and not if lsb is 1.

$sAxName = "MSCOMMLib.MSComm.1"
$obj = ObjCreate($sAxName)

With $obj
    .CommPort = 31
    .Settings = "9600,N,8,1"
    .PortOpen = True
    .NullDiscard = False
    .NullDiscard = True
    .HandShaking = 0
    .InputMode = 1
    .DTREnable = True
    .EOFEnable = False
    .RTSEnable = True
EndWith
 
For $iCnt = 0 To 15
    With $obj
        .NullDiscard = False
        $sMsg = "*A"  & Chr(0) & Chr(Int($iCnt))
       .Output = $sMsg
        If $iCnt < 3 then
            MsgBox(0, Default, $iCnt)
        EndIf
    EndWith
    Sleep(200)
Next

You might expect .NullDiscard to be relevant.  The VB works with this true or false, and I've tried both in AutoIt.  It's possible that I have some other property different, but I've looked through them all.

I wonder if AutoIt is passing the whole string to the control, or stopping at the zero.  I can't think how to prove that either way.

Chr(Int($iCnt)) works correctly for values > 2, but for 1 it doesn't do the same as Chr(1) - what am I missing here?

Edited by RichardL
Link to post
Share on other sites

Alright, you can't use a string with nulls in it.  But you can use 'Binary' and the code below sends a 16 bit number via serial to Arduino.

#cs
Test program for sending message to Arduino.
Message is *A followed by 16 bit binary number, total 4 bytes.
This sends a sequence of numbers from 0 to 256
#ce

Opt("MustDeclareVars", 1)

Local $Msg
Local $sAxName
Local $obj

$sAxName = "MSCOMMLib.MSComm.1"
$obj = ObjCreate($sAxName)

With $obj
    .CommPort = 31
    .Settings = "9600,N,8,1"
    .NullDiscard = False
    .NullDiscard = True
    .HandShaking = 0
    ;.InBufferSize = 50
    ;.OutBufferSize = 50
    .InputMode = 0
    .DTREnable = True
    .EOFEnable = False
    .RTSEnable = True
    .PortOpen = True
EndWith
Sleep(50)

;For $iCnt = 0x100 To 0 Step - 1
For $iCnt = 0 To 0x100
    With $obj
        .NullDiscard = False
        $Msg = Binary("*A"  & Chr(BitShift($iCnt, 8)) & Chr(BitAND($iCnt, 0xFF)))
        .Output = $Msg
    EndWith
    Sleep(50)
Next

With $obj
    .PortOpen = False
EndWith
Link to post
Share on other sites

I would like to use AutoIt to speak to an Arduino but I don't know much about it.  An Arduino UDF would be purely awesome for us physical computing newbies.

https://esto-es-un-proyecto.googlecode.com/files/funciones%20nativas.rar

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites

BrewManNH - that is too cool!  :D

Has that been published here before?  I have searched the forum for Arduino and could not find anything.  I appreciate the sharing.  I would love to get some folks posting some threads on examples and designs.  Thanks for sharing.

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Link to post
Share on other sites

That was linked to from the Examples scripts section, I haven't used it before, but it looks interesting

'?do=embed' frameborder='0' data-embedContent>>

This one is a GUI to use when programming the arduino

'?do=embed' frameborder='0' data-embedContent>>

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to post
Share on other sites

Thanks for your comments.  The serial protocol I'm using is very simple, copied from here.  I found that it didn't recover if it became out of step, so I made it discard anything that didn't comply.  Really it needs to use acknowledgement and 'please repeat' replies.

       Arduino...
        while ( Serial.peek() != '*'){Serial.read();}
        // do we still have enough for a message?
        if ( Serial.available() >= 4 )
        {
            // this probably has to be true now
            if( Serial.read() == '*' )
            {
                tag = Serial.read();
                if( tag == 'A' )
                {
                    x = Serial.read() * 256;
                    x = x + Serial.read();
                    digitalWrite( 13 , H );
                    delay( 5 );
                    digitalWrite( 13, L );
                    delay( 5 );
                    updateLEDs( x );
                    delay( 10 );
                }
            }
        }

 
Link to post
Share on other sites

RichardL, I have an embarrassing question as a complete Arduino newb :>.  I grabbed the link for the arduino.au3 file above and the blink example included therein.  I used my Ardunio to attach an led to the digital port 13 and changed the blink example as follows:

#include <arduino.au3>
conecta(7,9600)

While 1
    digitalwrite(13,1)
    Sleep(1000)
    digitalwrite(13,0)
    Sleep(1000)
WEnd

I know I am on port 7 because I checked it in the device manager.  One pin for the LED is in 13 and one is in GRND.  The LED is lit up - but it does not blink.  Can you tell me what I am doing wrong?

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By t0nZ
      Computer Plucker is a fork and a "restoration" of Computer stats utility by @Rogue5099 (link)
      You can obtain various data about local machine, as from the screenshot.

      But my goal is not (only) to provide another PC stats tool, but to develop a very basic remote inventory tool, like (a small subset of) the abandoned Spiceworks or the Microsoft SCCM.
      So this script can be deployed on a machine as a standalone executable (yes you need only ONE file to distribute) , and launching the .EXE it installs itself to a folder (es. c:\autoit\computerplucker)
      In tools menù you can set two tasks and not only
      one for creating every week a file.ini with all the data of the machine. another one  for creating every week a file.ini with all the data of the machine PLUS uploading a CRYPTED record of the machine in a free mysql DB online For my needs, I am  deploying the .exe and schedule the second task, uploading data one time a week.
      In tools menù you will find also commands for:
      manual creating the .ini data file  to upload the data in crypted records to upload the data in clear text records (for testing..) total uninstall of ComputerPlucker, with a simple security code. many other tasks in not so logical order.. next version I have to tidy the menù... So my plan is to deploy this on my machines, and have the data in a online DB, this DB will be for me only a "cache" for data records to be downloaded (and deleted) by another script.
      As today the account of this TEST DB is embedded in code, using the "File to Base64 String' Code Generator" by @UEZ (used also for embedding icons and XML task definitions)
          Local $mysqlhost = "sql11.freemysqlhosting.net"
          Local $mysqlport = "3306"
          Local $mysqldb = "sql11403701"
          Local $mysqluser = "sql11403701"
          Local $mysqlpsw = "QtL7T9U5GV"
      The DB is one table of 30 mysql tinytext fields... very simple indeed...
      You can try to upload data to this DB and access (phpmyadmin or a random mysql browser app) to see the results.
      Or you can use my ComputerPluckerDBbrowser (see at the end of this post) a script to collect and manage the data.

      PRIVACY warning: you are uploading data of your PC account and PC data, test as your risk (or test in another account...)
       
      I also created a small post in a blog for not so geek friends, and I have a more advanced version of the data browser, you can have more info lookin' here. 😀
      Attached you will find the code, and all the necessary includes are at this >link<.
      ComputerPlucker.EXE is downloadable >HERE<
      ComputerPluckerDBbrowser.EXE is downloadable >HERE<
       
      ComputerPluckerDBbrowser_LITE.au3
       
      ComputerPlucker.au3

    • By Inpho
      Hi All,
      I didn't know where to put this; my apologies if this is the wrong location.
      When you plug a Samsung mobile device (phone, tablet, etc.) into the USB of a Windows PC, you can right-click the device in My Computer and select Properties. Here, it will show you the correct serial number of the device. Clearly Windows doesn't use adb to get the SN so I'm stuck at how to get the SN without adb and where the device doesn't have a drive letter assigned to it.
      When I last picked this up, I tried seemingly everything I could from wmi(?) and winmgmts(?) but either it's hidden cloak-and-dagger style or I can't see the forest for the trees...
      Does anyone know what API Windows uses when getting the serial number of a device Windows calls a Portable Media Player?

    • By mikered82
      Hello!
      Could someone help me please with a UDF/library/example to communicate with Arduino thru USB using V-USB Driver (HID Device).
      I want to implement remote control for PC (TV Tuner remote). Arduino would read and decode data from an IR Sensor then send the decoded data to the PC thru USB.
      Thanks! Any help much appreciated!!
    • By MazeM
      Hi
      here's another UDF for the serial port. It is very similar to CommAPI using kernel32.dll, but all code is packed into a single file without any dependencies, not even using WinAPI.au3. It differs from existing UDF that it doesn't allow a timeout when reading, instead it always returns immediately, either with the requested amount ob bytes read or with a failure status. And of course there is a function provided to query the amount of available bytes in the receive buffer. The reason behind this design decision: You can do 1000 other things in the main loop while checking from time to time if enough data bytes arrived. There's no point to block the program waiting for the serial port.
      It is currently a work-in-progress, as I didn't test all functions yet. The code was developed and tested on Windows 7 64 bit.  The ComUDF-Tests.au3 shows some tests and basic usage of the UDF. Maybe there's no reason to use this UDF, given the existence of the others UDFs, but I did it to get to know DllCall better - I use structs no only to pass but also to get data back (I don't use the array returned by DllCall to read that data, unless required). You're welcome to test it on older and newer Windows versions.
      Here's a list of the implemented functions:
      ; _ComListPorts ; _ComOpenPort ; _ComSetTimeouts ; _ComClosePort ; ; _ComSetBreak ; _ComClearBreak ; _ComGetInputcount ; _ComGetOutputcount ; _ComClearOutputBuffer ; _ComClearInputBuffer ; ; _ComSendByte ; _ComReadByte ; _ComSendBinary ; _ComReadBinary ; ; _ComSendChar ; _ComReadChar ; _ComSendCharArray ; _ComReadCharArray ; _ComSendString ; _ComReadString ; ; __ComClearCommError ; __PurgeComm Maze
       
      ComUDF.au3
      ComUDF-Tests.au3
    • By Satvik
      Hi 
      I am trying to open a dll using DLLOpen, however everytime i try to do so the function fails and returns -1 result. 
      I have the dll in the same folder as the auto it script. 
      Local $hDLL = DllOpen("C:\Users\310255155\Downloads\COMMGvv2\commg.dll") DllCall($hDLL, "int", "MessageBox", "hwnd", 0, "str", "Some text", "str", "Some title", "int", 0) DllClose($hDLL) MsgBox(0,'result',$hDLL)  
×
×
  • Create New...