簡單的串口測試程序
出處:mabaoqiu 發(fā)布于:2009-11-30 13:42:01
1.TTY為編譯好的在嵌入式系統(tǒng)utuLinux下的可執(zhí)行文件(交叉編譯器為arm-linux-gcc).
2.程序?yàn)閅C2440通過串口1(COM1)向計(jì)算機(jī)主機(jī)發(fā)送一個字符串,主機(jī)可用超級終端(windows)或minicom(Linux)接收。
3.minicom下只能顯示90個左右的字符,原因?qū)ふ抑小?/P>
#include <STdio.h> /*標(biāo)準(zhǔn)輸入輸出定義*/
#include <stdlib.h> /*標(biāo)準(zhǔn)函數(shù)庫定義*/
#include <unistd.h> /*Unix標(biāo)準(zhǔn)函數(shù)定義*/
#include <sys/types.h> /**/
#include <sys/stat.h> /**/
#include <fcntl.h> /*文件控制定義*/
#include <termios.h> /*PPSIX終端控制定義*/
#include <errno.h> /*錯誤號定義*/
#define FALSE -1
//設(shè)置串口通信速率
// fd 類型 int 打開串口的文件句柄
// speed 類型 int 串口速度
//
int speed_arr[] = {B115200,B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {115200,38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, };
int set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfseTIspeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
perror("tcsetattr fd1");
return FALSE;
}
tcflush(fd,TCIOFLUSH);
}
}
/**
[email=*@brief]*@brief[/email] 設(shè)置串口數(shù)據(jù)位,停止位和效驗(yàn)位
[email=*@param]*@param[/email] fd 類型 int 打開的串口文件句柄*
[email=*@param]*@param[/email] databits 類型 int 數(shù)據(jù)位 取值 為 7 或者8*
[email=*@param]*@param[/email] stopbits 類型 int 停止位 取值為 1 或者2*
[email=*@param]*@param[/email] parity 類型 int 效驗(yàn)類型 取值為N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits) /*設(shè)置數(shù)據(jù)位數(shù)*/
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return(FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB); /* 設(shè)置為奇效驗(yàn)*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 轉(zhuǎn)換為偶效驗(yàn)*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
/* 設(shè)置停止位*/
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (0);
}
/**
[email=*@breif]*@breif[/email] 打開串口
*/
int OpenDev(char *Dev)
{
int fd = open(Dev, O_RDWR | O_NOCTTY | O_NDELAY); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{ /*設(shè)置數(shù)據(jù)位數(shù)*/
perror("Can't Open Serial Port");
return FALSE;
}
else
return fd;
}
/**
[email=*@breif]*@breif[/email] main()
*/
int main(int argc, char **argv)
{
int fd;
int nwrite;
char *buff="Hello! This is a Serial Test Program,if you can see this,you success!Congratulations!!!!!!!!!";
char *dev ="/dev/s3c2410_serial1";
fd = OpenDev(dev);
if (fd>0)
set_speed(fd,115200);
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
if (set_Parity(fd,8,1,'N')== FALSE)
{
printf("Set Parity Error\n");
exit(1);
}
if((nwrite = write(fd,buff,strlen(buff)))>0)
{
printf("%s\n",buff);
printf("\nLen %d\n",nwrite);
// buff[nread+1]='\0';
//printf("\n%s",buff);
}
close(fd);
}
版權(quán)與免責(zé)聲明
凡本網(wǎng)注明“出處:維庫電子市場網(wǎng)”的所有作品,版權(quán)均屬于維庫電子市場網(wǎng),轉(zhuǎn)載請必須注明維庫電子市場網(wǎng),http://m.58mhw.cn,違反者本網(wǎng)將追究相關(guān)法律責(zé)任。
本網(wǎng)轉(zhuǎn)載并注明自其它出處的作品,目的在于傳遞更多信息,并不代表本網(wǎng)贊同其觀點(diǎn)或證實(shí)其內(nèi)容的真實(shí)性,不承擔(dān)此類作品侵權(quán)行為的直接責(zé)任及連帶責(zé)任。其他媒體、網(wǎng)站或個人從本網(wǎng)轉(zhuǎn)載時(shí),必須保留本網(wǎng)注明的作品出處,并自負(fù)版權(quán)等法律責(zé)任。
如涉及作品內(nèi)容、版權(quán)等問題,請?jiān)谧髌钒l(fā)表之日起一周內(nèi)與本網(wǎng)聯(lián)系,否則視為放棄相關(guān)權(quán)利。
- 什么是氫氧燃料電池,氫氧燃料電池的知識介紹2025/8/29 16:58:56
- SQL核心知識點(diǎn)總結(jié)2025/8/11 16:51:36
- 等電位端子箱是什么_等電位端子箱的作用2025/8/1 11:36:41
- 基于PID控制和重復(fù)控制的復(fù)合控制策略2025/7/29 16:58:24
- 什么是樹莓派?一文快速了解樹莓派基礎(chǔ)知識2025/6/18 16:30:52
- 微帶線阻抗匹配設(shè)計(jì)與實(shí)操調(diào)試技巧
- 從S參數(shù)到實(shí)際元件:微帶線濾波器的設(shè)計(jì)與仿真流程
- IP67/IP68連接器設(shè)計(jì)解析
- 電源管理IC失效的常見原因
- MOSFET體二極管特性分析
- PCB高頻高速信號布線設(shè)計(jì)核心規(guī)范(實(shí)操版)
- 基于FPGA的FIR濾波器硬件實(shí)現(xiàn):架構(gòu)優(yōu)化與資源管理
- 工業(yè)自動化設(shè)備連接器選型指南
- 提高電源管理IC可靠性的設(shè)計(jì)方法
- MOSFET柵極驅(qū)動電路設(shè)計(jì)技巧









