SRA Lab Open Source Code

18Apr/110

La DjangoCong 2011 est terminée

Grâce à Makina Corpus, j'ai pu assister à nouveau à la DjangoCong. C'est une occasion idéale pour retrouver l'équipe des Djangonautes de Makina au grand complet ;)

La nouvelle organisation du WE mettait l'accent sur la participation avec des mini-confs très courtes le matin (12 mn) et des ateliers à la barcamp, et encore une fois les organisateurs ont été irréprochables, bravo ! C'est un plaisir de retrouver des têtes connues et de rencontrer de nouveaux venus pleins de bonnes idées. Les entreprises étaient plus impliquées cette année (merci les sponsors) et l'écosystème autour de Django semble en bonne voie avec des produits tels Django SHOP et Django CMS,

Nous quittons Marseille avec quelques outils à utiliser au plus vite et de bonnes idées:

DjangoCong est la conférence Django francophone à suivre !

Quelques photos http://www.flickr.com/photos/vineolia/sets/72157626398900439/

14Apr/110

libmodbus for Arduino (almost!)

The libmodbus project is a real success on many platforms and to conquer the world, I've written a version dedicated to Arduino devices. My goals are:

  • to talk with a standard Arduino UNO or Duemilanove via the integrated USB
    serial line (8N1)
  • to have the lowest footprint (binary sketch size and SRAM)
  • to be robust

OK, you're right, other projects exist already:

  • Master/slave by jpmzometa and others is great but the reading in loop doesn't handle serial latency and the code contains too many arrays IMHO
  • ModbusMaster is a master and I want a slave.
  • Arduino-Modbus, the code is, eh, ugly!
  • modbusmq by Mario Di Bacco is based on libmodbus but it aims Modbus TCP.

So I've extracted/adapted only the slave, RTU, reading/writing registers of libmodbus. To reduce the footprint even further, I've replaced the fast CRC code based on precomputed arrays (512 bytes) by a slow one (not so slow!). The result is clean library for Arduino able to handle exceptions if an error occurs.

The binary sketch size is 2,450 bytes and around 1,830 bytes of memory still free when running on 2K SRAM model.

The source code is available on github and have the project is referenced in the arduino.cc playground wiki.

6Apr/110

New IRC channel

I've just registered #libmodbus channel on Freenode, so let's talk!

25Mar/110

The libmodbus documentation has landed

You've been numerous to ask for documentation and the work of gass with GTK-doc help me to prioritize it, so this month I've finally tackled this problem but not as expected!

At first, I loved the GTK-doc solution, I'm a GNOME lover after all, but GTK-doc can't generate man pages and the setup code is too cumbersome IMHO. To choose the best solution for libmodbus, I read a comparison matrix on Wikipedia, after some tests with GTK-doc, Doxygen and AsciiDoc, and I finally choose AsciiDoc.

However, there is a major difference between AsciiDoc and the other envisaged solutions, the documentation isn't included in the source code. I used to think it was good idea to keep the both at the same place to keep them in sync but:

  • it doesn't prevent you to code without updating the documentation
  • the documentation must be escaped by the language comment tags
  • I don't want to abuse my wheel mouse (to skip documentation) when I code

The 0MQ project, endless source of inspiration, was a very good starting point to learn some tricks with AsciiDoc.

Now, I'm looking for help to review and extend the documentation which has just landed in git (make doc to generate man pages). I've generated the documentation in HTML for the web site.

27Jan/110

Dynamic OR’d Queryset Creation with Django

To complete posts and comments published on Elf Sternberg's blog, this is the method I found to create dynamic complex queryset with Django (Q and or):

field = 'toto'
qs = Q(**{field + '__slug': 'AL'})
qs |= Q(**{field + '__isnull': True})
Document.objects.filter(qs)

PS: You can start with qs = Q() if you need a loop.

Tagged as: No Comments
17Jan/110

libmodbus v2.9.3 is out!

During this development cycle, I've received more feedback than for any other releases. That's really cool, thank you! With this new release, you can consider to migrate from the previous 2.0.x series for your applications.

libmodbus v2.9.3 (2011-01-14)

  • Major rewriting of the message reading (no more timeouts on exception)
  • New function to reply to an indication with an exception message modbus_reply_exception()
  • New function modbus_get_header_length(modbus_t *ctx)
  • New functions to manipulate data:
    • MODBUS_GET_INT32_FROM_INT16
    • MODBUS_GET_INT16_FROM_INT8
    • MODBUS_SET_INT16_TO_INT8
  • Fix GH-2. Read/write were swapped in _FC_READ_AND_WRITE_REGISTERS
  • Install an ignore handler for SIGPIPE on *BSD.
    Original patch by Jason Oster.
  • Fix closing of Win32 socket.
    Reported by Petr Parýzek.
  • Fix unit identifier not copied by the TCP server.
    Reported by Antti Manninen
  • Fix missing modbus_flush() in unit tests.
  • Fixes for OpenBSD by Barry Grumbine and Jason Oster

This time, the release is not only availabe as tarball but also as Fedora and Ubuntu packages on the download page.

