導航:首頁 > 操作系統 > linux多線程串口

linux多線程串口

發布時間:2022-10-17 15:11:55

1. linux下的串口編程

這有個友善的串口常式,參考下吧,用gcc編譯可以在linux下用
# include <stdio.h>
# include <stdlib.h>
# include <termio.h>
# include <unistd.h>
# include <fcntl.h>
# include <getopt.h>
# include <time.h>
# include <errno.h>
# include <string.h>

int CommFd, TtyFd;

static void Error(const char *Msg)
{
fprintf (stderr, "%s\n", Msg);
fprintf (stderr, "strerror() is %s\n", strerror(errno));
exit(1);
}
static void Warning(const char *Msg)
{
fprintf (stderr, "Warning: %s\n", Msg);
}

static int SerialSpeed(const char *SpeedString)
{
int SpeedNumber = atoi(SpeedString);
# define TestSpeed(Speed) if (SpeedNumber == Speed) return B##Speed
TestSpeed(1200);
TestSpeed(2400);
TestSpeed(4800);
TestSpeed(9600);
TestSpeed(19200);
TestSpeed(38400);
TestSpeed(57600);
TestSpeed(115200);
TestSpeed(230400);
Error("Bad speed");
return -1;
}

static void PrintUsage(void)
{

fprintf(stderr, "comtest - interactive program of comm port\n");
fprintf(stderr, "press [ESC] 3 times to quit\n\n");

fprintf(stderr, "Usage: comtest [-d device] [-t tty] [-s speed] [-7] [-c] [-x] [-o] [-h]\n");
fprintf(stderr, " -7 7 bit\n");
fprintf(stderr, " -x hex mode\n");
fprintf(stderr, " -o output to stdout too\n");
fprintf(stderr, " -c stdout output use color\n");
fprintf(stderr, " -h print this help\n");
exit(-1);
}

static inline void WaitFdWriteable(int Fd)
{
fd_set WriteSetFD;
FD_ZERO(&WriteSetFD);
FD_SET(Fd, &WriteSetFD);
if (select(Fd + 1, NULL, &WriteSetFD, NULL, NULL) < 0) {
Error(strerror(errno));
}

}

int sendUart(char c)
{
WaitFdWriteable(CommFd);
return write(CommFd, &c, 1);
}

char recUart()
{
char c='\0';
if (FD_ISSET(CommFd, &ReadSetFD))
{
if(read(CommFd, &c, 1) == 1) return c;
else printf("No data to receive.\n");
}
}

int main(int argc, char **argv)
{
struct termios TtyAttr;
struct termios BackupTtyAttr;

int DeviceSpeed = B115200;
int TtySpeed = B115200;
int ByteBits = CS8;
const char *DeviceName = "/dev/ttyS0";
const char *TtyName = "/dev/tty";
int OutputHex = 0;
int OutputToStdout = 0;
int UseColor = 0;

printf("Now we start.\n");
opterr = 0;
for (;;) {
int c = getopt(argc, argv, "d:s:t:7xoch");
if (c == -1)
break;
switch(c) {
case 'd':
DeviceName = optarg;
break;
case 't':
TtyName = optarg;
break;
case 's':
if (optarg[0] == 'd') {
DeviceSpeed = SerialSpeed(optarg + 1);
} else if (optarg[0] == 't') {
TtySpeed = SerialSpeed(optarg + 1);
} else
TtySpeed = DeviceSpeed = SerialSpeed(optarg);
break;
case 'o':
OutputToStdout = 1;
break;
case '7':
ByteBits = CS7;
break;
case 'x':
OutputHex = 1;
break;
case 'c':
UseColor = 1;
break;
case '?':
case 'h':
default:
PrintUsage();
}
}
if (optind != argc)
PrintUsage();

CommFd = open(DeviceName, O_RDWR, 0);
if (CommFd < 0)
Error("Unable to open device");
if (fcntl(CommFd, F_SETFL, O_NONBLOCK) < 0)
Error("Unable set to NONBLOCK mode");

memset(&TtyAttr, 0, sizeof(struct termios));
TtyAttr.c_iflag = IGNPAR;
TtyAttr.c_cflag = DeviceSpeed | HUPCL | ByteBits | CREAD | CLOCAL;
TtyAttr.c_cc[VMIN] = 1;

if (tcsetattr(CommFd, TCSANOW, &TtyAttr) < 0)
Warning("Unable to set comm port");

TtyFd = open(TtyName, O_RDWR | O_NDELAY, 0);
if (TtyFd < 0)
Error("Unable to open tty");

TtyAttr.c_cflag = TtySpeed | HUPCL | ByteBits | CREAD | CLOCAL;
if (tcgetattr(TtyFd, &BackupTtyAttr) < 0)
Error("Unable to get tty");

if (tcsetattr(TtyFd, TCSANOW, &TtyAttr) < 0)
Error("Unable to set tty");

for (;;) {
unsigned char Char = 0;
fd_set ReadSetFD;

void OutputStdChar(FILE *File) {
char Buffer[10];
int Len = sprintf(Buffer, OutputHex ? "%.2X " : "%c", Char);
fwrite(Buffer, 1, Len, File);
}

FD_ZERO(&ReadSetFD);

FD_SET(CommFd, &ReadSetFD);
FD_SET( TtyFd, &ReadSetFD);
# define max(x,y) ( ((x) >= (y)) ? (x) : (y) )
if (select(max(CommFd, TtyFd) + 1, &ReadSetFD, NULL, NULL, NULL) < 0) {
Error(strerror(errno));
}
# undef max

if (FD_ISSET(CommFd, &ReadSetFD)) {
while (read(CommFd, &Char, 1) == 1) {

WaitFdWriteable(TtyFd);
if (write(TtyFd, &Char, 1) < 0) {
Error(strerror(errno));
}
if (OutputToStdout) {
if (UseColor)
fwrite("\x1b[01;34m", 1, 8, stdout);
OutputStdChar(stdout);
if (UseColor)
fwrite("\x1b[00m", 1, 8, stdout);
fflush(stdout);
}
}
}

if (FD_ISSET(TtyFd, &ReadSetFD)) {
while (read(TtyFd, &Char, 1) == 1) {
static int EscKeyCount = 0;
WaitFdWriteable(CommFd);
if (write(CommFd, &Char, 1) < 0) {
Error(strerror(errno));
}
if (OutputToStdout) {
if (UseColor)
fwrite("\x1b[01;31m", 1, 8, stderr);
OutputStdChar(stderr);
if (UseColor)
fwrite("\x1b[00m", 1, 8, stderr);
fflush(stderr);
}

if (Char == '\x1b') {
EscKeyCount ++;
if (EscKeyCount >= 3)
goto ExitLabel;
} else
EscKeyCount = 0;
}
}

}

ExitLabel:
if (tcsetattr(TtyFd, TCSANOW, &BackupTtyAttr) < 0)
Error("Unable to set tty");

return 0;
}

