新聞中心
當(dāng)前,Linux操作系統(tǒng)已經(jīng)成為在開發(fā)嵌入式系統(tǒng)、服務(wù)器以及超級計算機(jī)等領(lǐng)域中更流行的操作系統(tǒng)之一。作為一款開源系統(tǒng),Linux具有強(qiáng)大的開放性和可定制性,同時也支持許多不同的編程語言。因此,在Linux中使用串口通信成為許多開發(fā)人員競相使用的手段之一。下文將介紹如何在Linux系統(tǒng)中打開串口,以及如何基于C語言實現(xiàn)串口程序。

創(chuàng)新互聯(lián)建站專注于滄州企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,成都做商城網(wǎng)站。滄州網(wǎng)站建設(shè)公司,為滄州等地區(qū)提供建站服務(wù)。全流程按需開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)建站專業(yè)和態(tài)度為您提供的服務(wù)
1.打開串口
在Linux系統(tǒng)中,對于串口設(shè)備的操作一般采用文件操作的方法,因此需要使用文件操作相關(guān)的函數(shù)打開串口設(shè)備。下面是一個基本的串口打開程序的實現(xiàn),其中串口編號為/dev/ttyS0:
“`c
#include
#include
#include
int fd;
void openPort(char *portDevice)
{
fd=open(portDevice,O_RDWR|O_NOCTTY|O_NDELAY);
if(fd==-1)
{
printf(“openPort(): error when opening the device %s\n”,portDevice);
}
else
{
fcntl(fd,F_SETFL,0/*FASYNC*/);
printf(“openPort(): successfully opened the device %s\n”,portDevice);
}
}
int mn()
{
openPort(“/dev/ttyS0”);
return 0;
}
“`
在此程序中,使用了open(打開串口設(shè)備)函數(shù)打開串口設(shè)備文件,傳入設(shè)備路徑作為之一個參數(shù)。第二個參數(shù)使用了標(biāo)準(zhǔn)的串口設(shè)置,即O_RDWR|O_NOCTTY|O_NDELAY,可確保對設(shè)備的打開同時可以讀寫,不承認(rèn)是否為端口控制終端設(shè)備,以及不阻塞讀寫。當(dāng)函數(shù)返回為-1時,即表示未能成功打開該設(shè)備,否則則打印串口打開成功。
2.配置串口
串口打開后,需要進(jìn)行相關(guān)的設(shè)置以配置串口。通過在Linux系統(tǒng)中設(shè)置來設(shè)置和控制串口的標(biāo)準(zhǔn)和速率等參數(shù),下面的函數(shù)將串口配置為9600波特率,不使用停止位,不設(shè)置奇偶校驗,不使用等待、分割和其他特殊字符。
“`c
#define BAUDRATE B9600
#define MODEMDEVICE “/dev/ttyS0”
#define _POSIX_SOURCE 1 /* POSIX compliant source */
static int set_interface_attribs (int fd, int speed, int parity)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf(“set_interface_attribs(): error from tcgetattr\n”);
return -1;
}
cfsetospeed (&tty, speed);
cfsetispeed (&tty, speed);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit characters
// disable IGNBRK for miatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn’t block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf(“set_interface_attribs(): error from tcsetattr\n”);
return -1;
}
return 0;
}
static void set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
printf(“set_blocking(): error from tcgetattr\n”);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
{
printf(“set_blocking(): error setting term attributes\n”);
}
}
“`
在此函數(shù)中,使用tcsetattr函數(shù)設(shè)置套接口參數(shù)。其中,函數(shù)的之一個參數(shù)為串口設(shè)備文件的文件描述符,以及需要設(shè)置的波特率、奇偶校驗等參數(shù)。在本例中,將波特率設(shè)置為9600,停止位數(shù)為0,奇偶校驗不配置。另外,方法中通過CLOCAL和CREAD標(biāo)志控制本端口是否為本地串口,以及是否允許讀操作。同時,將VMIN屬性設(shè)置為0表示直接讀取控制臺中的內(nèi)容,而不必等待它的輸入,以達(dá)到同步操作的效果。
3.發(fā)送接收數(shù)據(jù)
在打開并設(shè)置好串口之后,需要使用C語言實現(xiàn)數(shù)據(jù)的讀寫操作。一般使用read(讀取數(shù)據(jù))和write(寫入數(shù)據(jù))函數(shù)來實現(xiàn)此目的。
“`c
static void send(int fd,char* text)
{
int len = strlen(text);
write(fd,text,len);
}
static void receive(int fd)
{
char buf[10];
read(fd,buf,sizeof(buf));
}
“`
在本例中,使用write函數(shù)將一個簡短的文本字符串發(fā)送到串口,然后使用read函數(shù)接收數(shù)據(jù)并存儲到緩沖區(qū)中,其中調(diào)用了initPort()函數(shù)。同時,讀數(shù)據(jù)時可以設(shè)置超時時間以避免阻塞程序。
除此之外,還可以使用poll函數(shù)實現(xiàn)基于Linux系統(tǒng)的串口通信程序。poll函數(shù)可以同時監(jiān)聽多個文件描述符上的事件,或者等待指定時間。由于C庫提供的函數(shù)比較原始,因此使用poll函數(shù)能夠更好的控制程序的時間操作和性能。
“`c
struct pollfd fds[1];
fds[0].fd = fd;
fds[0].events = POLLIN;
while (1)
{
int ret = poll(fds, 1, 1000); // wt for up to 1000ms
if (ret
{
printf(“poll(): error during poll()\n”);
exit(-1);
}
else if (ret == 0)
{
printf(“poll(): timeout occurred, no data received\n”);
}
else
{
if (fds[0].revents & POLLIN)
{
char buf[256];
int n = read(fd, buf, sizeof buf);
if (n
{
printf(“read(): error reading input string”);
}
printf(“%.*s”,n,buf);
}
}
}
“`
相關(guān)問題拓展閱讀:
- 關(guān)于怎樣在linux上用C寫串口收發(fā)數(shù)據(jù)程序
關(guān)于怎樣在linux上用C寫串口收發(fā)數(shù)據(jù)程序
對于睜寬編程來說,沒悉弊亮什么區(qū)別,通過控制485的使能端該程序完全可以使用。唯一的區(qū)別就是你在發(fā)送的時候通過程序把485的控制腳拉高,接收的時候把他拉低就可以了。至于電氣卜虛方面的區(qū)別:RS232是全雙工,可以同時收發(fā),RS485是半雙工,不能同時收發(fā),還有電平信號不一樣,這個編程你就不要理了。
關(guān)于linux c 打開串口的介紹到此就結(jié)束了,不知道你從中找到你需要的信息了嗎 ?如果你還想了解更多這方面的信息,記得收藏關(guān)注本站。
香港服務(wù)器選創(chuàng)新互聯(lián),2H2G首月10元開通。
創(chuàng)新互聯(lián)(www.cdcxhl.com)互聯(lián)網(wǎng)服務(wù)提供商,擁有超過10年的服務(wù)器租用、服務(wù)器托管、云服務(wù)器、虛擬主機(jī)、網(wǎng)站系統(tǒng)開發(fā)經(jīng)驗。專業(yè)提供云主機(jī)、虛擬主機(jī)、域名注冊、VPS主機(jī)、云服務(wù)器、香港云服務(wù)器、免備案服務(wù)器等。
分享標(biāo)題:在Linux系統(tǒng)中如何打開串口?C語言實現(xiàn)(linuxc打開串口)
文章分享:http://www.dlmjj.cn/article/cdepjdo.html


咨詢
建站咨詢
