more G-Labs products

Author Topic: Serial port helper problem  (Read 1025 times)

July 04, 2016, 09:14:08 PM
Read 1025 times

siepacz

  • *
  • Information
  • Newbie
  • Posts: 6
Hi All
I'm looking for some help with serial port communication.
I wrote automation program to deal with mysensors serial gateway based on user "haitch"(thanks haitch for an inspiration) python version. Can not make his script working, so decide to have some fun and utilize SerialPort helper ;-) . Everything works ok,  but when I'm trying to send a message from gateway to sensor node (using When.WebServiceCallReceived),nothing happening :-(
I'm running HG r525 on raspberry pi 2.Script attached (I'm not c # coder, just understand a few bits here and there)
Any advise if you do not mind ;-)
Thanks

July 05, 2016, 02:32:07 PM
Reply #1

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
What are you passing in as the url?

July 05, 2016, 06:32:52 PM
Reply #2

siepacz

  • *
  • Information
  • Newbie
  • Posts: 6

July 06, 2016, 12:41:26 PM
Reply #3

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
I would say you are probably catching an error, put a program.notify within the case "control.on" statements

I would also check the status of module - IE check for null or similar to make sure you actually have a valid module..

As other than that it looks ok with a quick test of the logic..

http://hg_address/api/Relay/1/Control.On/undefined/

would send: 1;1;1;0;2;0

David


July 06, 2016, 01:26:11 PM
Reply #4

siepacz

  • *
  • Information
  • Newbie
  • Posts: 6
Hi Dave
Thanks for advice. I'm not at home right now, so can not check it (I'll do it tonight). But I'm sure, I'm not catching an error.
Program.RaiseEvent(module, "Status.Level", "1", "Switch" + switchid);
module.Parameter("Status.Level").Value ="1";
are executed.
My widget reacting on Control.ON/Off action, but arduino node is not receiving any message.

By saying "I would also check the status of module", you mean variable module in not null in my script when webservicecall happening?

I've tested this node using "Mycontroller.org" and relay is reacting on web requests(node receiving a proper message).
BTW: I'm not sure but, is not that serial communication using com port exclusively? I mean ,once port is opened and "listener" (read from port) is enabled, the same thread can not use it for (write) at the same time? Is SerialPort Helper handling async read/write?
Or maybe
 Program.RunAsyncTask(()=>{
 SerialPort.SendMessage(node+";"+switchid+";1;0;2;1");
  return true;
});

will do the job? I'm not an expert just guessing ;-)

Code: [Select]
switch(command)
    {

          case "Control.On":
                SerialPort.SendMessage(node+";"+switchid+";1;0;2;1");
      Program.RaiseEvent(module, "Status.Level", "1", "Switch" + switchid);
                module.Parameter("Status.Level").Value ="1";

res = "{ 'ResponseValue' : 'OK' }";
break;
          // eg. http://hg_address/api/HomeAutomation.ArduinoSwitches/1/Control.Off
      case "Control.Off":
               
               SerialPort.SendMessage(node+";"+switchid+";1;0;2;0");      
               Program.RaiseEvent(module, "Status.Level", "0", "Switch " + switchid);
               module.Parameter("Status.Level").Value ="0";
               

        res = "{ 'ResponseValue' : 'OK' }";
      break;
    }

Thanks again
Cheers,
siepacz

July 07, 2016, 02:43:23 PM
Reply #5

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
I'm not sure whether you will be able to have multiple things talking to the serial port, I would suspect not and would suggest it was a bad idea.

I would create a method/api call that allows you to pass messages through to the program that 'owns' the serial port

I've done similar for my Text Messaging app as the app that processes and receives the messages owns  the serial port and programs that want to sent messages send them in via the api.

July 07, 2016, 04:53:31 PM
Reply #6

siepacz

  • *
  • Information
  • Newbie
  • Posts: 6
Hi David
Thanks for your time and advice. Now, let me digest  your post and do some homework ;-)
Can I use your SMS Messaging App as a staring point? (I'm not c# coder, need to learn a lot)
Any sample codes are more than welcome ;-)
Cheers,
siepacz

July 07, 2016, 05:19:02 PM
Reply #7

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
Ill dig it out for you tomorrow if I remember, about to go out on the beer :)

July 08, 2016, 10:25:17 AM
Reply #8

siepacz

  • *
  • Information
  • Newbie
  • Posts: 6
Thanks Dave.
Enjoy your beer  8) (I'll do the same tonight he he)
Anyway, good news, found the problem. Of course it was my fault. did not read mysensors serial protocol manual carefully enough, where critical information is at the very first line  ;D
The serial commands has the following format:
node-id;child-sensor-id;message-type;ack;sub-type;payload\n
All commands ends with a newline

So, small adjustment does the job:
SerialPort.SendMessage(node+";"+switchid+";1;0;2;0\r\n");   
Everything works as expected  :)

Dave, thanks again for troubleshooting guidance. Problem Solved  8)
Cheers,
siepacz