2010年6月2日 星期三

簡易連線uart的程式

本來是想要把minicom搬到android上,不過他的source code很多,也蠻複雜的,
後來在網路上找到tick_minicom,只有幾個檔案,應該很容易讓他編進android

tick_minicom:簡單的uart程式
http://linuxocarina.blogspot.com/2007/09/minicom.html

不過我的連線設定跟他不一樣,所以參考以下網站對程式做了一些修改之後就可以使用了
http://book.51cto.com/art/200711/59758.htm



int
set_baud (int fd, int rate)
{
int i, rt;

u_int32_t bd = 0;

struct termios ti;

//select baudrate
for (i = 0; i < ARRAY_SIZE (bdrts); i++) {
if (bdrts[i].bps == rate) {
bd = bdrts[i].b;
debug("select baudrate: %d\n",rate);
break;
}
}
if (bd == 0) {
error ("Cannot set Baud Rate!!\n");
return -1;
}

//set baudrate
rt = tcgetattr (fd, &ti);
if (rt < 0) {
error ("Cannot get attr i=%d\n", rt);
return rt;
}

debug ("Open uart input with speed %d\n", bdrts[i].bps);
//rt = cfsetispeed (&ti, B0);
rt = cfsetispeed (&ti, bd);
if (rt < 0) {
//error ("Cannot set input baud to B0!!\n");
error ("Cannot set input baud to bd!!\n");
return rt;
}

debug ("Open uart output with speed %d\n", bdrts[i].bps);
rt = cfsetospeed (&ti, bd);
if (rt < 0) {
error ("Cannot set baud rate %d\n", bdrts[i].bps);
return rt;
}

rt=tcsetattr (fd, 0, &ti);
if(rt<0){
return rt;
}

//set no hareware conrtol
rt = tcgetattr (fd, &ti);
//ti.c_cflag |= CRTSCTS; // hardware control
ti.c_cflag &= ~CRTSCTS; // no hardware control
rt=tcsetattr (fd, 0, &ti);
if(rt<0){
return rt;
}


//set databit:8
rt = tcgetattr (fd, &ti);
ti.c_cflag&=~CSIZE;
ti.c_cflag |=CS8;
rt=tcsetattr (fd, 0, &ti);
if(rt<0){
return rt;
}


//set no parity bit
rt = tcgetattr (fd, &ti);
ti.c_cflag &= ~PARENB;
rt=tcsetattr (fd, 0, &ti);
if(rt<0){
return rt;
}


return rt;
}

沒有留言: