January 1

Using OpenDMX in Linux

Enttec makes a handy little USB to DMX converter called OpenDMX. (DMX is the standard theatrical lighting control protocol – a serial protocol where up to 512 lights are daisy chained together). Several people have made Linux kernel drivers for this device – for example: the DMX4Linux project and Erwin Rol – however, at the time I got one of these dongles, they either didn’t work with the 2.6.x kernels or required compiling a kernel module (irritating when apt-get keep installing new ones requiring a module recompile…). So, I set about using another approach – using just the stock Linux serial drivers.

The OpenDMX device actually uses a standard FTDI USB to Serial IC that is well supported by Linux. The main tricky bit is persuading Linux to use the unusual baud rate used by DMX (250Kbits/sec). Handily recent linux kernels (the earliest I’ve tried is 2.6.17) allow you to set the data rate of a serial port to the required value.

The second tricky bit is that DMX requires that the lighting data is sent out continuously. The OpenDMX has no data buffer of it’s own so it relies on the PC software to keep spitting out all of the DMX data. I knocked together a crude daemon that memory maps a file that stores the lighting levels and keeps spitting the values out to the OpenDMX device.

Here it is the code – admittedly lacking error checking, or configuration parameters – but, you get the idea:

/*
 * $Id: dmx_server.cpp,v 1.2 2008/10/04 17:05:22 anthony Exp $
 */

using namespace std;

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <iostream>
#include <sys/mman.h>

#include <termios.h>
#include <linux/serial.h>
#include <termio.h>

#define DMXDEVICE "/dev/ttyUSB0"

int main(int argc, char* argv[])
{
	int mcr=0;
	struct termios newtio;
	struct serial_struct advsettings;
	unsigned char *buffer;

	int res;
	int fd;
	int statefd;
	cout << "Opening state file..." << endl;
	statefd=open("/var/dmx/state",O_RDONLY);
	cout << "mmap'ing state file..." << endl;
	buffer = static_cast<unsigned char *>(mmap(0, 513, PROT_READ, MAP_SHARED, statefd, 0));

	cout << "Opening output device... " << endl;
	// Open write-only, not controlling TTY
	fd = open(DMXDEVICE,O_WRONLY | O_NOCTTY);
        if (fd < 0) {
    	perror("open");
		exit(-1);
	}

	// Now setup serial port as required:
	// First setup custom baud rate
	ioctl(fd,TIOCGSERIAL,&advsettings);
        printf("Before: Baudbase: %i Divisor: %i \n",advsettings.baud_base,advsettings.custom_divisor);

	advsettings.flags = (advsettings.flags & ~ASYNC_SPD_MASK) | ASYNC_SPD_CUST;
	advsettings.custom_divisor = advsettings.baud_base / 250000;

	printf("After: Baudbase: %i Divisor: %i \n",advsettings.baud_base,advsettings.custom_divisor);

	ioctl(fd,TIOCSSERIAL,&advsettings);

	// First zero all fields in struct:
	bzero(&newtio, sizeof(newtio));

	// 8bit, 2stopbits, ignore modem signals
	newtio.c_cflag = CS8 | CSTOPB | CLOCAL | B38400;

	tcflush(fd,TCIFLUSH);
	tcsetattr(fd,TCSANOW,&newtio);

	// Set RTS High:
	ioctl(fd, TIOCMGET, &mcr);
        mcr &= ~TIOCM_RTS;
        ioctl(fd, TIOCMSET, &mcr);

	cout << "Running." << endl;
	while(1) {
		// Wait for TX buffer to empty:
		tcdrain(fd); 

		// 88us break
		ioctl(fd,TIOCSBRK);
		usleep(88);
		ioctl(fd,TIOCCBRK);
		usleep(8);

		res = write(fd, buffer, 513);

		if (res < 0){
			perror("write");
			exit(-1);
		}

	}

	close(fd);

	munmap(buffer, 513);
	close(statefd);

	return 0;
}

Tags:
Copyright 2021. All rights reserved.

Posted January 1, 2009 by anthony in category "Open-source software

