导航:首页 > 操作系统 > 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微信第三方登录demo 浏览:534
上海php工具开发源码交付 浏览:788
哪里有求购黄页的源码 浏览:194
商城矿机源码矿场系统 浏览:195
单片机的led灯熄灭程序 浏览:222
洛阳python培训 浏览:702
小键盘命令 浏览:192
单片机c语言返回主程序 浏览:816
dockerpythonweb 浏览:970
程序员算法有多强 浏览:717
pythonworkbook模块 浏览:245
什么app能查医生 浏览:175
轻量级的编程语言 浏览:338
程序员那么可爱生孩子 浏览:432
后缀him3加密文件是什么软件 浏览:984
坚果隐藏app为什么要140版本才能用 浏览:313
淘宝dns服务器地址 浏览:259
领英转型app哪个好用 浏览:943
压缩软件的图标 浏览:97
卖鞋哪个app是真的 浏览:469