トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS   ログイン

認知ロボティクス研究室>ゼミのお話>ロボット工学者養成所>一覧>シリアル通信によるデバイス制御>センサ情報取得プログラム作成>センサ値読取プログラム
01:#include <sys/types.h>
02:#include <sys/stat.h>
03:#include <sys/ioctl.h>
04:#include <fcntl.h>
05:#include <termios.h>
06:#include <unistd.h>
07:#include <stdio.h>
08:#include <stdlib.h>
09:#include <string.h>
10:#include <sys/time.h>
11:
12:#define SERIAL_PORT     "/dev/ttyAM1"
13:
14:int main(){
15:  int fd;
16:  fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY);
17:  if(fd < 0){
18:    printf("%s doesn't open it\n",SERIAL_PORT);
19:    return -1;
20:  }
21:
22:  struct termios oldtio, newtio;
23:  tcgetattr(fd, &oldtio);
24:  newtio = oldtio;
25:
26:  newtio.c_iflag = IGNPAR;
27:  /* 非カノニカル入力処理 */
28:  newtio.c_lflag = 0;
29:
30:  /* read関数での文字待ちうけの設定 */
31:  newtio.c_cc[VTIME] = 0; /* 値x0.1秒待つ */
32:  newtio.c_cc[VMIN] = 1; /* 値の文字分だけ入力されるまで待つ*/
33:
34:  /* バッファをクリア */
35:  tcflush(fd, TCIFLUSH);
36:  
37:  cfsetspeed(&newtio, B9600);
38:  tcsetattr(fd, TCSANOW, &newtio);
39:
40:  /* ボードの設定 */
41:  int id=120;
42:
43:  int i, j, k;
44:  unsigned char command[4] = {255, id, 1, 1};
45:  unsigned char data[20];
46:  
47:  for(i=1 ; i<=100 ; i++){
48:    printf("Getting data : %d :", i);
49:
50:    /* センサボードにデータ要請 */
51:    write(fd, command, sizeof(command));
52:    usleep(100000);
53:    /* 1文字目データ受信 */
54:    read(fd, &data[0], 1);
55:    if(data[0] == 255){
56:      read(fd, &data[1], 1);
57:      if(data[1] == id){
58:        read(fd, &data[2], 1);
59:        if(data[2] == 17){
60:          read(fd, &data[3], 1);
61:          if(data[3] == 1){
62:            for(k=4 ; k<20 ; k++){
63:              /* read(fd, data2, 16);とか聞くかな */
64:              read(fd, &data[k], 1);
65:            }
66:  
67:            for(k=4 ; k<7 ; k++){
68:              /* read(fd, data2, 16);とか聞くかな */
69:              printf("data = %d :", data[k]);
70:            }
71:            printf("\n");
72:          }
73:        }
74:      }
75:    }
76:  }
77:  
78:  tcsetattr(fd, TCSANOW, &oldtio);
79:  close(fd);
80:  return 0;
81:}

Last-modified: 2023-03-29 (水) 10:47:55