當前位置:首頁 » 操作系統 » 讀串口linux

讀串口linux

發布時間: 2022-06-28 14:18:22

1. linux系統下,如何寫讀串口的程序要注意那些

去 google 下吧

概括來說,打開串口設備,讀串口數據到你緩沖區,寫數據到串口設備

2. linux怎麼用C程讀取XBee接收到的串口數據

太專業鳥

3. 在linux系統下怎麼讀取串口伺服器的實時數據

Linux串口讀寫:
#include <stdio.h> /*標准輸入輸出定義*/
#include <stdlib.h> /*標准函數庫定義*/
#include <unistd.h> /*Unix 標准函數定義*/
#include <sys/types.h>
#include <sys/stat.h>
#include "string.h"
#include <fcntl.h> /*文件控制定義*/
#include <termios.h> /*PPSIX 終端控制定義*/
#include <errno.h> /*錯誤號定義*/

#define FALSE -1
#define TRUE 0

/*********************************************************************/
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR | O_NOCTTY ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}

/**
*@brief 設置串口通信速率
*@param fd 類型 int 打開串口的文件句柄
*@param speed 類型 int 串口速度
*@return void
*/
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400,
19200, 9600, 4800, 2400, 1200, 300, };
void 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;
}
tcflush(fd,TCIOFLUSH);
}
}
}

/**
*@brief 設置串口數據位,停止位和效驗位
*@param fd 類型 int 打開的串口文件句柄
*@param databits 類型 int 數據位 取值 為 7 或者8
*@param stopbits 類型 int 停止位 取值為 1 或者2
*@param parity 類型 int 效驗類型 取值為N,E,O,,S
*/
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
options.c_oflag &= ~OPOST; /*Output*/
if ( tcgetattr( fd,&options) != 0) {
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits) /*設置數據位數*/
{
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); /* 設置為奇效驗*/
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD; /* 轉換為偶效驗*/
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);
}
/* 設置停止位*/
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;
tcflush(fd,TCIFLUSH);
options.c_cc[VTIME] = 150; /* 設置超時15 seconds*/
options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}

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

int fd;
int nread;
char buff[512];
char *dev = "/dev/ttyS0"; //串口二
fd = OpenDev(dev);
set_speed(fd,4800);
if (set_Parity(fd,8,1,'N') == FALSE)
{
printf("Set Parity Error/n");
exit (0);
}
int i;
i = getchar();
if ( i == '1')
{
while (1) //循環讀取數據
{
while((nread = read(fd, buff, 512))>0)
{
printf("/nLen %d/n",nread);
buff[nread+1] = '/0';
printf( "/n%s", buff);
}
}
}
if ( i == '2')
{
while (1) //循環寫入數據
{

gets(buff);

printf("------buff--->%s<--------/n",buff);
int num = strlen(buff);
printf("--------num---->%d<--------------/n",num);
if ( num > 0)
{
printf("Wirte num not NULL./r/n");
nread = write(fd, buff ,num);
if(nread == -1)
{
printf("Wirte sbuf error./n");
}
printf("--nread---->%d<-----------/n",nread);
}

}
}
close(fd);
//exit (0);
}

4. linux串口讀寫的問題

i g i e j e a u w b i g g
承接各類停車場IC卡的制加密與復制
s v r r s s s i g n x c u
類型:門禁卡加密復制,停車卡加密復制,電梯卡加密復制,桑拿房卡加密復制,物業卡加密復制等等
l i b g c h c w b s l j j
提供M1,S50。S70卡解密,復制,限合法:另解密技術轉讓1200元(包括正版解密工具一套免費升級)包教會。QQ:1 8 2 j a 330 now 8147 bg

a c p q q q r j h o t w s

5. Linux中串口read怎樣阻塞的方式讀入數據

Linux中串口read怎樣阻塞的方式讀入數據C/C++ code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

pthread_mutex_lock(&ptty->mt);
ret = read(ptty->fd, pbuf, 1); //由於read並不阻塞 要監聽隨時可能來數據的串口 得不停的循環的查看 效率好低
if(ret < 0)
{
//串口沒有數據進來
}
else
{
//相關處理
}
/*
怎樣加以控制使得,或使用什麼函數
readfunction(ptty->fd, pbuf, ......);//使得這兒沒有串口數據時阻塞,有串口數據了就讀到
//避免不停的循環+sleep() 耗費ARM板資源,而且有時還漏讀
//有的說用什麼fcntl,ioctl 這兩不太懂,希望可以解釋清楚點
*/
pthread_mutex_unlock(&ptty->mt);

