- Timestamp:
- 03/31/08 00:24:08 (8 months ago)
- Files:
-
- 1 modified
-
lang/c/tds01v/sample.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/c/tds01v/sample.c
r8351 r8533 1 1 /* MacでUSB経由にてTDS01Vの値を取得する 2 2 */ 3 4 // nanosleepをつっこむ事で受信を待つという、暫定的な対応をしている。 5 // 同期・非カノニカルにする事で、ちゃんとした作りにできるはず。 3 6 4 7 #include<stdio.h> … … 9 12 #include<fcntl.h> 10 13 14 #include <unistd.h> 15 16 #include <time.h> 17 11 18 #define BAUDRATE B9600 /* 通信速度の設定 */ 12 #define MODEMDEVICE "/dev/tty.usbserial-0000103D" /* デバイスファイルの指定 */13 19 14 20 #define FALSE 0 15 21 #define TRUE 1 16 22 23 #define SLEEP_NSEC (99 * 1000 * 1000) // 99msec 24 17 25 volatile int STOP=FALSE; 18 26 19 int main( )27 int main(int argc, char *argv[]) 20 28 { 29 21 30 int fd, c, res; /* fd:ファイルディスクリプタ res:受け取った文字数 */ 22 31 struct termios oldtio, newtio; /* 通信ポートを制御するためのインターフェイス */ 23 32 char buf[255]; /* 受信文字を格納 */ 33 char crlf[2]; 24 34 25 if((fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ))==-1){ 35 char modem_device[4096]; 36 37 char send_command[4096]; 38 39 int i; 40 41 struct timespec treq, trem; 42 43 char dir_dig_str[5]; 44 short dir_dig; 45 46 treq.tv_sec = (time_t)0; 47 treq.tv_nsec = SLEEP_NSEC; 48 49 printf("argc = %d\n", argc); 50 51 argc--; 52 53 if(argc != 1){ 54 printf("specify usb device path.\n"); 55 return 1; 56 } 57 58 strcpy(modem_device, argv[1]); 59 60 if((fd = open(modem_device, O_RDWR | O_NOCTTY ))==-1){ 26 61 /* O_RDWR:読み書き両用 O_NOCTTY:tty制御をしない */ 27 perror( MODEMDEVICE);62 perror(modem_device); 28 63 exit(-1); 29 64 } … … 68 103 puts("START"); 69 104 105 // センサ情報項目設定 全センサ全データ 106 // (ベクトルデータ+計測データ) 107 sprintf(send_command, "%s", "0DF7"); 108 printf("%s\n", send_command); 109 sprintf(buf, "%s\r\n", send_command); 110 write(fd,buf,strlen(buf)); 111 112 // ACK センサ情報項目設定応答 113 nanosleep(&treq, &trem); 114 res = read(fd,buf,2); 115 read(fd,crlf,2); 116 buf[res]=0; 117 printf(":%s:%d\n", buf, res); 118 printf(":%02x%02x:%d\n", buf[0], buf[1], res); 119 printf(":%02x%02x:%d\n", crlf[0], crlf[1], res); 120 70 121 // 計測条件設定 71 puts("WRITE"); 72 sprintf(buf, "050027950000\r\n"); 122 sprintf(send_command, "%s", "050027950000"); 123 printf("%s\n", send_command); 124 sprintf(buf, "%s\r\n", send_command); 73 125 write(fd,buf,strlen(buf)); 74 126 75 127 // [ACK 計測条件設定応答]を読み取ってみる 76 puts("READ");128 nanosleep(&treq, &trem); 77 129 res = read(fd,buf,2); 78 puts("AFTER READ");79 printf("res=%d\n", res);80 130 buf[res]=0; 81 131 printf(":%s:%d\n", buf, res); 132 printf(":%02x%02x:%d\n", buf[0], buf[1], res); 133 read(fd,crlf,2); 134 printf(":%02x%02x:%d\n", crlf[0], crlf[1], res); 82 135 83 if(0){ // ひとまずコメントアウト 84 while (STOP==FALSE) { /* 終了条件が満たされるまでループ */ 85 puts("."); 86 // res = read(fd,buf,255); 87 res = read(fd,buf,255); 88 buf[--res]=0; /* 文字列の終端をセットする */ 89 printf(":%s:%d", buf, res); 90 if (buf[0]=='z') STOP=TRUE; 136 // 地磁気センサ初期化要求 137 sprintf(send_command, "%s", "27"); 138 printf("%s\n", send_command); 139 sprintf(buf, "%s\r\n", send_command); 140 write(fd,buf,strlen(buf)); 141 142 // ACK 地磁気センサ初期化要求応答 143 nanosleep(&treq, &trem); 144 res = read(fd,buf,2); 145 buf[res]=0; 146 printf(":%s:%d\n", buf, res); 147 printf(":%02x%02x:%d\n", buf[0], buf[1], res); 148 read(fd,crlf,2); 149 printf(":%02x%02x:%d\n", crlf[0], crlf[1], res); 150 151 for(i = 0; i < 10*60; i ++){ 152 153 // 計測開始 154 sprintf(send_command, "%s", "21"); 155 printf("%s\n", send_command); 156 sprintf(buf, "%s\r\n", send_command); 157 write(fd,buf,strlen(buf)); 158 159 // ACK 計測開始応答 160 nanosleep(&treq, &trem); 161 res = read(fd,buf,2); 162 buf[res]=0; 163 printf(":%s:%d\n", buf, res); 164 printf(":%02x%02x:%d\n", buf[0], buf[1], res); 165 read(fd,crlf,2); 166 printf(":%02x%02x:%d\n", crlf[0], crlf[1], res); 167 168 // センサ情報要求 169 sprintf(send_command, "%s", "29"); 170 printf("%s\n", send_command); 171 sprintf(buf, "%s\r\n", send_command); 172 write(fd,buf,strlen(buf)); 173 174 // センサ情報 全センサ全データ(ベクトルデータ+計測データ) 175 nanosleep(&treq, &trem); 176 res = read(fd,buf,52); 177 buf[res]=0; 178 printf(":%s:%d\n", buf, res); 179 printf(":%02x%02x:%d\n", buf[0], buf[1], res); 180 read(fd,crlf,2); 181 printf(":%02x%02x:%d\n", crlf[0], crlf[1], res); 182 183 memcpy(dir_dig_str, &buf[12], 4); 184 dir_dig_str[4] = '\0'; 185 dir_dig = (short)strtol(dir_dig_str,NULL,16); 186 187 printf("dir_dig = %f\n", dir_dig/10.0); 188 91 189 } 92 } 190 93 191 puts("END"); 94 192 tcsetattr(fd, TCSANOW, &oldtio); /* 退避させた設定に戻す */
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)