Announcement

Collapse
No announcement yet.

Serial Comm script loop help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Serial Comm script loop help

    I bought a GPIO board that connects to my system via USB configured as a comm port (COM4). Trouble is that the board is setup to recieve individual commands for I/O port statusese. So you send "GI1" and it returns either a "0" or a "1". No vbcr is required to get reply. This is too much work so I cam up with a loop that polls each input. I thing there is a timing issue of some sort. There is another event that could be sending commands to so maybe a sync issue too.

    I am having comm errors like this:

    <table border="0" cellpadding="0" cellspacing="2" width="100%"><tbody><tr><td colspan="1" class="LOGDateTime0" align="left" nowrap="nowrap">3/28/2011 7:07:04 PM </td><td colspan="3" class="LOGType0" align="left"> Error </td><td colspan="8" class="LOGEntry0" align="left">In OpenComPort, error opening port: Unable to obtain a handle to the COM port</td></tr> <tr> <td colspan="1" class="LOGDateTime1" align="left" nowrap="nowrap">3/28/2011 7:07:04 PM </td><td colspan="3" class="LOGType1" align="left"> Error opening my com port </td><td colspan="8" class="LOGEntry1" align="left">Error: Unable to obtain a handle to the COM port</td></tr> <tr> <td colspan="1" class="LOGDateTime0" align="left" nowrap="nowrap">3/28/2011 7:07:04 PM </td><td colspan="3" class="LOGType0" align="left"> Warning </td><td colspan="8" class="LOGEntry0" align="left">Attempt to send to COM port 4, port is not open</td></tr> <tr> <td colspan="1" class="LOGDateTime1" align="left" nowrap="nowrap">3/28/2011 7:07:04 PM </td><td colspan="3" class="LOGType1" align="left"> Error </td><td colspan="8" class="LOGEntry1" align="left">Running script, script run or compile error in file: labo_inputs_check.txt-2147467261:Object reference not set to an instance of an object. in line 27 More info: Object reference not set to an instance of an object.</td></tr></tbody></table>
    Basically this is my script...

    sub main()

    Dim Data
    Dim Count
    Dim e


    hs.CloseComPort(CommPort)

    e=hs.OpenComPort(CommPort,"9600,n,8,1",0,"","")
    if e <> "" then
    hs.writelog "Error opening my com port",e
    end if

    For Count = 1 to 8

    hs.SendToComPort CommPort,"GI" & Count &vbcr

    Do

    Data = hs.GetComPortData(CommPort)

    Loop While InStr(Data, "") = 0

    'hs.CloseComPort(CommPort)

    'hs.writelog "Comport data",Data

    Select Case Data

    Case 0
    hs.SetDeviceStatus "x" & Count, 3 'is ON
    Case 1
    hs.SetDeviceStatus "x" & Count, 2 'is ON
    Case Else
    hs.writelog "Comport data error",Data

    End Select

    Next

    end sub


    Any pointers? Thanks in advance.

    #2
    Is there part of that script missing? Where is 'commport' being declared/set? This should also be an integer, rather than a string - ie 4 rather than COM4

    My preferred method when dealing with com ports is to open it when HS starts with an entry in the startup file and close it in the shutdown file, that way you don't have to open and close it inside the scripts - maybe something to think about...

    Comment


      #3
      Thanks for the tips.


      Left out this part...

      Const CommPort = 4

      I think that I will change it to open comm port once as you say when HS starts to see if this helps.

      Also I have to figure out how to sync the 2 events some how so that there is no overlapping commands sent.

      Comment


        #4
        More troubles now I have slow communications. I am opening the comm port when HS starts up and leaving open.

        I have an singe event polling the device for inputs then the outputs on 2 separate scripts. Both scripts work perfect if sent separately. I don't want to send them async because data will be crashing together.

        Problem is that the data is so slow it is on the port and messes up the message on the next polling.

        Anybody has any thoughts? What if I close the comm port then open it just before sending commands? Will this work?

        Comment


          #5
          Anyone comment on this?

          I get chatter on relay every time triggered...why?
          ---------------------------------------------------



          For Count = 1 to 8

          i = hs.DeviceStatus ("y" & Count)

          hs.waitsecs 0.1

          Select Case i

          Case 2
          hs.SendToComPort CommPort,"SR" & Count
          hs.waitsecs 0.1
          Case 3
          hs.SendToComPort CommPort,"CR" & Count
          hs.waitsecs 0.1
          Case Else
          hs.writelog "Device status error",i
          end select


          rData = hs.GetComPortData(CommPort)

          'hs.writelog "Labo Comms", rData

          Next

          Comment


            #6
            I have to be honest and offer my opinion here but I think for truly two way serial comms you would be better off writing a plugin and perhaps using the MS comm port handling stuff. As to why the relay is chattering (I take it you mean the coil is opening/closing rapidly) I have no idea, sorry.

            Comment


              #7
              Okay got chatter resolved with firmware update.

              Need to come up with a way to send communication to get input status and send output commands.

              The problem is that I am polling the inputs repeatedly and the the outputs my change at any time.

              Any ideas on a strategy to do this? They use same comm port so I cannot send overlapping communications or I will mess up the device.

              any thoughts?

              Comment


                #8
                Originally posted by smokey1384 View Post
                Okay got chatter resolved with firmware update.

                Need to come up with a way to send communication to get input status and send output commands.

                The problem is that I am polling the inputs repeatedly and the the outputs my change at any time.

                Any ideas on a strategy to do this? They use same comm port so I cannot send overlapping communications or I will mess up the device.

                any thoughts?
                I've just finished building/coding/installing/testing a COM port board of my own so i've recently been in this position, how are you polling the inputs? Is this through a recurring event? Also how are you monitoring the outputs to send to the board?...

                Comment


                  #9
                  Yes I have an event triggering every 20 seconds re-occuring for the inputs.

                  Made a simple script that sends a command for input register value then I parse the value into bits and then update Homeseer.

                  That will run forever but I don't have any control over when the outputs need to turn on a relay. Since it's same board and port it causes havoc with the board and port.

                  mrhappy...What board you using? I am using the Labotronik USB Relay board. The guy actually wrote new firmware for me to use. Now it's flawless but the communication strategy is my hurdle.

                  Just need a hand with the communications.

                  Comment


                    #10
                    Originally posted by smokey1384 View Post
                    Yes I have an event triggering every 20 seconds re-occuring for the inputs.

                    Made a simple script that sends a command for input register value then I parse the value into bits and then update Homeseer.

                    That will run forever but I don't have any control over when the outputs need to turn on a relay. Since it's same board and port it causes havoc with the board and port.

                    mrhappy...What board you using? I am using the Labrotronik USB Relay board. The guy actually wrote new firmware for me to use. Now it's flawless but the communication strategy is my hurdle.

                    Just need a hand with the communications.
                    I'm using a board based on a PICAXE (well two boards), i've written my own PIC code and plugin so I kind've went for some easy serial comms.

                    I think I get what you are going at - how do tell the board the output has changed by pressing the buttons on the HS webpage (?), two options in scripting that I can see. One involves as many events as you have outputs with the triggers of '<<device>> changed to any status', then you call into a script passing a parameter of which device called it. Then you also have the 'hs.RegisterStatusChangeCB' function that will call into the script whenever a device changes, you can then check the status changes to find out which ones originated from your board and send the serial data to change the outputs accordingly.

                    Have you got a copy of the serial communications protocol? Out of interest i'll look to see if the plugin i've written can be easily amended (no promises and its difficult for me to test obviously) to that board.

                    Comment


                      #11
                      Mine is not that complicated as I'm only using vbscript function and still needs refinement.

                      I open the comm port at Homeseer startup script. Then trigger events and send the input request.

                      Here is the input script:



                      'User Constants
                      '==============================================
                      Const CommPort = 6


                      sub main()

                      Dim sData
                      Dim CommError
                      Dim convert


                      hs.CloseComPort(CommPort)
                      CommError=hs.OpenComPortEx(CommPort,"9600,n,8,1",0,"","")

                      If CommError<> "" Then
                      hs.WriteLog "Error opening COM" & CommPort,CommError
                      Else
                      hs.WriteLog "COM" & CommPort, "Port Open and Waiting For Data"
                      End If

                      hs.SendToComPort CommPort,"GIA"
                      hs.waitsecs 0.1

                      Do
                      hs.GetComPortCount(Commport) > 0
                      sData = hs.GetComPortData(CommPort)

                      Loop While InStr(1,sData, "") = 0

                      hs.writelog "Labo Comms","GIA Request" & "=" & sData

                      BinaryConvert(sData)

                      end sub

                      Sub BinaryConvert(convert)

                      If convert > 255 Then
                      hs.writelog "Labo Comms","Data Error=" & sData
                      Exit sub
                      End If

                      If convert <= 255 And convert > 127 Then
                      hs.SetDeviceStatus "x8", 2 'is ON
                      convert = convert - 128
                      Else
                      hs.SetDeviceStatus "x8", 3 'is OFF
                      End If



                      If convert <= 127 And convert > 63 Then
                      hs.SetDeviceStatus "x7", 2 'is ON
                      convert = convert - 64
                      Else
                      hs.SetDeviceStatus "x7", 3 'is OFF
                      End If



                      If convert <= 63 And convert > 31 Then
                      hs.SetDeviceStatus "x6", 2 'is ON
                      convert = convert - 32
                      Else
                      hs.SetDeviceStatus "x6", 3 'is OFF
                      End If



                      If convert <= 31 And convert > 15 Then
                      hs.SetDeviceStatus "x5", 2 'is ON
                      convert = convert - 16
                      Else
                      hs.SetDeviceStatus "x5", 3 'is OFF
                      End If



                      If convert <= 15 And convert > 7 Then
                      hs.SetDeviceStatus "x4", 2 'is ON
                      convert = convert - 8
                      Else
                      hs.SetDeviceStatus "x4", 3 'is OFF
                      End If



                      If convert <= 7 And convert > 3 Then
                      hs.SetDeviceStatus "x3", 2 'is ON
                      convert = convert - 4
                      Else
                      hs.SetDeviceStatus "x3", 3 'is OFF
                      End If



                      If convert <= 3 And convert > 1 Then
                      hs.SetDeviceStatus "x2", 2 'is ON
                      convert = convert - 2
                      Else
                      hs.SetDeviceStatus "x2", 3 'is OFF
                      End If



                      If convert <= 1 And convert > 0 Then
                      hs.SetDeviceStatus "x1", 2 'is ON
                      convert = convert - 1
                      Else
                      hs.SetDeviceStatus "x1", 3 'is OFF
                      End If



                      end sub

                      Comment


                        #12
                        Do you have a copy of the manual for the board that details all the expected commands? If so i'll see if its easy to adjust my plugin to suit...I think you would benefit from looking at a plugin, IMHO you will have difficulty doing everything by script.

                        Comment


                          #13
                          Yeah sounds good here is the website...

                          http://www.labotronik.com/lang-en/us...ch-lab-us.html

                          Link to manual at bottom of page. I have the firmware v1.41. This version is not listed in the user manual but includes extra commands such as: "GIA", "GRA", "CRA".

                          I would appreciate any help with regards to your plugin as it maybe suitable with some simple changes.

                          Comment


                            #14
                            Originally posted by smokey1384 View Post
                            Yeah sounds good here is the website...

                            http://www.labotronik.com/lang-en/us...ch-lab-us.html

                            Link to manual at bottom of page. I have the firmware v1.41. This version is not listed in the user manual but includes extra commands such as: "GIA", "GRA", "CRA".

                            I would appreciate any help with regards to your plugin as it maybe suitable with some simple changes.
                            I got it now (was searching the net for labtronik not labotronik), i'm just changing a few commands over and will upload it here for you to try (its doubtful it will work straight off, may not work at all but its something to try), what do the GIA/GRA/CRA commands do compared to the firmware GI1, GR1 or CR1? Have they just been renamed as letters?

                            Comment


                              #15
                              GIA - gets input all
                              GRA - gets relay status
                              CRA - clear relay all

                              Comment

                              Working...
                              X