Author Topic: Generic Serial Driver  (Read 1802 times)

Frunple

  • Hero Member
  • *****
  • Posts: 774
    • View Profile
Generic Serial Driver
« on: December 30, 2014, 07:09:32 pm »
Anyone ever use it?? I'm trying to, but I can't seem to find where to put the received data...

I want to connect to an Arduino which sends the status of my blinds. So when Elve receives a specific message, the corresponding blind status will be updated. There doesn't seem to be any filters in the "Whenever" tab of the rule for "received data".

Frunple

  • Hero Member
  • *****
  • Posts: 774
    • View Profile
Re: Generic Serial Driver
« Reply #1 on: December 31, 2014, 07:35:22 am »
I figured out how to use it but I'm having a problem with the scripting.

Code: [Select]
IncomingCommand = EventArgs.Data;
ThisSource = IncomingCommand.SubString(0,4); //source number
ThisCommand = IncomingCommand.SubString(4,1); //the command
system.Log( ThisSource );
system.Log( ThisCommand );
if (ThisSource == "Z1W1")
{
if (ThisCommand == "1")
vars.SetVariableValue("Z1W1", "Open"); // set z1w1 open
else if (ThisCommand == "0")
vars.SetVariableValue("Z1W1", "Closed"); // set z1w1 closed
}

Results in the same error:

Code: [Select]
Date:     12/31/2014
Time:     9:28:53 AM
Severity: Error
Source:   MasterService
Computer: OPTIPLEX755
Context Type: Script
Context Name: rule 'blind status'
Description:
An error occurred while executing the script rule 'blind status'.
ScriptEvaluatorException: An unknown error occurred while evaluating the script.  TargetInvocationException: Exception has been thrown by the target of an invocation.  ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length

CodecoreTechnologies.Elve.DriverFramework.Scripting.ScriptEvaluatorException: An unknown error occurred while evaluating the script. ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentOutOfRangeException: Index and length must refer to a location within the string.
Parameter name: length
   at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
   at CodecoreTechnologies.Elve.DriverFramework.Scripting.ScriptString.Substring(ScriptNumber startIndex, ScriptNumber length)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at CodecoreTechnologies.Elve.DriverFramework.Scripting.ScriptObject.InvokeMethod(Object o, String methodName, Object[] parameters, Boolean autoConvertType, IScriptObject& result)
   at CodecoreTechnologies.Elve.DriverFramework.Scripting.ScriptObject.InvokeMethod(String methodName, IScriptObject[] parameters, IScriptObject& result)
   at Common.ScriptEvaluator.ScriptEvaluator.objectMemberAndArrayMember()
   at Common.ScriptEvaluator.ScriptEvaluator.postIncrementDecrement()
   at Common.ScriptEvaluator.ScriptEvaluator.factor()
   at Common.ScriptEvaluator.ScriptEvaluator.multiplicative()
   at Common.ScriptEvaluator.ScriptEvaluator.additive()
   at Common.ScriptEvaluator.ScriptEvaluator.bitwiseShift()
   at Common.ScriptEvaluator.ScriptEvaluator.relational()
   at Common.ScriptEvaluator.ScriptEvaluator.equality()
   at Common.ScriptEvaluator.ScriptEvaluator.bitwiseAnd()
   at Common.ScriptEvaluator.ScriptEvaluator.bitwiseExclusiveOr()
   at Common.ScriptEvaluator.ScriptEvaluator.bitwiseInclusiveOr()
   at Common.ScriptEvaluator.ScriptEvaluator.logicalAnd()
   at Common.ScriptEvaluator.ScriptEvaluator.logicalOr()
   at Common.ScriptEvaluator.ScriptEvaluator.variableAssignment()
   at Common.ScriptEvaluator.ScriptEvaluator.evaluateBlock()
   at Common.ScriptEvaluator.ScriptEvaluator.evaluate(ScriptParser parser, ScriptVariableCollection variables, GlobalFunctionEvaluatorDelegate globalFunctionEvaluator, ScriptObjectTypeInfoCollection objectInfos, ScriptFunctionInfoCollection scriptFunctionInfos)
   --- End of inner exception stack trace ---
   at Common.ScriptEvaluator.ScriptEvaluator.evaluate(ScriptParser parser, ScriptVariableCollection variables, GlobalFunctionEvaluatorDelegate globalFunctionEvaluator, ScriptObjectTypeInfoCollection objectInfos, ScriptFunctionInfoCollection scriptFunctionInfos)
   at Common.ScriptEvaluator.ScriptEvaluator.Evaluate(ScriptParser parser, ScriptVariableCollection variables, GlobalFunctionEvaluatorDelegate globalFunctionEvaluator, ScriptObjectTypeInfoCollection objectInfos, ScriptFunctionInfoCollection scriptFunctionInfos)
   at Common.ScriptEvaluator.ScriptEvaluator.Evaluate(String s, ScriptPosition position, ScriptVariableCollection variables, GlobalFunctionEvaluatorDelegate globalFunctionEvaluator, ScriptObjectTypeInfoCollection objectInfos, ScriptFunctionInfoCollection scriptFunctionInfos)
   at Common.ScriptEvaluator.ScriptEvaluator.Evaluate(String s, ScriptVariableCollection variables, GlobalFunctionEvaluatorDelegate globalFunctionEvaluator, ScriptObjectTypeInfoCollection objectInfos, ScriptFunctionInfoCollection scriptFunctionInfos)
   at Common.ScriptEvaluator.ScriptEvaluator.Evaluate(String s, ScriptVariableCollection variables, GlobalFunctionEvaluatorDelegate globalFunctionEvaluator, ScriptObjectTypeInfoCollection objectInfos)
   at Common.ScriptEvaluator.ScriptEvaluator.EvaluateNow(String s, ScriptVariableCollection variables, GlobalFunctionEvaluatorDelegate globalFunctionEvaluator, ScriptObjectTypeInfoCollection objectInfos)
   at MasterService.ScriptRunner.RunNow(String scriptName, String script, ScriptVariableCollection extraScriptVariables, Boolean throwError)
