Dannier 寫:謝謝!
已經解決了因為我的RS232接腳接錯了...真不好意思
可是問題又來了
我把程式寫成cocoa後發現
我不能用while loop 因為這樣彩球會狂轉 久了就變成沒有回應了
請問是用 startPeriodicEvents 嗎? 可是我不知道要怎麼利用
我對cocoa的event還不是很了解
還是有別的方法可以讓cocoa 程式可以一直做某件事而不會hang住
抱歉因為我對 cocoa 的知識也很一知半解
感謝高手的指點!!
我參考了Dannier 大大的 code , 再加上 NSTimer 目前有連到 serial port 上,
但是讀到的資料卻是錯的,可以請各位幫我看看問題在哪嗎??
連線條件 : 9600,7,N,2,Non-flow control
int fd, res;
struct termios oldtio, newtio;
char buf[256];
printf("Start Open Serial ...\n");
fd = open(RS232Path, O_RDWR|O_NOCTTY|O_NONBLOCK);
if (fd < 0)
printf("Serial Open Fail...\n");
perror(RS232Path);
serialopen = FALSE;
exit(1);
}
else {
printf("Serial Open OK ...\n");
tcgetattr(fd, &oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BaudRate|CS7|CLOCAL|CREAD|HUPCL|CSTOPB;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
serialopen = TRUE;
printf("Reading...\n");
}
- (void) handleTimer: (NSTimer *) timer
{
int xx;
printf("Timer Run.....\n");
if (serialopen) {
res = read(fd, buf, 256);
buf[res]=0;
printf("res=%d buf=%s\n", res, buf);
for (xx=0; xx<res; xx++)
printf("res=%d,buf=%x\n", xx, buf[xx]);
if (buf[0] == 0) printf("Empty! ");
for (xx=0; xx<256; xx++)
buf[xx]=0;
}
}
*********** Debug view *************
Timer Run.....
res=48 buf=б\240\263\261\261\273\263\266\263\273\263\262\256\262\215б\240\263\261\261\273\263\266\263\273\263\262\256\262\215б\240\263\261\261\273\263\266\262\273\263\262\256\262\215
res=0,buf=ffffffd0
res=1,buf=ffffffb1
res=2,buf=ffffffa0
res=3,buf=ffffffb3
res=4,buf=ffffffb1
res=5,buf=ffffffb1
res=6,buf=ffffffbb
res=7,buf=ffffffb3
res=8,buf=ffffffb6
.......
***********************************
以 ZTerm 獲得的正確的資料是 P1 311;363;32.1 這種格式~
