Author Topic: RS232  (Read 275 times)

Offline r4arnys

  • Newbie
  • *
  • Posts: 13
  • Karma: +0/-0
RS232
« on: March 26, 2018, 10:09:41 am »
Is there not any way to send a simple command to a serial device connected to the usb port which is recognised by Vera as Name:ftdi_sio / device number : usb-xhci-hcd-1 / port:3481 / baud changed to:1200 / no path / no used by device set ?

I thought something simple from within a scene (or pleg) like luup.io.write('off\r\n') should work but it seems it is far more complicated than this and I haven't found anything that relates (other than write a plugin which is putting the cart before the horse)

Any pointers gratefully accepted

Offline tinman

  • Full Member
  • ***
  • Posts: 139
  • Karma: +6/-0
Re: RS232
« Reply #1 on: April 10, 2018, 09:33:50 am »
I've been wanting to do some work with a serial device for some time. I've finally taken the time to try some things out. From my experience you must create a minimal set of device files, upload them to Vera and then create a device using the Create Device option in the Apps -> Develop Apps page. I only added three files so you can reference them when you create the device.
A device file (D_mydevice1.xml):
Code: [Select]
<?xml version="1.0"?>
<root xmlns="urn:schemas-upnp-org:device-1-0">
  <specVersion>
    <major>1</major>
    <minor>0</minor>
  </specVersion>
  <device>
    <deviceType>urn:schemas-mydevice-com:device:mydevice:1</deviceType>
    <staticJson>D_mydevice.json</staticJson>http://forum.micasaverde.com/index.php/board,45.0.html
    <serviceList>
      <service>
        <serviceType>urn:schemas-micasaverde-com:service:HaDevice:1</serviceType>
        <serviceId>urn:micasaverde-com:serviceId:HaDevice1</serviceId>
        <SCPDURL>S_HaDevice1.xml</SCPDURL>
      </service>
    </serviceList>
  </device>
</root>

An implementation file (I_mydevice.xml):
Code: [Select]
<?xml version="1.0"?>
<implementation>
<settings>
<protocol>raw</protocol>
</settings>
  <startup>
    <code>
      luup.log("running startup code",51)
local IOdevice = luup.variable_get("urn:micasaverde-com:serviceId:HaDevice1", "IODevice",lul_device)
if ((luup.io.is_connected(lul_device) == false) or (IOdevice == nil))
then
error("Serial port not connected. First choose the seial port and restart the lua engine.")
task("Choose the Serial Port", TASK_ERROR_PERM)
return false
end

luup.log("Serial port is connected")

-- Check serial settings
local baud = luup.variable_get("urn:micasaverde-org:serviceId:SerialPort1", "baud",tonumber(IOdevice))
if ((baud == nil) or (baud ~= "9600"))
then
error("Incorrect setup of the serial port. Select 9600 bauds.")
task("Select 9600 bauds for the Serial Port", TASK_ERROR_PERM)
return false
end

luup.log("Baud is 9600")
    </code>
  </startup>

  <xactionList>
  </xactionList>
</implementation>

