📄 comv1.4.c
字号:
#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <pthread.h>
/**********************************************************/
void com2_ini(void);
volatile int fd;
struct termios option;
int OpenDev(char *Dev);
int send (char * data, int count);
/**********************************************************/
int main(int argc, char **argv){
char buff[256]="hello world!";
char *dev = "/dev/ttyS1"; //串口2
fd = OpenDev(dev);
com2_ini();
write(fd, buff, 256);
close(fd);
// exit (0);
}
void com2_ini(void)
{
//**********************************************************
//波特率设置
//*********************************************************
tcgetattr(fd,&option); /* save current modem settings */
cfsetispeed(&option,B9600); /*设置为9600Bps*/
cfsetospeed(&option,B9600);
tcsetattr(fd,TCSANOW,&option);
//**********************************************************
//无效验, 8 位
//*********************************************************
option.c_cflag &= ~PARENB;
option.c_cflag &= ~CSTOPB;
option.c_cflag &= ~CSIZE;
option.c_cflag |= ~CS8;
//**********************************************************
// 1 位停止位
//*********************************************************
option.c_cflag &= ~CSTOPB;
//**********************************************************
// 原始模式(Raw Mode)方式来通讯
//*********************************************************
option.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Input*/
option.c_oflag &= ~OPOST;
}
int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
/*Notice:运行时出现 "No such file or directory"
cd /dev
ln -sf /dev/tts/0 ttyS0*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -