TLC59116 - C program example 1

User documentation
10/04/2013

#include <string>
#include <cstring>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <iomanip>
#include <iostream>
#include <cmath>
#include <sstream>

using namespace std;

string hexant(int a) {
a--;
stringstream ss;
ss << setw(2)<<setfill('0')<<hex << static_cast<int>(0x01*pow(4,a));

return ss.str();

}

void stringupcount(int tty_fd, string sa, string sb,int sleep1){
for( int c=1;c<5;c++){
string sender = sa + hexant(c) + sb;
//cout << sender << '\n';
//cout.flush();
for(int z=0;z<sender.length();z++){
usleep(100);
write(tty_fd,&sender.at(z),1);
//cout << sender.at(z);
}

usleep(sleep1);
}
}

void stringdowncount(int tty_fd,string sa, string sb,int sleep1){
for( int c=4;0<c;c--){
string sender = sa + hexant(c) + sb;
for(int z=0;z<sender.length();z++){
usleep(100);
write(tty_fd,&sender.at(z),1);
}

usleep(sleep1);
}
}

int main(int argc,char** argv) {
struct termios tio;
struct termios stdio;
struct termios old_stdio;
int tty_fd;

unsigned char c='D';
tcgetattr(STDOUT_FILENO,&old_stdio);
argv[1]="/dev/ttyUSB0";
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
cfsetospeed(&tio,B115200); // 115200 baud
cfsetispeed(&tio,B115200); // 115200 baud
tcsetattr(tty_fd,TCSANOW,&tio);
string sender0;
sender0 = "sc0 80 00 p sc0 94 00 00 00 00p"; // setzt mode 1 und alle LEDs auf aus
for(int z=0;z&lt;sender0.length();z++){
usleep(10);
write(tty_fd,&sender0.at(z),1);
cout << sender0.at(z);
}

while(1){

stringupcount(tty_fd, "sc0 94 ", " 00 00 00 p",30000);
stringupcount(tty_fd, "sc0 94 00 ", " 00 00 p",30000);
stringupcount(tty_fd, "sc0 94 00 00 ", " 00 p",30000);
stringupcount(tty_fd, "sc0 94 00 00 00 ", " p",30000);
stringdowncount(tty_fd, "sc0 94 00 00 00 ", " p",30000);
stringdowncount(tty_fd, "sc0 94 00 00 ", " 00 p",30000);
stringdowncount(tty_fd, "sc0 94 00 ", " 00 00 p",30000);
stringdowncount(tty_fd, "sc0 94 ", " 00 00 00 p",30000);

}
sender0 = "sc0 80 00 p sc0 94 00 00 00 00p";
for(int z=0;z<sender0.length();z++){
usleep(100);
write(tty_fd,&sender0.at(z),1);
cout << sender0.at(z);
}

//if (read(tty_fd,&c,1)>0)        write(tty_fd,&c,1);              // if new data is available on the serial port, print it out
//if (read(STDIN_FILENO,&c,1)>0) write(tty_fd,&c,1); // if new data is available on the console, send it to the serial port

//}
close(tty_fd);
tcsetattr(STDOUT_FILENO,TCSANOW,&old_stdio);

return EXIT_SUCCESS;
}

Files