3,114 COMMENTS :

  1. By Steph Reed on

    Thanks loads for this! After problems with both the Erwinrol and DMX4Linux drivers I actually came up with the same idea.. it’s great someone’s already done it!

  2. By Peter on

    Dear Anthony,
    can I use your Code as open-source in my DMX project? I am planning to extend it a little more, add a socket for network control.

  3. By ajgreen (Post author) on

    @Peter
    Sure Peter, you are welcome to use the code on this page in your open-source project. If you release it on the web, let me know and I’ll put up a link.

  4. By MattaLor on

    Nicely put. With thanks! compare rx prices online pharmacy drugstore

  5. By MattaLor on

    You’ve made the point. canadian pharmacy cialis 20mg cialis online pharmacy

  6. By GregorPYCLE on

    Lovely content. Thanks a lot! canadian discount pharmacy no prior prescription required pharmacy

  7. By CorezDumma on

    Excellent forum posts. Thanks a lot. prescription drug assistance cheap prescription drugs

  8. By MattaLor on

    Thanks. A good amount of posts!
    canadadrugsonline best online canadian pharmacy

  9. By GregorPYCLE on

    Thanks a lot. A lot of facts!
    non prescription online pharmacy reviews drugs from canada online

  10. By MattaLor on

    Cheers, Plenty of data.
    medical pharmacy north west pharmacy canada

  11. By MattaLor on

    You have made your position quite well!! canadian pharmacy without prescription northwest pharmacy/com

  12. By MattaLor on

    Thanks a lot, Terrific stuff. pharmacy drugstore online buy cialis no 1 canadian pharcharmy online

  13. By GregorPYCLE on

    You reported that adequately. ordering prescriptions from canada legally walmart online pharmacy

  14. By MattaLor on

    You mentioned that terrifically. cvs pharmacy online most reliable canadian pharmacies

  15. By MattaLor on

    Terrific material. Thanks a lot! generic viagra online pharmacy Cialis Pzn canada drugs pharmacy online

  16. By CorezDumma on

    You expressed that adequately. pain meds online without doctor prescription canadian pharmacy online viagra

  17. By MattaLor on

    Seriously all kinds of beneficial information. pharmacy online shopping cheap cialis mexican pharmacy online

  18. By MattaLor on

    Regards. Numerous forum posts.
    canada pharmaceuticals online my canadian pharmacy

  19. By CorezDumma on

    Regards. Lots of posts!
    canadian drug stores online canadian pharcharmy online no precipitation

  20. By MattaLor on

    Reliable info. With thanks. pharmacy cheap no prescription my homework buy medication without an rx

  21. By MattaLor on

    Truly plenty of beneficial info. pain meds online without doctor prescription essay on paper online pharmacy busted

  22. By MattaLor on

    Helpful data. Appreciate it! international pharmacies that ship to the usa mail order pharmacies

  23. By MattaLor on

    You explained this fantastically! cialis generic pharmacy online canada viagra

  24. By MattaLor on

    Wonderful material. Kudos. canadian king pharmacy canadian pharmacy online

  25. By MattaLor on

    You suggested it superbly. international drug mart canadian pharmacy online store online rx pharmacy

  26. By CorezDumma on

    Thanks a lot, I like this. buy prescription drugs without doctor online pharmacy without a prescription

  27. By GregorPYCLE on

    You’ve made your point extremely nicely!! drugs without prescription canadian pharcharmy online

  28. By GregorPYCLE on

    Reliable advice. With thanks! pills viagra pharmacy 100mg online pharmacy without a prescription

  29. By MattaLor on

    Beneficial forum posts. Appreciate it. canadian pharmacy reviews top rated online canadian pharmacies

  30. By MattaLor on

    Thanks! Good stuff! canadian drugs pharmacy northwest pharmacy/com

  31. By GregorPYCLE on

    Point very well utilized.! prescription cost comparison reputable canadian mail order pharmacies

  32. By MattaLor on

    Amazing quite a lot of great material! canadian pharmacy king 25mg Cialis trusted online pharmacy reviews

  33. By GregorPYCLE on

    You actually expressed that exceptionally well! canadian drugs pharmacies online canadia online pharmacy

  34. By MattaLor on

    Whoa quite a lot of useful tips! walgreens online pharmacy health canada drug database

  35. By MattaLor on

    Awesome material. Regards. canadian online pharmacies legitimate by aarp phd committee canadian pharmacy certified canada pharmacy online

  36. By MattaLor on

    Incredible a lot of helpful knowledge! price pro pharmacy canada canadian pharmacy viagra generic

  37. By MattaLor on

    You stated that wonderfully. canadian pharma companies canadadrugs pharmacy

  38. By CorezDumma on

    Good content. With thanks! canadian pharmaceuticals online safe north west pharmacy canada

  39. By MattaLor on

    Whoa quite a lot of wonderful knowledge. canadian pharmacy king price pro pharmacy canada

  40. By MattaLor on

    You definitely made your point! safeway pharmacy buy prescription drugs from canada

  41. By MattaLor on

    Lovely material, Thank you! pharmacy online shopping online pharmacy india

  42. By MattaLor on

    Great info, Kudos! canadian pharmacy cialis mexican pharmacies

  43. By MattaLor on

    Nicely put. Kudos. ordering prescriptions from canada legally cialis canadian drugs pharmacies online

  44. By MattaLor on

    You actually stated this really well! canadian pharmacies online canada meds canadian online pharmacies legitimate by aarp

  45. By Robertfaf on

    Truly a lot of valuable information. prescription discount top rated canadian pharmacies online

  46. By Scottjaf on

    Regards, An abundance of knowledge.
    canadian pharcharmy onlinecanadian online pharmacy pharmacy drugstore online

  47. By CorezDumma on

    You stated this perfectly. canadian pharcharmy onlinecanadian online pharmacy pharmacy intern

  48. By MattaLor on

    You actually said this wonderfully! canadian pharcharmy online canada pharmacies online

  49. By MattaLor on

    You expressed this terrifically! order prescriptions online without doctor canada drugs prescription drugs from canada

  50. By MattaLor on

    Thanks a lot! Good stuff. canadian pharmacies online prescriptions legal canadian prescription drugs online

  51. By MattaLor on

    Cheers. Quite a lot of information.
    canadian drugs canada drugs online pharmacy

  52. By MattaLor on

    Whoa loads of fantastic facts. ordering prescriptions from canada legally canadian drug reputable canadian mail order pharmacies

  53. By GregorPYCLE on

    Effectively voiced truly. ! pharmacy prices [url=https://canadianpharmaciesrxstore.com/]overseas pharmacy forum[/url] medicine online shopping

  54. By MattaLor on

    Thank you! Helpful stuff! buy prescription drugs from canada canadian pharmacies top best

  55. By MattaLor on

    Excellent information. Cheers! viagra from canada internet pharmacy

  56. By MattaLor on

    Nicely put, Kudos! us pharmacy no prior prescription buy drugs online

  57. By MattaLor on

    Helpful advice. Thanks a lot! canada pharmacy online orders best canadian online pharmacy

  58. By MattaLor on

    With thanks! I appreciate this! shoppers pharmacy modafinil online pharmacy

  59. By infoforwomen.be on

    whoah this blog is magnificent i really like studying your posts. Stay up the great work! You recognize, many people are hunting round for this info, you could help them greatly. carme.infoforwomen.be/map32.php ph balans nypon

  60. By Albertjeara on

    Ребята только что сделали сайт мне, находится по ссылке:
    ссылке
    Расскажите, пожалуйста, свои впечатления. Сябки)

  61. By ThomasSmaws on

    Ребята только что сделали сайт мне, находится по ссылке:
    сюда
    Расскажите, пожалуйста, свои впечатления. Благодарю!

  62. By Amadotug on

    Ребята только что сделали сайт мне, находится по ссылке:
    тут
    Расскажите, пожалуйста, свои замечания. Сябки)

  63. By FrankBomia on

    Ребята только что сделали веб-сайт мне, находится по ссылке:
    здесь
    Расскажите, пожалуйста, свои рекомендации. Благодарю!

  64. By Antonmog on

    Ребята только что сделали вебсайт мне, находится по ссылке:
    моя ссылка
    Расскажите, пожалуйста, свои эмоции. Благодарю!

  65. By PatrickCoems on

    Ребята только что сделали сайт-визитку мне, находится по ссылке:
    вот такую
    Расскажите, пожалуйста, свои эмоции. Благодарю!

  66. By Jamesrug on

    Ребята только что сделали сайт-визитку мне, находится по ссылке:
    этого
    Расскажите, пожалуйста, свои замечания. Спасибо.

  67. By Darrylpaf on

    Ребята только что сделали веб-сайт мне, находится по ссылке: вот такой
    Расскажите, пожалуйста, свои эмоции. Благодарю!

  68. By AaronKaf on

    Ребята только что сделали вебсайт мне, находится по ссылке:
    вот тут
    Расскажите, пожалуйста, свои рекомендации. Спасибо.

Comments are closed.