For the next release, I've already merged my ipv6 branch (based on the work of Florian Forster), the rtai branch of Chris Cole is waiting for review (ah, RTAI, like in the good old days :) and I hope to take care of the gtk-doc work done by Luis Matos.

14Jan/110

libsoup and Django

If you need to interface C code with a Django app, you should consider to use libsoup. It's a nice HTTP client/server library based on GObject and integrated with the glib main loop.

This is the code I used to authenticate with libsoup against my Django application (and the code is similar for any forms):

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <glib.h>

#include <libsoup/soup.h>
#include <libsoup/soup-auth.h>

static char* django_get_csrftoken_from_body(SoupMessage *msg)
{
    char *csrftoken = NULL;
    char *p_found;

    p_found = g_strrstr_len(msg->response_body->data,
                msg->response_body->length,
                "csrfmiddlewaretoken");
    if (p_found != NULL) {
    csrftoken = g_strndup(p_found + strlen("csrfmiddlewaretoken' value='"),
                  strlen("c711343cd20077a3aa869fcc9b931ff3"));
    }
    return csrftoken;
}

static int django_authenticate(SoupSession *session, const char *base_uri)
{
    char *uri;
    SoupMessage *msg;
    char *csrftoken;

    uri = g_strconcat(base_uri, "login/", NULL);
    msg = soup_message_new(SOUP_METHOD_GET, uri);
    soup_session_send_message(session, msg);
    printf("Login Status Code: %d\n", msg->status_code);
    if (msg->status_code != 200) {
    g_object_unref(msg);
    g_free(uri);
    return -1;
    }

    csrftoken = django_get_csrftoken_from_body(msg);
    g_object_unref(msg);

    printf("CSRF token %s\n", csrftoken);

    msg = soup_form_request_new("POST", uri,
                "username", "MYUSERNAME",
                "password", "MYPASSWORD",
                "csrfmiddlewaretoken", csrftoken,
                NULL);
    soup_session_send_message(session, msg);
    printf("POST Login Status Code: %d\n", msg->status_code);
    printf("Message %s\n", msg->response_body->data);

    g_free(csrftoken);
    g_object_unref(msg);
    g_free(uri);

    return 0;
}

int main(int argc, char** argv)
{
    SoupSession *session;
    SoupCookieJar *jar;

    g_type_init();

    session = soup_session_sync_new();

    /* Accept all cookies */
    soup_session_add_feature_by_type(session, SOUP_TYPE_COOKIE_JAR);
    jar = SOUP_COOKIE_JAR(soup_session_get_feature(session,
                           SOUP_TYPE_COOKIE_JAR));
    soup_cookie_jar_set_accept_policy(jar, SOUP_COOKIE_JAR_ACCEPT_ALWAYS);

    django_authenticate(session, "http://localhost:8000/");

    soup_session_abort(session);
    g_object_unref(session);

    return 0;
}
5Dec/102

New libmodbus 2.9.2 with win32 support and backends

OK, I don't care about Windows but I'm happy libmodbus offers a bit of Open Source to this platform so thank you to Tobias Doerffel for this contribution. The other major change is the use of an internal backend to isolate the transport layers (only serial RTU and TCP/IPv4 for now).

libmodbus 2.9.2 (2010-12-05)

  • Fix segfault in bandwidth-server-many-up on inet_ntoa() call
  • Fix unit test of report slave ID in RTU
  • Fix GH-3. Remove inclusion of config.h in modbus.h
  • Correctly detect if we are cross
  • compiling for win32 by Kirill Smelkov.
  • Rename modbus_[listen|accept] to modbus_tcp_[listen|accept]
  • Fix setting of the broadcast address
  • Remove slave argument from modbus_new_rtu()
  • Win32 support by Tobias Doerffel
  • Split source code around RTU and TCP (backends)
  • Check received function code

So let's run your MinGW!

13Oct/108

Current Tranformer

At home or at work, you probably have spotlights which indicate only 20W or 35W of power consumption, but sadly these spotlights are powered by a lower voltage and so they require a transformer (hidden in the ceiling, a cupboard or a hood) which in turn consumes around 40W.

I've recently designed an application (with libmodbus, glib and Django) to measure the power consumption from various sensors built by LEM. By doing some tests at home, I've confirmed my suspicions about a transformer permanently plugged into the mains (0.18 * 224 = 40.3W for total of 80W when both spotlights are turned on).

So, I've replaced the shunt plug with a switch between the main and the transformer and once again I have a perfect 0W when off!

Do you know how many electricity transformers do you have around you?

3Oct/100

Not utf-8 by default

On Ubuntu 10.04 (tested on server and desktop editions), the PostgreSQL database created by default, uses the SQL_ASCII encoding!
It's not possible to create new utf-8 databases so check the encoding with psql -l as PostgreSQL user and you can use the following radical way (drop everything) to fix this:

rm -rf /var/lib/postgresql/8.4/main
/usr/lib/postgresql/8.4/bin/initdb -E UTF8 /var/lib/postgresql/8.4/main/
Tagged as: No Comments