Friday 19 July 2013

Creating an Android Studio Desktop File for launching on your panel or toolbar in GNU/Linux

I spent a while (read: too much time) trying to get Android Studio launching on my KDE panel via an icon.

The program would not launch, even after editing the studio.sh script so the user wouldn't have to press the key. Since I compiled and installed IcedTea Java 7, it would always warn me before launch. Oracle Java is recommended for running Android Studio, but I take some issue with Oracle as a company, so I would prefer not to use it. Plus, on Gentoo, the package manager requires the manual downloading of oracle's java.bin files because they do not allow Gentoo to carry it in their package repository due to copyright or some crap like that.

Anyway, back to the application launching: even when exporting JAVA_HOME in my .bashrc file (which gets run every login shell), the application would fail to run. The fix for this is to manually add the path to the EXEC= line in the .desktop file. Here is mine as an example:

[Desktop Entry]
Comment=
Exec=JDK_HOME="/usr/lib/jvm/icedtea-7/" /home/celestia/Programs/android-studio/bin/studio.sh
Icon=/home/celestia/Pictures/Icons/android.png
Name=AndroidStudio
NoDisplay=false
Path[$e]=
StartupNotify=true
Terminal=0
TerminalOptions=
Type=Application
X-KDE-SubstituteUID=false
X-KDE-Username=

I used the KDE Menu Editor to generate the template, but regardless of how you create your .desktop file, it's the EXEC line you need.
If your paths are set up system-wide, I doubt there will be any errors when you run studio.sh without the JDK_HOME path.

Hope this helps.
Cheers!

Tuesday 9 July 2013

Speedway Revolution 420 RFID Tag Reader in Python

So I recently was working on connecting a piece of software to a reader via a socket.

The vendors had a great script for connecting to it via ruby, as shown in the code below:

require 'socket'

s = TCPSocket.new '192.168.2.165', 14150

while line = s.gets
    puts line.chop
end

s.close


This code is available, you can see it here in the video:
http://learn.impinj.com/articles/en_US/RFID/Reading-Tags-over-TCP-IP-Socket-Using-Speedway-Connect-Software/

The site presents .NET code for connecting as well. However, as I do most of my work in python, I figured I'd keep things simple and convert their script to it.

---

The reason I'm writing this post is because I had one hell of a time getting to the point where it *would* read, because it would keep failing on the connect() method and from then on, I would have a Connection Refused error whenever I ran the python OR the ruby script. If you are having troubles with the Speedway Revolution 420 reader, know this:

* If you get Connection Refused errors, you need to browse to the ReaderConnect software, found at the reader ip address on port 8080. Click the save button, even if you haven't made any changes. This will allow connections to be received again, as I imagine it closes whatever broken connection python has left behind.
* You must use python 2.7 (or 2.x series, though I haven't tested beyond that) in order for it to run. When my code was failing, the header was "#!/usr/bin/env python", which on my linux mint system, would default to python 3. It was only until I *explicitly* stated "#!/usr/bin/env python2.7" that it miraculously worked. Here is the code:

#!/usr/bin/env python2.7

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.2.165", 14150))

data = s.recv(1024)
data = data.strip('\n')
while 1:
    print data
    data = s.recv(1024)
    data = data.strip('\n')
    
s.shutdown(socket.SHUT_RD)
s.close()


It makes two assumptions:

1. You want to read 1024 bytes. 0 does NOT work, it leaves me with an empty screen because the data variable does not get assigned to anything more than an empty string.
2. If you set the output to Linefeed, or Carriage Return Linefeed, this script will work. If you don't set the line ending value in the Output tab, there could be some issues.

Hopefully this script can help someone, someday.