Changechan

From MythTV Official Wiki
Jump to: navigation, search
/* changechan.c (Linux) for changing channel via serial port - works with D12-300 */

#include <stdio.h>
#include <termio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main(int argc,char *argv[])
{
	int chan;
	int port;
	int baud,parity,stop;
	struct termio term;	
	char buff[8];

	
	if (argc != 2)
		return(1);

	chan = atoi(argv[1]);

	port = open("/dev/ttyS0",O_RDWR);
	if (port < 0)
	{
		printf("Cannot open port");
		return(1);
	}

	baud=B9600;
	parity=0;
	stop=1;

	/*
	** Get the current setting just for grins.
	*/
	if(ioctl(port, TCGETA, &term))
	{
		perror("ioctl TCGETA");
		return -1;
	}

	term.c_iflag = IGNBRK;
	term.c_oflag = 0;		/* No output processing		*/
	term.c_lflag = 0;		/* No lexical processing	*/
	term.c_cc[VMIN] = 0;		/* Block forever for one input	*/
	term.c_cc[VTIME] = 100;		/*  character; no timeouts	*/

	/*
	** Set baud rate, eight bits, enable receiver, no modem control.
	*/
	term.c_cflag = baud | CS8 | CREAD | CLOCAL;
	/*
	** Set two stop bits if indicated.
	*/
	if(stop == 2)
		term.c_cflag |= CSTOPB;
	/*
	** Enable parity checking, odd or even as indicated.
	*/
	if(parity != 0)
	{
		term.c_cflag |= PARENB;
		if(parity == 1)
			term.c_cflag |= PARODD;
	}
	/*
	** Set up the port.
	*/
	if(ioctl(port, TCSETA, &term))
	{
		perror("ioctl TCSETA");
		return -1;
	}


	// You may need to use this longer command format on newer boxes
	sprintf(buff,"%c%c%c%c%c%c",0xfa,0xA6,chan >> 8,chan & 0xff,255,255);
	write(port,buff,6);

	// Format for older boxes
	//sprintf(buff,"%c%c%c%c",0xfa,0x46,chan >> 8,chan & 0xff);
	//write(port,buff,4);

	// sleep
	sleep(4);

	return 0;
}