




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
嵌入式Linux下串口編程一、配置內(nèi)核在嵌入式Linux下進(jìn)行串口編程之前,先在內(nèi)核中配置串口部分,如下:DeviceDrivers---> characterdevices---> Serialdrivers---> <*>SamssungS3C2440/S3C2442Serialportsupport二、應(yīng)用程序C文件:uart_init.c:打開設(shè)備、初始化串口(設(shè)置參數(shù))main.c:測試串口讀寫頭文件:uart_init.hMakfileuart_init.c#include<stdio.h>#include<stdlib.h>#include<string.h>#include<error.h>#include<unistd.h>#include<fcntl.h>#include<sys/types.h>#include<sys/stat.h>#include<termios.h>intopen_termios(char*dev){ intfd=-1; if(-1==(fd=open(dev,O_RDWR|O_NOCTTY|O_NONBLOCK))) { printf("opentermioserror!\n"); } returnfd;}intset_termios(intfd,longspeed,chardatabit,charstopbit,charoebit){ structtermiosnewtio; interr=-1; //清零結(jié)構(gòu)體 bzero(&newtio,sizeof(newtio)); //設(shè)置接收使能 newtio.c_cflag|=CLOCAL|CREAD; //設(shè)置數(shù)據(jù)位 switch(databit) { case7: newtio.c_cflag|=CS7; break; case8: newtio.c_cflag|=CS8; break; default: newtio.c_cflag|=CS8; break; } //設(shè)置停止位 switch(stopbit) { case1: newtio.c_cflag&=~CSTOPB; break; case2: newtio.c_cflag|=CSTOPB; break; default: newtio.c_cflag&=~CSTOPB; break; } //設(shè)置校驗位 switch(oebit) { case'O'://奇校驗 newtio.c_cflag|=PARENB; newtio.c_cflag|=(INPCK|ISTRIP); newtio.c_cflag|=PARODD; break; case'E'://偶數(shù) newtio.c_cflag|=PARENB; newtio.c_cflag|=(INPCK|ISTRIP); newtio.c_cflag&=~PARODD; break; case'N'://不校驗 newtio.c_cflag&=~PARODD; break; default: //不校驗 newtio.c_cflag&=~PARODD; break; } //設(shè)置波特率 switch(speed) { case2400: cfsetispeed(&newtio,B2400); cfsetospeed(&newtio,B2400); break; case4800: cfsetispeed(&newtio,B4800); cfsetospeed(&newtio,B4800); break; case9600: cfsetispeed(&newtio,B9600); cfsetospeed(&newtio,B9600); break; case57600: cfsetispeed(&newtio,B57600); cfsetospeed(&newtio,B57600); break; case115200: cfsetispeed(&newtio,B115200); cfsetospeed(&newtio,B115200); break; default: cfsetispeed(&newtio,B9600); cfsetospeed(&newtio,B9600); break; } //設(shè)置等待時間和最小接收字符數(shù) newtio.c_cc[VTIME]=0; newtio.c_cc[VMIN]=0; //處理未接收字符 tcflush(fd,TCIFLUSH); //激活配置 if((tcsetattr(fd,TCSANOW,&newtio))!=0) perror("comseterror!"); else err=0; perror("comsetdone!"); returnerr;}uart_init.h#ifndef__UART_INIT__#define__UART_INIT__externintopen_termios(char*dev);intset_termios(intfd,longspeed,chardatabit,charstopbit,charoebit);#endifmain.c#include<stdio.h>#include<stdlib.h>#include<error.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<termios.h>#include"uart_init.h"char*uart0_dev="/dev/ttySAC0";intmain(void){ intuart0_fd; interr=0; charuart0_byte=0; char*uart0_buff[8]={0}; uart0_fd=open_termios(uart0_dev); if(uart0_fd<0) { exit(1); } do { err=set_termios(uart0_fd,115200,8,1,'N'); } while(-1==err); while(1) { read(uart0_fd,&uart0_byte,1); if(0xfd==uart0_byte) { read(uart0_fd,uart0_buff,8); write(uart0_fd,uart0_buff,8); memset(uart0_buff,0,sizeof(uart0_buff)); uart0_byte=0; } } exit(0);}Makefile#YoucanaddsourcecodefilelikethefollowingSOURCE+=main.cSOURCE+=uart_init.c##YoucanaddheadercodefilelikethefollowingHEADER+=uart_init.h#YoucannametheexecutablefilelikethefollowingTARGET=UART_ARM#YoucanaddincludepathlikethefollowingINCLUDEPATH=#YoucanlibrarypathlikethefollowingLIBPATH=#YoucanaddlibrarylikethefollowingLIBS=-lpthread#YoucantaddcompileoptionslikethefollowingOPTION+=-WallOPTION+=-g#YoucanspecifythecompilerCC=arm-linux-gcc#Don'tmodifythefollowingcodeunlessyouknowhowtoexactlyOBJECTS=$(SOURCE:%.c=%.o)$(TARGET):$(OBJECTS) $(CC)$(OPTIO
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 墊資合同協(xié)議書范本
- 連鎖藥店戰(zhàn)略合同協(xié)議書
- 買房借款合同協(xié)議書范本
- 以項目促融合,扎實推進(jìn)融媒體建設(shè)
- 裝卸磚工合同協(xié)議書
- 煤炭承包生產(chǎn)合同協(xié)議書
- 2025年中國雷帕霉素項目創(chuàng)業(yè)計劃書
- 杯狀病毒治療方案-貓杯狀病毒最佳治療方案
- 2025秋五年級語文上冊統(tǒng)編版-【語文園地七】交互課件
- 河道清淤合同協(xié)議書范文
- 《一榀框架的結(jié)構(gòu)計算和設(shè)計21000字(論文)》
- DBJ04-T 259-2024 人工砂生產(chǎn)應(yīng)用技術(shù)規(guī)程
- 萬科金域華府-前期物業(yè)服務(wù)合同
- 儲能科學(xué)與工程單選題100道及答案解析
- 中國急性缺血性卒中診治指南(2023)解讀
- 2025屆四川省成都市彭州市重點達(dá)標(biāo)名校中考沖刺卷生物試題含解析
- 砌體及構(gòu)造柱圈梁等二次結(jié)構(gòu)施工方案
- 四年級 下冊 人教版 數(shù)學(xué) 第八單元《平均數(shù)(二)(例2)》 課件
- 《公路隧道監(jiān)控量測技術(shù)規(guī)程》
- 1.5.2填土的壓實方法與影響填土壓實質(zhì)量的因素-尚德勵志精技強55課件講解
- 中建外墻保溫工程施工方案
評論
0/150
提交評論