当前位置:首页 » 编程软件 » linux串口编程

linux串口编程

发布时间: 2022-01-08 01:36:19

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;
}

⑵ Linux RS485串口编程

对于编程来说,没什么区别,通过控制485的使能端该程序完全可以使用。唯一的区别就是你在发送的时候通过程序把485的控制脚拉高,接收的时候把他拉低就可以了。至于电气方面的区别:RS232是全双工,可以同时收发,RS485是半双工,不能同时收发,还有电平信号不一样,这个编程你就不要理了。

⑶ linux文件编程和串口编程的基本概念是什么

简单说几句吧,linux下的设备都是文件,流程也无非是open, read/write, close等当然,串口你得设置各种属性才行对不对,比如在win下的超级终端就设置了波特率啊,停止位啊,奇偶校验啊什么的,这些属性都通过 int tcgetattr(int fd, struct termios *termios_p); int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);函数来设置。完整代码吗自己去google,一把一把的,其实最重要的是设置好属性,剩下的就是read,write的问题咯。希望对你有用对了,了解终端函数的详情请在linux命令行终端获取: man termios

⑷ 如何实现linux下的串口中断编程

#include
#include
#include
#include
#include
#include

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 /* POSIX 系统相容 */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

void signal_handler_IO (int status); /* 定义讯号处理程序 */
int wait_flag=TRUE; /* 没收到讯号的话就会是 TRUE */

main()
{
int fd,c, res;
struct termios oldtio,newtio;
struct sigaction saio; /* definition of signal action */
char buf[255];

/* 开启装置为 non-blocking (读取功能会马上结束返回) */
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

/* 在使装置异步化前, 安装讯号处理程序 */
saio.sa_handler = signal_handler_IO;
saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);

/* 允许行程去接收 SIGIO 讯号*/
fcntl(fd, F_SETOWN, getpid());
/* 使档案ake the file descriptor 异步 (使用手册上说只有 O_APPEND 及
O_NONBLOCK, 而 F_SETFL 也可以用...) */
fcntl(fd, F_SETFL, FASYNC);

tcgetattr(fd,&oldtio); /* 储存目前的序列端口设定值 */
/* 设定新的序列端口为标准输入程序 */
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

/* 等待输入讯号的回圈. 很多有用的事我们将在这做 */
while (STOP==FALSE) {
printf(".\n");usleep(100000);
/* 在收到 SIGIO 后, wait_flag = FALSE, 输入讯号存在则可以被读取 */
if (wait_flag==FALSE) {
res = read(fd,buf,255);
buf[res]=0;
printf(":%s:%d\n", buf, res);
if (res==1) STOP=TRUE; /* 如果只输入 CR 则停止回圈 */
wait_flag = TRUE; /* 等待新的输入讯号 */
}
}
/* 回存旧的序列端口设定值 */
tcsetattr(fd,TCSANOW,&oldtio);
}

⑸ linux 串口编程乱码

关于串口设备,最好联系厂家询问是否提供API接口。

关于API接口一般是一个可供调用的DLL文件。

如果有可以直接在C#中引用,作为类库来操作设备。

具体调用方式需要询问厂家或参阅api的文档说明。

不提供软件接口的设备是无法进行开发的。

我大概搜了下这个设备,设备是提供配套软件的,那么这款设备是有相关通讯接口类库的,也就是可以用C#进行开发。
如果找不到API文档,
请尝试用串口调试工具,跟踪配套软件的每一步操作,获取串口通讯报文,用C#模拟操作报文自己封装通讯类后进行开发。

⑹ 如何在LINUX下编写一个C语言的串口程序

1、参考这个:POSIX操作系统串口编程指南和 UNIX环境高级编程。
2、简单介绍一下:
《POSIX操作系统的串口编程指南》是在UNIX环境或PC上对串口进行编程的教程,每一章提供的例程都使用POSIX(Portable Standard for UNIX)终端控制函数,只需极少的修改就可运行在IRIX 、HP-UX、 SunOS、 Solaris、 Digital UNIX、 Linux等大多数类UNIX操作系统。

⑺ 很简单的linux串口编程问题:fd = open("/dev/ttysn",|XXXXXXX)。其中哪个ttysn具体是多少

没做过linux下的,提供个建议,看成不成。
PC端的串口必须配置正确。 要确定 与 板子的 波特率 要一致。
此外, PC端作为串口总控端, COM端口配置只针对于PC端自己。 比如你使用了COM1口,那么定义的时候,(ttysn 应该是 ttysn1 --- 没用过linux下的不知道是不是在这配置,你要查)

板子端的COM口配置也是只针对于自己,如果你使用板子的COM1和COM2, 那么程序中初始化的时候需要同时把COM1/2都初始化,那么PC端就可以连接任意的板子端口。

PC(COM1) ---- 板子(COM1) 或 PC(COM1) ---- 板子(COM2)

在确保板子硬件没有问题的情况下,且PC端程序无误, 如果PC端无法接收到数据, 尝试
在PC端编写程序时,在 接收数据之前 加上时间延迟。 也就是说,PC发出数据后需要等待
一段时间才能接收到板子 返回的数据。 具体时间测试来看。

⑻ 关于linux下多串口编程

或许 是 硬件资源 的 问题
串口 相关的 中断、IO 地址等等

⑼ linux下怎样对串口编程

使用串口协议登录Linux终端控制台,通过Zmodem文件传输协议接收一个外部文件。 命令:rz -y 会弹出文件浏览窗口,选择要上传的文件即可。 -y 表示若文件已存在,则覆盖。

热点内容
license服务器是什么 发布:2024-10-18 20:13:56 浏览:975
我的世界电脑几种最好玩的服务器 发布:2024-10-18 20:10:22 浏览:399
手机联系人云存储 发布:2024-10-18 19:56:04 浏览:529
dnd编程 发布:2024-10-18 19:37:58 浏览:110
matlab编程学习 发布:2024-10-18 19:12:53 浏览:456
c语言赋值函数 发布:2024-10-18 19:10:43 浏览:967
ftp3级 发布:2024-10-18 18:57:11 浏览:45
python的zip 发布:2024-10-18 18:56:05 浏览:574
sql2008清理日志 发布:2024-10-18 18:38:37 浏览:462
linux实战项目 发布:2024-10-18 18:30:20 浏览:359