6 Star 0 Fork 0

openEuler2020/41-M78-84

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test_2_serial.c 4.98 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2021-04-20 17:27 . final submit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <pthread.h>
#include <sys/time.h>
#define MAX 2
int fd,fd1,fd2;
pthread_t thread[3];
pthread_mutex_t mut;
int set_port(int fd, int nbits)
{
struct termios newtio, oldtio;
if (tcgetattr(fd, &oldtio) != 0)
{
perror("faile\n");
return -1;
}
bzero(&newtio, sizeof(newtio)); //清零
newtio.c_cflag |= CLOCAL | CREAD;//用于本地连接和接收使能
newtio.c_cflag &= ~CSIZE;//设置数据位
switch (nbits)
{
case 7:
newtio.c_cflag |= CS7; break;
case 8:
newtio.c_cflag |= CS8; break;
}
//设置奇校验位
newtio.c_cflag |= PARENB;
//设置波特率
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
//设置停止位
newtio.c_cflag &= ~PARENB;
if ((tcsetattr(fd, TCSANOW, &newtio)) != 0)
{
perror("setting faile\n");
return -1;
}
printf("save \n");
return 0;
}
void* thread0()//用于向ttyAMA0写入数据
{
int i;
printf("thread0 \n");
for (i = 0; i < MAX; i++) {
pthread_mutex_lock(&mut);
if (i == 0) {
printf("write %d\n", i + 1);
char buf1[] = "This is ttyAMA0 First\r\n";
int length = sizeof(buf1);
int j = write(fd, buf1, length);
puts(buf1);
if (j < 0)printf("send faile\n");
printf("%d n", j);
}
else if (i == 1) {
printf("write %d\n", i + 1);
char buf2[] = "This is ttyAMA0 Second\r\n";
int length = sizeof(buf2);
int j = write(fd, buf2, length);
puts(buf2);
if (j < 0)printf("send faile\n");
printf("%d \n", j);
}
sleep(3);
pthread_mutex_unlock(&mut);
}
printf("thread 0 stop\n");
pthread_exit(NULL);
}
void* thread1()//用于向ttyAMA1写入数据
{
int i;
printf("thread1 \n");
for (i = 0; i < MAX; i++) {
pthread_mutex_lock(&mut);
if (i == 0) {
printf("write %d\n", i + 1);
char buf1[] = "This is ttyAMA1 First\r\n";
int length = sizeof(buf1);
int j = write(fd1, buf1, length);
puts(buf1);
if (j < 0)printf("send faile\n");
printf("%d n", j);
}
else if (i == 1) {
printf("write %d\n", i + 1);
char buf2[] = "This is ttyAMA1 Second\r\n";
int length = sizeof(buf2);
int j = write(fd1, buf2, length);
puts(buf2);
if (j < 0)printf("send faile\n");
printf("%d \n", j);
}
sleep(3);
pthread_mutex_unlock(&mut);
}
printf("thread 1 stop\n");
pthread_exit(NULL);
}
void* thread2()//用于向ttyAMA2写入数据
{
int i;
printf("thread2 \n");
for (i = 0; i < MAX; i++) {
pthread_mutex_lock(&mut);
if (i == 0) {
printf("write %d\n", i + 1);
char buf1[] = "This is ttyAMA2 First\r\n";
int length = sizeof(buf1);
int j = write(fd2, buf1, length);
puts(buf1);
if (j < 0)printf("send faile\n");
printf("%d n", j);
}
else if (i == 1) {
printf("write %d\n", i + 1);
char buf2[] = "This is ttyAMA2 Second\r\n";
int length = sizeof(buf2);
int j = write(fd2, buf2, length);
puts(buf2);
if (j < 0)printf("send faile\n");
printf("%d \n", j);
}
sleep(3);
pthread_mutex_unlock(&mut);
}
printf("thread 2 stop\n");
pthread_exit(NULL);
}
void thread_create(void)
{
int temp;
memset(&thread, 0, sizeof(thread));
/*创建线程*/
if ((temp = pthread_create(&thread[0], NULL, thread0, NULL)) != 0)//线程0创建
printf("thread 0 faile\n");
else
printf("thread 0 success\n");
if ((temp = pthread_create(&thread[1], NULL, thread1, NULL)) != 0)//线程1创建
printf("thread 1 faile\n");
else
printf("thread 1 success\n");
if ((temp = pthread_create(&thread[2], NULL, thread2, NULL)) != 0) //线程2创建
printf("thread 2 faile\n");
else
printf("thread 2 success\n");
}
void thread_wait(void)
{
/*等待线程结束*/
if (thread[0] != 0) { //线程0进行写入工作
pthread_join(thread[0], NULL);
printf("write stop thread 0 \n");
}
if (thread[1] != 0) { //线程1进行写入工作
pthread_join(thread[1], NULL);
printf("write stop thread 1 \n");
}
if (thread[2] != 0) { //线程2进行读取工作
pthread_join(thread[2], NULL);
printf("read stop thread 2 \n");
}
}
int main(void) {
int i, j, k;
fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
fd1= open("/dev/ttyAMA1", O_RDWR | O_NOCTTY | O_NDELAY);
fd2= open("/dev/ttyAMA2", O_RDWR | O_NOCTTY | O_NDELAY);
if (-1 == fd||-1==fd1||-1==fd2)printf("Please open the communication port\n");
else
{
i = set_port(fd, 8);
if (i < 0)
{
perror("setting faile\n");
return 0;
}
pthread_mutex_init(&mut, NULL);
printf("creat preadth\n");
thread_create();
printf("deal with message \n");
thread_wait();
close(fd);
close(fd1);
close(fd2);
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/openeuler2020/team-1520021340.git
git@gitee.com:openeuler2020/team-1520021340.git
openeuler2020
team-1520021340
41-M78-84
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385