Location in script: line 2, column 43.

ThisSource = IncomingCommand.SubString(0,4); //source number
                                          ^

The received commands are either "Z1W11" or Z1W10" .

bpwwer

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Re: Generic Serial Driver
« Reply #2 on: December 31, 2014, 09:10:14 am »
Can you output the IncomingCommand variable to the log also, right after you set it?  The error is implying that the string in IncomingCommand is less than 4 characters long.

One of the things I'll do when trying to debug something like this is simplify the script down to just the area that it's failing and add Log commands everywhere just to try and narrow down what the problem really is.

I haven't looked at the SubString command, but since some things in Elve use 1 as the first index instead of zero, you could also try starting at index 1.  Experiment with various substring commands and see what the result is.  I'd probably start with a script that just does:

Code: [Select]
IncomingCommand = EventArgs.Data;
system.Log(IncomingCommand);

Then try
Code: [Select]
IncomingCommand = EventArgs.Data;
system.Log(IncomingCommand);
ThisSource = IncomingCommand.SubString(1,1);
system.Log(ThisSource);

The results of those should be pretty informative about what Elve is really doing. 


iostream212

  • Sr. Member
  • ****
  • Posts: 459
    • View Profile
Re: Generic Serial Driver
« Reply #3 on: December 31, 2014, 09:34:10 am »
Also if you receive extra data outside of what you are expecting you can wrap your logic in a try/catch. Then you can keep your script going when you encounter an unexpected value.

Sent from my SCH-I545 using Tapatalk

I always wanted to be somebody. In retrospect, I think I should have been more specific.

Frunple

  • Hero Member
  • *****
  • Posts: 774
    • View Profile
Re: Generic Serial Driver
« Reply #4 on: December 31, 2014, 09:48:19 am »
Code: [Select]
IncomingCommand = EventArgs.Data;
system.Log(IncomingCommand);

Then try
Code: [Select]
IncomingCommand = EventArgs.Data;
system.Log(IncomingCommand);
ThisSource = IncomingCommand.SubString(1,1);
system.Log(ThisSource);

The results of those should be pretty informative about what Elve is really doing.

First result is the full command:

Z1W11

Second result is:

Z1W11
1

If I change to ThisSource = IncomingCommand.SubString(0,1);
I get :

Z1W11
Z

So it definitely starts with a 0 and it's getting the whole command.
The actual Arduino code is:

Code: [Select]
   if (z1w1State != lastz1w1State)
   {
      if (z1w1State == HIGH)
  {  
Serial.println("Z1W11\r\n" );
  }
  else
  {
Serial.println("Z1W10\r\n" );
  }
   }

As I pasted that I noticed I'm using the println and also sending a \n. Gonna try serial.print now.

Frunple

  • Hero Member
  • *****
  • Posts: 774
    • View Profile
Re: Generic Serial Driver
« Reply #5 on: December 31, 2014, 09:52:51 am »
That did it. Double carriage return.

Thanks for the help guys!
Happy New Year!!