2. linux寫串口

不給我分嗎?還是回答的不夠清楚

3. 請教linux下串口編程的幾個問題,高手相助

連串口,中斷設定一般控制這幾個參數就可以了:
1。波特率 -》一般串口設置成115200 ,你的終端可以輸出訊息,這個參數應該沒問題
2。奇偶校驗 -》一般關閉
3。流控 -》 一般關閉
4。停止位 -》一般設置為無
5。有時候要設置數據位 -》8位
我一直是這么用的,目前還沒出過什麼問題 。

4. 求教linux中串口發送at命令的問題

要滿足這三個條件:
1,單開線程自動寫at
2,單開線程讀at
3,打開文件讀配置,將讀出來後的配置文件放到log文件里去。
這邊記錄下如何在ui下開一個線程的過程:用一個handler變數調用handler.post函數,然後在runnable裡面重寫run介面就可以,實際上,這樣調用出來的線程跟ui主線程是一個線程,不會創建新的。所以這邊如果要用線程創建的話,必須
wthread = new HandlerThread("thread");

wthread.start();

wHandler = new Handler(wthread.getLooper());

wHandler.post(runnable);
這時重寫runnable的run函數才可以實現重新開啟一個線程。
在這個線程裡面可以讀寫串口,但是界面的刷新不能在這個子線程裡面做。必須主線程在做一個handler,然後子線程調用Message 變數傳進主線程的handler.sendMessage(msg);然後主線程用handlerMessage接收子線程傳過來的消息,在主線程裡面刷新界面。

5. linux c 串口 收發數據

1、接受數據一般是阻塞,就是沒有接收到數據就一直等待,可以設置為不阻塞,這樣就可以了

2、另一種方法是,創建線程,一收、一發,就可以互不影響

6. linux下,如何查看工控機的串口被哪個線程佔用,能否使該線程強制釋放串口

看看這兩個
ps axl |grep ttyS (串口)
ps axl |grep ttyUSB (usb串口)
[root@localhost ~]# ps axl |grep ttyS
0 0 1558 1534 20 0 116264 3044 poll_s S+ tty1 0:00 minicom -b 9600 -D ttyS0
0 0 1655 1637 20 0 112704 972 pipe_w S+ pts/0 0:00 grep --color=auto ttyS
-----------------------------------------
minicom ttyS0 1558 (第三列為進程號)

7. Linux,通過串口實現線程對數據實現收發,為什麼只能寫線程,而讀線程運行不了

另一個線程完全可以運行,是否運行決定權在你。 如果另一個線程需要等待串口的數據,那麼它應該調用wait來等待信號量 讀取串口數據的線程應該在讀取完成後通知等待在信號量上的線程,以繼續運行。

8. linux下多串口的問題

你open倆串口就OK了
至於轉發數據的話 你read一個串口的數據在buf里 判斷是否轉發的 是的話就write buf到另一個串口裡 很簡單

int com0_fd = open(...) ;
int com1_fd = open(...) ;
tcgetattr() tcsetattr()....
.....
char buf[1024] ;
int length = read(com0_fd , buf , 1024) ;
判斷是否轉發的
是的話
write(com1_fd , buf, length);
當然 實際寫的時候不是這么簡單的流程 你可能給倆串口開倆線程 用鎖來控制同步 通過全局數據結構體傳遞數據 串口的讀函數可能會用到select等。

閱讀全文

與linux多線程串口相關的資料

熱點內容
php取域名中間 瀏覽:896
cad命令欄太小 瀏覽:830
php開發環境搭建eclipse 瀏覽:480
qt文件夾名稱大全 瀏覽:212
金山雲伺服器架構 瀏覽:230
安卓系統筆記本怎麼切換系統 瀏覽:618
u盤加密快2個小時還沒有搞完 瀏覽:93
小米有品商家版app叫什麼 瀏覽:94
行命令調用 瀏覽:434
菜鳥裹裹員用什麼app 瀏覽:273
窮查理寶典pdf下載 瀏覽:514
csgo您已被禁用此伺服器怎麼辦 瀏覽:398
打開加密軟體的方法 瀏覽:156
雲存儲伺服器可靠嗎 瀏覽:967
2核1g的雲伺服器能帶動游戲嘛 瀏覽:898
逆命20解壓碼 瀏覽:146
徐州辦犬證需要下載什麼app 瀏覽:1002
百保盾是什麼樣的app 瀏覽:699
文件和文件夾的命名規格 瀏覽:798
java命令行運行java 瀏覽:664