And a JSON file so it appears on the devices page (D_mydevice.json):
Code: [Select]
{
"default_icon": "zwave_default.png",
"x": "2",
"y": "3",
"inScene": "1",
"Tabs": [
{
"Label": {
"lang_tag": "tabname_control",
"text": "Control"
},
"Position": "0",
"TabType": "flash",
"top_navigation_tab": 1,
"ControlGroup": [
{
"id": "1",
"scenegroup": "1",
"isSingle": "1"
},
{
"id": "2",
"scenegroup": "1",
"isSingle": "1"
}
],
"SceneGroup": [
{
"id": "1",
"top": "2",
"left": "0",
"x": "2",
"y": "1"
}
],
"Control": [
{
"ControlGroup": "2",
"ControlType": "button",
"top": "0",
"left": "0",
"Label": {
"lang_tag": "cmd_on",
"text": "On"
},
"Display": {
"Service": "urn:upnp-org:serviceId:SwitchPower1",
"Variable": "Status",
"Value": "1",
"Top": 60,
"Left": 50,
"Width": 75,
"Height": 20
},
"Command": {
"Service": "urn:upnp-org:serviceId:SwitchPower1",
"Action": "SetTarget",
"Parameters": [
{
"Name": "newTargetValue",
"Value": "1"
}
]
}
},
{
"ControlGroup": "1",
"ControlType": "button",
"top": "0",
"left": "1",
"Label": {
"lang_tag": "cmd_off",
"text": "Off"
},
"Display": {
"Service": "urn:upnp-org:serviceId:SwitchPower1",
"Variable": "Status",
"Value": "0",
"Top": 60,
"Left": 145,
"Width": 75,
"Height": 20
},
"Command": {
"Service": "urn:upnp-org:serviceId:SwitchPower1",
"Action": "SetTarget",
"Parameters": [
{
"Name": "newTargetValue",
"Value": "0"
}
]
}
}
]
},
{
"Label": {
"lang_tag": "settings",
"text": "Settings"
},
"Position": "1",
"TabType": "javascript",
"ScriptName": "shared.js",
"Function": "simple_device"
},
{
"Label": {
"lang_tag": "advanced",
"text": "Advanced"
},
"Position": "2",
"TabType": "javascript",
"ScriptName": "shared.js",
"Function": "advanced_device"
},
{
"Label": {
"lang_tag": "device_options",
"text": "Device Options"
},
"Position": "3",
"TabType": "javascript",
"ScriptName": "shared.js",
"Function": "device_zwave_options"
},
{
"Label": {
"lang_tag": "logs",
"text": "Logs"
},
"Position": "4",
"TabType": "javascript",
"ScriptName": "shared.js",
"Function": "device_logs"
},
{
"Label": {
"lang_tag": "notifications",
"text": "Notifications"
},
"Position": "5",
"TabType": "javascript",
"ScriptName": "shared.js",
"Function": "device_notifications"
}
],
"sceneList": {
"group_1": {
"cmd_1": {
"label": "ON",
"serviceId": "urn:upnp-org:serviceId:SwitchPower1",
"action": "SetTarget",
"arguments": {
"newTargetValue": "1"
},
"display": {
"service": "urn:upnp-org:serviceId:SwitchPower1",
"variable": "Status",
"value": "1"
}
},
"cmd_2": {
"label": "OFF",
"serviceId": "urn:upnp-org:serviceId:SwitchPower1",
"action": "SetTarget",
"arguments": {
"newTargetValue": "0"
},
"display": {
"service": "urn:upnp-org:serviceId:SwitchPower1",
"variable": "Status",
"value": "0"
}
}
}
},
"eventList2": [
{
"id": 1,
"label": {
"lang_tag": "ui7_a_device_is_turned_on_off",
"text": "A device is turned on or off"
},
"serviceId": "urn:upnp-org:serviceId:SwitchPower1",
"argumentList": [
{
"id": 1,
"dataType": "boolean",
"defaultValue": "1",
"allowedValueList": [
{
"Off": "0",
"HumanFriendlyText": {
"lang_tag": "ui7_hft_device_turned_off",
"text": "Whenever the _DEVICE_NAME_ is turned off"
}
},
{
"On": "1",
"HumanFriendlyText": {
"lang_tag": "ui7_hft_device_turned_on",
"text": "Whenever the _DEVICE_NAME_ is turned on"
}
}
],
"name": "Status",
"comparisson": "=",
"prefix": {
"lang_tag": "ui7_which_mode",
"text": "Which mode"
},
"suffix": {}
}
]
}
],
"DeviceType": "urn:schemas-xbee-com:device:xbee:1",
"device_type": "urn:schemas-xbee-com:device:xbee:1"
}

There's a lot of unneeded stuff in the JSON file but I didn't bother to get rid of it. Right now the buttons that appear don't do anything and if clicked will report errors.
Make sure the baud rate in the implementation file matches what you need.
Connect your device to the USB port and then upload your files to Vera. This should cause luup to reload. I'm actually using a powered hub to which I have connected another serial device and a flash drive. Everything works.
In the Develop Apps page, select the create device option and fill in the Device Type field with the <device type> in your D_mydevice1.xml file. Enter that device file name in the Upnp Device File name field. Enter the I_mydevice.xml filename in the Upnp Implementation Filename field. Give your device a name in the Description field. That's all you need. Then click the Create Device button.
You may have to cause luup to reload again. I did this by merely uploading one of the same files again.
Then go to the Develop Apps -> Serial port configuration. If your serial device is alive you should see a new port there. Make sure the baud rate and other options are set to what you need. Then select your device from the Used By Device drop-down and save it.
At that point you should be able to see your device in the Vera devices screen. Go to the device's advanced page and note the device ID number. If you look at the device's variables you should see an IOdevice listed.
If all of this has worked you can now go back to the Apps -> Develop Apps page and in the Test Luup code box enter Lua code to be executed. To send serial data to your device enter this:
luup.io.write("some test text", yourDeviceNumber)
Make sure you use the device number of your created device and not the number you may have seen for the serial port.
I haven't yet tried to capture anything that is sent by my serial device. I expect that adding something like this to the implementation file may work:
Code: [Select]
<incoming>
<lua>luup.log(lul_data)</lua>
</incoming>
This will at least capture something in the Vera log.
All of this is a bit crude but I think it's a good starting point. I hope it's of some use to you.