2011年12月20日火曜日

Linuxでシリアル接続:screen利用

Linuxでシリアル接続をするときのメモ

1. screenインストール
atmark@atde3:~$ sudo apt-get install screen

2. screenで接続
$ screen /dev/ttyUSB0 115200

/dev/ttyUSB0 は変わる場合があります。

システムプログラミング:プロセス間通信(メッセージキュー利用)

[msend.c]

#include <stdio.h>
#include <stdlib.h>
#include <="" span="">ipc.h>
#include <="" span="">types.h>
#include <unistd.h>
int main(){
  struct msgbuf{
  long mtype;
  char mtext [256];
  };
  char line [256];
  int msqid;
  key_t msgkey;
  struct msgbuf msgdata, *p;

  p=&msgdata;
  printf (“Enter message: “);
  fflush (stdout);
  fgets (line, sizeof (line), stdin);
  if (sscanf (line, “%[^\n]”, p->mtext) >0){
  // プロセスIDをmtypeに入れる
  p->mtype=getpid ();
  msgkey=ftok (“mrecv”, ‘a’);
  // メッセージキュー識別子を得る
  msqid=msgget (msgkey, IPC_CREAT | 0666);
  // メッセージを送信する
  msgsnd (msqid, p, sizeof (p->mtext), 0);
  }
  return EXIT_SUCCESS;
}

[mrecv.c]
#include <stdio.h>
#include <stdlib.h>
#include <="" span="">ipc.h>
#include <="" span="">msg.h>
#include <="" span="">types.h>
int main(){
  struct msgbuf{
  long mtype;
  char mtext [256];
  };
  int msqid;
  key_t msgkey;
  struct msgbuf msgdata, *p;

  p=&msgdata;
  msgkey=ftok (“mrecv”, ‘a’);
  // メッセージキュー識別子を得る
  msqid=msgget (msgkey, IPC_CREAT | 0666);
  while (1){ //無限ループ
  // メッセージを受信する
  msgrcv (msqid, p, sizeof (p->mtext), 0, 0);
  printf (“message received from %ld\n%s\n” ,p->mtype, p->mtext);
  }
  return EXIT_SUCCESS;
}

atmark@atde3:~/Desktop/work$ gcc -o mrecv mrecv.c
atmark@atde3:~/Desktop/work$ gcc -o msend msend.c
atmark@atde3:~/Desktop/work$ ./msend
Enter message: Hello
atmark@atde3:~/Desktop/work$ ./msend
Enter message: Good
atmark@atde3:~/Desktop/work$ ./mrecv
message received from 10658
Hello
message received from 10659
Good

システムプログラミング:シグナル利用

Ctr+Z(SIGTSTP)を押したら経過時間を表示するプログラム


#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
time_t start;
int main(){
  void stop ();
  time (&start);
  // SIGTSTPが発生したら関数stopを実行
  signal (SIGTSTP, stop);
  while (1){
  sleep (1);
  printf (“ . “);
  fflush (stdout);
  }
  return EXIT_FAILURE;
}

void stop (){
  time_t end;
  time (&end);
  printf (“\n”);
  printf (“elapsed time= %ld seconds\n”, end-start);
  exit (EXIT_SUCCESS);
}

atmark@atde3:~/Desktop/work$ ./timer
 .  .  .  .  .  .  .  .  .  . ^Z\nelapsed time= 10 seconds

システムプログラミング:プロセス間通信(pipe利用)

親プログラムで入力したものを、子プログラムを処理し親プログラムに返す

[parent.c]
#include <stdio.h>
#include <stdlib.h>
#include <="" span="">types.h>
#include <="" span="">wait.h>
#include <unistd.h> 
int main(){
  char line[32];
  char read_fd[3], write_fd[3];
  int number, result;
  int fd[2];
  int st;
  if (pipe(fd) <0){
    perror("pipe");
    exit (EXIT_FAILURE);
  }

  snprintf (read_fd, sizeof (read_fd),"%d", fd[0]);
  snprintf (write_fd, sizeof (write_fd),"%d", fd[1]);
  fgets (line, sizeof (line), stdin);
  if (sscanf (line,"%d", &number) >0){
    if (fork () == 0){
      if (execl ("child", "child", read_fd, write_fd, NULL) <0){
        exit (EXIT_FAILURE);
      }
    }
    write (fd[1], &number, sizeof (number));
    wait (&st);
    read (fd[0], &result, sizeof (result));
    printf ("%d x %d = %d\n", number, number, result);
    close (fd[0]);
    close (fd[1]);
  }
  return EXIT_SUCCESS;
}


[child.c]
#include <stdlib.h>


#include <unistd.h>
int main (int argc, char *argv[]){
  int number, result;
  int read_fd, write_fd;
  // 引数の文字列を整数に変換
  read_fd = atoi (argv[1]);
  write_fd = atoi (argv[2]);
  // パイプラインからデータを読み込む
  read (read_fd, &number, sizeof (number));
  result = number*number;
  // パイプラインにデータを書き込む
  write (write_fd, &result, sizeof (result));
  close (read_fd);
  close (write_fd);
  return EXIT_SUCCESS;
}


atmark@atde3:~/Desktop/work$ ./parent
3
3 x 3 = 9\natmark@atde3:~/Desktop/work$