6. linux下串口通信,第一次可以讀數據,然後往串口寫數據,再讀數據卻讀不出,求幫助

A<==RS232==>B,A和B通信,通過RS232協議,讀數據的話,是讀對方發來的數據;寫數據的話,是給對方寫數據。所以,要用個while死循環,始終監聽串口是否讀到數據。

7. linux串口讀取問題

首先你確定你那串口是否有東西可讀? 就是你上面說的「一個文件不停的寫數據到串口」!你可以先不這樣讀取,你可以在終端上用cat試試是否有數據可讀:cat /dev/ttyS0

如果有的話,那你就檢查串口設置是否正確,如波特率,數據位,停止位,校驗位等!

最後就是你讀取的函數了,看看先不要用printf列印字元串了,先看看十六進制是否有,然後再看字元等!

就是以上一些,你還可以參考Linux下串口文檔,網路上很多的……

8. linux下串口讀寫問題掛死

這樣寫肯定會寫死的啊..寫(也就是串口發送數據)實際應用不會這么頻繁的.樓主可以在寫後加上一個sleep(1);這樣就不會寫死了..
高並發讀寫這樣設計也是錯誤的應該用隊列來處理..

9. 關於linux下QT中讀寫串口的問題

一、程序設計的基礎,例如:基本的編程語言基礎,至少對數據類型、程序的結構及流程式控制制等最基本的內容要相當清楚!另外有不少同學都問到數據結構的基礎,我一直認為數據結構和演算法的學習是幫助形成程序設計邏輯思維的很好訓練方式,對於程序員的長期專業素養的提高一定有好處,所以建議即使已經在嵌入式行業中工作之後也應該多補充一些相關的知識。許多在學校沒有學過數據結構的同學往往認為這部分非常枯燥、難學。而實際上如果你能明白研究計算機存儲和數據組織方式的意義,就一定能夠充分體會到數據結構的價值和魅力。
二、操作系統工作原理,這部分往往是非計算機專業的同學在學校時沒有接觸過的。而由於嵌入式軟體設計相關的多任務環境、模塊間的同步與通信協同、驅動設計等往往都需要有對操作系統工作機制的了解和掌握作為基礎,因此建議沒有系統學習過的同學,找一本相關的操作系統工作原理書籍認真看一下。
三、基本的硬體基礎,由於嵌入式Linux開發往往是ARM+Linux路線,所以為了能夠在後續培訓過程中很好地掌握主流嵌入式微處理器的結構與原理(例如:ARM9),就需要對硬體工作原理有初步的了解和掌握,建議看一下諸如計算機組成原理、體系結構等相關的專業書籍。
有沒有基礎知識及基礎知識的多少在很大程度上影響著你能否學習嵌入式linux及學習進度,因此,各個培訓機構在學員入學前讀會對其進行基礎知識考查,像其中的東方賽富嵌入式學院對這一塊更加看重,因為他們是保障真正100%就業,對學員入學的要求更加嚴格!

熱點內容
伺服器屏蔽了別人的ip 發布:2024-05-18 19:10:09 瀏覽:618
怎麼獲取ins伺服器地址 發布:2024-05-18 19:10:03 瀏覽:29
仙方一般是什麼配置 發布:2024-05-18 18:53:43 瀏覽:158
黑莓安卓手機主題下載到哪裡 發布:2024-05-18 18:47:18 瀏覽:56
湯靖軒編程 發布:2024-05-18 18:46:04 瀏覽:532
腳本故事梗 發布:2024-05-18 18:29:02 瀏覽:822
安卓和csharp哪個發展好 發布:2024-05-18 18:09:30 瀏覽:527
換編程題庫 發布:2024-05-18 18:00:58 瀏覽:562
如何使用伺服器ip直連網站 發布:2024-05-18 18:00:49 瀏覽:432
三星n7100哪個安卓版本好用 發布:2024-05-18 17:55:41 瀏覽:490