Example #1
0
void sig_handler(int signal)
{
    if (signal == SIGUSR1) {
        if (stopped) {
            stopped = 0;
            //printf("\nSignal received, opening port.\r\n");

            if (init() < 0) {
                //printf("Cannot open port.\r\n");
                close_tty();
                exit(1);
            }
        }
    }
    else if (signal == SIGUSR2) {
        if (!stopped) {
            stopped = 1;
            //printf("\nSignal received, closing port. \r\n");
            pthread_cancel(serial_reader);
            close_serial_port();
        }
    }
    else if (signal == SIGINT) {
        //printf("SIGINT received, exiting...\n");
        pthread_cancel(serial_reader);
        close_serial_port();
        close_tty();
        exit(0);
    }
}
Example #2
0
int main(int argc, char **argv)
{
    if (argc == 2) {
        port_name = argv[1];
    }

    //printf("Using %s as serial device.\n", port_name);

    char ttybuf[255];
    tty_fd = open_tty();

    if (tty_fd < 0) {
        //printf("Error opening terminal.\n");
        return (1);
    }

    install_sighandler();

    if (init() < 0) {
        //printf("Cannot open port.\r\n");
        exit(1);
    }

    while (1) {
        int n = read(tty_fd, ttybuf, sizeof(ttybuf));
        int i;

        /* check for 0x3 (ctrl-c), clean exit */
        for (i = 0; i < n; i++) {
            if (ttybuf[i] == 0x3) {
                if (i > 0) {
                    write_serial_port(ttybuf, i);
                }

                close_serial_port();
                close_tty();
                system("tset -c");
                return 0;
            }

        }

        write_serial_port(ttybuf, n);
    }

    close_tty();
    close_serial_port();
    return 0;
}
Example #3
0
int main(int argc, char **argv)
{
    if (argc < 3) {
        usage();
        exit(1);
    }

    char *port_name = argv[1];
    char *file_name = argv[2];

    if (open_serial_port(port_name) < 0) {
        return (1);
    }

    if (!download_begin(file_name)) {
        return 1;
    }

    while (!programming_done) {
        handle_port_input();
    }

    close_serial_port();

    return 0;
}
Example #4
0
int main(int argc,char **argv){
    byte port;
    int i;
    
    if(argc != 3 ){
        fprintf(stderr,"usage: %s com tx_data\t(com: B0=USB0, A0=AMA0)\n",argv[0]);
        return -1;
    }
    if( atoi(argv[1]) < 0 ){
        port = 0x9F + (byte)(-atoi(argv[1])) ;
    }else  if( ( argv[1][0]=='b' || argv[1][0]=='B' )&& argv[1][1]!='\0' ){
        port = 0xB0 + ( argv[1][1] - '0');
    }else  if( ( argv[1][0]=='a' || argv[1][0]=='A' )&& argv[1][1]!='\0' ){
        port = 0xA0 + ( argv[1][1] - '0');
    }else port = (byte)(atoi(argv[1]));
    if( sci_init(port) ==0 ){
        fprintf(stderr,"Serial Open ERROR\n");
        return -1;
    }
    if(strncmp(argv[2],"+++",3)==0){
        puts_serial_port("+++");
        sleep(1);
        usleep(100000);
    }else{
        puts_serial_port(argv[2]);
        puts_serial_port("\r");
    }
    do{
        i=gets_serial_port(rx_data,RX_MAX);
        if(i) printf("%s\n",rx_data);
    }while(i);
    close_serial_port();
    return 0;
}
Example #5
0
int close_rfcomm(){
/*
    open_rfcommで開いたプロセスをkillするモジュールです。
    戻り値はkillを実行した時のプロセスIDです。
    killが実行できなかった場合は0を応答します。
*/
    FILE *fp;
    char s[]="pidof /usr/bin/rfcomm |awk '{print $1;}'";
    int id;
    
    close_serial_port();
    fp=popen(s,"r");
    s[0]='\0';
    if(fp){
        if( feof(fp)==0 ) fgets(s,sizeof(s),fp);    // 終端では無い時にfpから値を読む
        fclose(fp);
    }
    
    id=atoi(s);                                     // atoiはセキュリティ対策と改行対策
    if(id>0){
        sprintf(s,"sudo kill %d",id);
        printf("[%s]\n",s);
        system(s);
    }
    return id;
}
Example #6
0
static int disconnect_device(void)
{
    int result;

    fprintf(stdout, TTY_NONE "Disconnecting...");

    if ((result = close_serial_port()))
        return result;

    return DONE;
}
Example #7
0
void termination_signal(int signum)
{
    int work = config.work;        
    config.work = 0;
    if (config.outputDevice != -1)
    {
        close_serial_port(config.outputDevice);
        config.outputDevice = -1;
    }
    if (work)
        kill(0, SIGTERM);
}
int main(){
    char s[32];                                     // 文字データ用
    int len=0;                                      // 文字長
    char c;                                         // 文字入力用の文字変数
    int ctrl=0;                                     // 制御用 0:先頭, -1~-3:「-」入力数
                                                    //        1:コマンド, 2:プログラム
    printf("Ichigo Term for Raspberry Pi\n");
    if(open_serial_port() <= 0){
        printf("UART OPEN ERROR\n");
        return -1;
    }
    printf("CONNECTED\nHit '---' to exit.\nTX-> ");
    write(ComFd, "\x1b\x10 CLS\n", 7);              // IchigoJamの画面制御
    while(1){
        if( kbhit() ){
            c=getchar();                            // キーボードからの文字入力
            s[len]=c;                               // 入力文字を保持
            if(len < 31) len++;                     // 最大長未満のときに文字長に1を加算
            write(ComFd, &c, 1 );                   // IchigoJamへ送信
            if( ctrl<=0 && c =='-' ){               // 先頭かつ入力された文字が「-」の時
                ctrl--;                             // ctrl値を1減算
                if(ctrl <= -3) break;               // 「-」が3回入力された場合に終了
            }else if(ctrl==0){
                if(isdigit(c)) ctrl=2; else ctrl=1; // 先頭に数値でプログラムと判定し、
            }                                       // そうで無い時はコマンド入力と判定
            if(c=='\n'){                            // 入力文字が改行の時
                if(ctrl==2 || len==1) printf("TX-> ");  // 無入力とプログラム入力時
                ctrl=0;                             // 入力文字状態を「先頭」にセット
                len=0;
                usleep(250000);                     // 250msの(IchigoJam処理)待ち時間
            }
            usleep(25000);                          // 25msの(IchigoJam処理)待ち時間
        }
        c=read_serial_port();                       // シリアルからデータを受信
        if(c){
            printf("\nRX<- ");                      // 受信を識別するための表示
            while(c){
                if( isprint(c) ) printf("%c",c);    // 表示可能な文字の時に表示する
                if( c=='\n' ) printf("\n     ");    // 改行時に改行と5文字インデントする
                if( c=='\t' ) printf(", ");         // タブの時にカンマで区切る
                c=read_serial_port();               // シリアルからデータを受信
            }
            s[len]='\0';
            printf("\nTX-> %s",s);                  // キーボードの入力待ち表示
        }
    }
    printf("\nDONE\n");
    close_serial_port();
    return 0;
}
Example #9
0
int main(int argc, char **argv)
{
	run_as_emulator(argv[0]);
	init_settings();
	term_fd = open_tty();
	if (term_fd < 0) {
		printf("Unable to open I/O to terminal (/dev/tty), r=%d\n",
			term_fd);
		sleep(1);
		exit(1);
	}
	create_window(&argc, &argv);
	run_gui();
	close_serial_port();
	close(term_fd);
	return 0;
}
Example #10
0
int main(int argc, char** argv) {
  #ifdef DEBUG
  printf("Debug mode\n");
  #endif

  printf("Opening port...\n"); 
  SerialPort maestro;
  open_serial_port(&maestro, "/dev/ttyACM0");
  printf("Port open with fd: %d\n", maestro.file_descriptor); 
  unsigned const char data[] =     "012341234";
  printf("Sending %d bytes... ", (int)sizeof(data));
  bool result;
  result = write_bytes(&maestro, data, sizeof(data)); 
  if (result) {
    printf("Success\n");
  } else {
    printf("Failure\n");
  }
  printf("Closing port...\n"); 
  close_serial_port(&maestro);
  printf("Port closed.\n"); 
  return 0;
}