1
嵌入式串口和网络编程实验报告
第一篇:嵌入式串口和网络编程实验报告
嵌入式实验报告
题目:linux
串口和网络编程
一、实验目的:
1、强化本学期所学的相关的内容。
2、掌握串口相关设置。
3、强化基于TCP网络传ily
=
AF_INET;
=
htons(CLIENT_PORT);
=
htons(INADDR_ANY);
if(bind(clifd,(struct
sockaddr*
)&cliaddr,sizeof(cliaddr))
<
0
)
9
{
printf(
"bind
to
port
%d
failure!\n
"
,CLIENT_PORT);
exit(
1
);
}//绑定的目的是让其端口是随机的,否则端口是自增1
//一般情况下client端不用绑定
bzero(&servaddr,
sizeof(servaddr));
=
AF_INET;
inet_aton(argv[1],
&);
=
10
htons(SERVER_PORT);
if(connect(clifd,(
struct
sockaddr
*)&servaddr,
socklen)
<
0)
{
printf(
"can't
connect
to
%s!\n",
argv[1]);
exit(1);
}
runflag=1;
pth=pthread_create(&tidp,NULL,Thread1,NULL);
if(pth!=0){printf("error!");return
-1;}
pth=pthread_create(&tidp2,NULL,Thread2,NULL);
if(pth!=0){printf("error!");return
11
-1;}
pthread_detach(tidp);
pthread_detach(tidp2);
while(runflag){;}
close(clifd);
return
0;
}
4、
相关代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
12
#include
#include
#include
#include
#include
#define
COM0
0
#define
BLOCK_MODE
1
#define
NONBLK_MODE
0
#define
SERVER_PORT
20220
#define
LENGTH_OF_LISTEN_QUEUE
10
#define
QUEUE
2022define
13
BUFFER_SIZE
256
#define
WELCOME_MESSAGE
"welcome
to
connect
the
server."
static
struct
termios
g_newtio,g_oldtio;
static
int
speed_arr[]
=
{
B1152022
B57600,
B38400,
B192022
B9600,
14
B4800,
B2400,
B12022
B300,
B1152022
B57600,
B38400,
B192022
B9600,
B4800,
B2400,
B12022
B300,
};
static
int
name_arr[]
=
{
1152022
57600,
38400,
192022
15
9600,
4800,
2400,
12022
300,
1152022
57600,
38400,
192022
9600,
4800,
2400,
12022
300,
};
int
Init_COM(int
Comm,int
Baudrate,int
Parity,int
Stopbit,int
Flagblock)
{
16
int
ret,i;
char
dev_buf[16];
if(Comm
>
3)
{
printf("Com%d
not
exist\n",Comm);
return
-1;
}
memset(
嵌入式串口和网络编程实验报告 来自淘豆网m.daumloan.com转载请标明出处.