⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serudp.c

📁 我的毕业设计
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define PORT       5000                       //定义通信端口
#define LENGTH     64

void task(int *counter);
void task2(int *counter);

int gCounter = 0;
int gCounter2 = 0;

//task的变量
int fd;
int size;
int sockfd;                         
int num;                                
char revbuf[LENGTH];


//task2的变量
int fd;
int size2;                      
int num2;                             
char revbuf2[LENGTH];

//共同的变量
char m_strServer[]="192.168.0.10";       
struct sockaddr_in addr_local;
struct sockaddr_in addr_remote; 


int main(void)
{
	pthread_t thrd,thrd2;	
	int result;

       //打开串口0
      fd=open("/dev/ttyS0",O_RDWR);
      printf("the number fd is %d !\n",fd);

      //ser2udp设置
	addr_remote.sin_family=AF_INET;       //UDP方式
       addr_remote.sin_port=htons(PORT);                  //绑定端口
	addr_remote.sin_addr.s_addr=inet_addr((const char*)m_strServer);    //绑定目标IP
	
      //udp2ser设置
	if((sockfd=socket(AF_INET,SOCK_DGRAM,0))==-1)
      	       {
      	       printf("ERROR:Cannot obtain Socket Descriptor!\n");
	       return(0);
          	}
       addr_local.sin_family=AF_INET;                    //UDP方式
       addr_local.sin_port=htons(PORT);                       //绑定端口
	addr_local.sin_addr.s_addr=INADDR_ANY;      //填入本地IP
	if(bind(sockfd,(struct sockaddr *)&addr_local,sizeof(struct sockaddr))==-1)
		{
		printf("ERROR:Cannot bind port %d\n !",PORT);
		return (0);
		}
	else
		{
		printf("OK:Bind successfully!\n");
		}
       size2=sizeof(struct sockaddr);

	fprintf(stderr,"hello world\n"); 

	result = pthread_create(&thrd,NULL,(void*)task,(void*)&gCounter);     //创建线程
	if (result)
	{
		perror("pthread create fail");
		exit(0);	
	}
	result = pthread_create(&thrd2,NULL,(void*)task2,(void*)&gCounter2);    //创建线程2
	if (result)
	{
		perror("pthread create fail");
		exit(0);	
	}
	pthread_join(thrd,NULL);        //等待线程结束
	return 0;
}

//线程:ser2udp
void task(int *counter)
{
      printf("hello world from pthread1!\n");
	while(1)
		{	
                 size=read(fd,revbuf,8);       //从串口0读取数据;size保存读取到的数据量	
                 if(size>0)                             //size>0:读取成功	
	   	       {
		          num=sendto(sockfd,revbuf,size,0,(struct sockaddr * )&addr_remote,sizeof(struct sockaddr_in));    //发送到网络
		          if(num<0)                                    //发送失败		      
		       	{
		       	  printf("ERROR:Cannot send your dta!\n");
		       	}
		          else
		       	{
		       	}
		   	}
		}
}


//task2:udp2ser
void task2(int *counter)
{		
     printf("hello world from pthread2!\n");
	while(1)
		{
		   num2=recvfrom(sockfd,revbuf2,sizeof(revbuf2),0,(struct sockaddr * )&addr_remote,&size2);   //从网络获取数据
		   if(num2<0)                                          //获取失败
		   	{

		   	}
		   else                                                 //否则
		   	{
		   	write(fd,revbuf2,num2);     //写入串口
			  printf("\n");
		   	}
		}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -