【Rayeager PX2分享】PX2上ttys3串口測試程序編寫

在PX2開發板上有個已引出的的ttys3,你們若是用PX2來開發或者學習,時不時老是會須要用到這個串口的,而在android系統中,使用串口的方式也很簡單,由於在PX2的源碼中已經有了ttys3的驅動,咱們只須要將起編譯進內核(編譯fangshihttp://bbs.chipspark.com/forum.p ... =%E4%B8%B2%E5%8F%A3)就能夠像操做文件同樣,操做這個串口,而後操做這個串口的時候,咱們須要作的也只有先作個初始的配置,就是設置波特率,中止位,數據位,奇偶校驗。php

注:ttys3的鏈接若是是DB9腳的,理論上只鏈接RX ,TX,GND即可工做。樓主即是用這個的linux

1.串口的配置,android

串口的配置是利用POSIX終端的termios結構ios

termios 結構定義以下 編程

struct termios 函數

學習

tcflag_t c_iflag /* 輸入選項標誌 */ 測試

tcflag_t c_oflag /* 輸出選項標誌 */ spa

tcflag_t c_cflag /* 控制選項標誌 */ 調試

tcflag_t c_lflag /* 本地選項標誌 */ 

cc_t c_cc[NCCS] /* 控制特性 */ 

而部分參數的做用參考附件中的內容  Linux下串口編程-很是全面-精品.txt.zip

核心主要配置波特率,校驗位,數據位,中止位,

而樓主本身寫的驅動以下,其中的設置爲波特率115200,無效驗,八位數據位,一位中止位,


#include <stdio.h> /*標準輸入輸出定義*/ 

#include <stdlib.h> /*標準函數庫定義*/ 

#include <unistd.h> /*Unix 標準函數定義*/ 

#include <sys/types.h> 

#include <sys/stat.h> 

#include <fcntl.h> /*文件控制定義*/ 

#include <termios.h> /*PPSIX 終端控制定義*/ 

#include <errno.h> /*錯誤號定義*/ 

#include <linux/kernel.h>


    int OpenDev(char *Dev){


    int fd = open( Dev, O_RDWR )  

    if (-1 == fd){ 

        perror("Can't Open Serial Port") 

        return -1 

    else 

        return fd 

    int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop){

    struct termios newios,oldios

    if(tcgetattr(fd, &oldios)!=0){    //獲取以前定義的終端值    

        perror("setupserial 1")

        return -1

}

    bzero(&newios,sizeof(newios))//將newios結構體裏的數據從新設置爲0

    newios.c_cflag|=CREAD//使能讀和

    newios.c_cflag&=~CSIZE//字符長度掩碼

    switch(nBits){

        case 7:newios.c_cflag|=CS7break

        case 8:newios.c_cflag|=CS8break



}

    switch(nEvent){

        case '0':

            newios.c_cflag|=PARENB

            newios.c_cflag|=PARODD

            newios.c_cflag|=(INPCK|ISTRIP)//偶校驗

            break

        case '1':

            newios.c_cflag|=PARENB

            newios.c_cflag&=~PARODD//奇校驗

            break

        case 'N':

            newios.c_cflag&=~PARENB//無校驗

            break


}

    switch(nSpeed){//設置波特率

        case 2400:

            cfsetispeed(&newios,B2400)

            cfsetospeed(&newios,B2400)

            break

        case 4800:

            cfsetispeed(&newios,B4800)

            cfsetospeed(&newios,B4800)

            break

        case 9600:

            cfsetispeed(&newios,B9600)

            cfsetospeed(&newios,B9600)

            break

        case 115200:

            cfsetispeed(&newios,B115200)

            cfsetospeed(&newios,B115200)

            break

        default:

            cfsetispeed(&newios,B115200)

            cfsetospeed(&newios,B115200)

            break

    }

    if(nStop==1)

        newios.c_cflag&=~CSTOPB//一中止位

    else if (nStop==2)

    {

        newios.c_cflag|=CSTOPB//兩中止位

        newios.c_cc[VTIME]=0//無延時

        newios.c_cc[VMIN]=0//無附加

        tcflush(fd,TCIOFLUSH)//刷新輸出隊列

}        


    newios.c_lflag &= ~(ICANON | IEXTEN | ISIG | ECHO)//原始數據輸入

    newios.c_oflag &= ~OPOST//原始數據輸出

    if(tcsetattr(fd,TCSANOW,&newios)!=0)//將配置賦予POSIX終端

        {

            perror("com set error")

            return -1


            }


        printf("set done\n")

return 0


}

int main(int argc, char **argv){ 

int fd 

int nread,nwrite 

char buff[8] 

char *dev = "/dev/ttyS3" //串口3

char bufsend[8]

fd = OpenDev(dev) 


if (set_opt(fd,115200,8,'N',1) == FALSE) { 

printf("Set Parity Errorn") 

exit (0) 

memset(buff,0,8*sizeof(char))

if(strcmp(argv[1],"receive")==0)

{

while(1){

while((nread = read(fd, buff, 8))>0) //讀數據,在死循環中不斷等待,輸出數據,

printf("receive %d ",nread)       

printf( "\n%s", buff) 

}}}

memset(bufsend, 0,8*sizeof(char))//清空數據棧

if(strcmp(argv[1],"send")==0){//發送數據,類型字符

printf("send message:  ")

fgets(bufsend,8,stdin)

printf("\n%s",bufsend)


nwrite=write(fd,bufsend,8)


}


close(fd) 

exit (0) 


2.程序測試,

確認鏈接無誤後,樓主開始發送數據,可是,雖然正常實現串口的通訊,可是發送的數據倒是亂碼,一開始樓主發送了111111111111,而接受到的數據是ggggggggggg,其接收到的即不是ascii碼,而對應的十六進制是67,具體的問題樓主還在研究,估計還得過段時間,才能解決這個問題,按樓主的估計可能性有兩個,一個是樓主是使用DB9接口的,只連了三根線,並無VCC,會不會是這個有影響,二,樓主配置串口時,只作了基本配置,或許是某個配置出錯了。這裏先放到論壇上分享給你們,後續再繼續補充,與你們共勉。

串口調試助手 
 sscom32.rar

相關文章
相關標籤/搜索