/*
****************************************Copyright (c)**************************************************
** Guangzhou Zhiyuan Electronic Co.,LTD.
** graduate school
** http://www.zyinside.com
**
**------------------------------------- File Info ------------------------------------------------------
** File name: talker.c
** Last modified Date: 2005-12-30
** Last Version: 1.0
** Descriptions: talker of UDP.
**------------------------------------------------------------------------------------------------------
** Created by: Ganda
** Created date: 2005-12-27
** Version: 1.0
** Descriptions: Preliminary version.
**
**------------------------------------------------------------------------------------------------------
** Modified by: Chenxibing
** Modified date: 2005-12-30
** Version: 1.0.1
** Descriptions: modified for MagicARM2410.
**
********************************************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#define PORT 5000 // The port which is communicate with server
#define LENGTH 512 // Buffer length
#define DEV_NAME "/dev/ttyS0" //修改这里,填上串口的设备路径,一般在/dev/下
int Open(char *szPath)
{
int s32Fd = 0;
s32Fd = open(szPath, O_RDONLY);
if(s32Fd < 0)
{
fprintf(stderr, "open %s fail\n", szPath);
return -1;
}
return s32Fd;
}
int DVM_COMRead(int fd, char* buff, int len)
{
int iRet;
fd_set wset; //fd为一组文件描述符(fd)的集合
if(NULL == buff)
{
fprintf(stderr, "%s read buff is null\n", __FUNCTION__);
return -1;
}
FD_ZERO(&wset);//将wset清零
FD_SET(fd, &wset);//将fd加入wset
iRet = select(fd + 1, NULL, &wset, NULL, 0);//监视文件的读写变化
if (iRet < 1)
{
fprintf(stderr, "%s select fd fail\n", __FUNCTION__);
return -1;
}
if(FD_ISSET(fd, &wset) <= 0)
{
fprintf(stderr, "%s fd is not ready\n", __FUNCTION__);
return -1;
}
if(len != read(fd, buff, len))
{
fprintf(stderr, "%s read fail\n", __FUNCTION__);
return -1;
}
return 0;
}
int main(int argc, char *argv[])
{ int sockfd; // Socket file descriptor
int num; // Counter of received bytes
char sdbuf[LENGTH]; // Receive buffer
struct sockaddr_in addr_remote; // Host address information
char sdstr[LENGTH]= {};
// char buff[LENGTH]= {};//1024为你所需要读入的数据长度
int ret = 0;
ret = Open(DEV_NAME);
if(ret < 0)
{
return -1;
}
ret = DVM_COMRead(ret,sdstr, LENGTH);
if(ret < 0)
{
return -1;
}
return 0;
/* Check parameters number */
if (argc != 2)
{
printf ("Usage: talker HOST IP (ex: ./talker 192.168.0.94).\n");
return (0);
}
/* Get the Socket file descriptor */
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
printf("ERROR: Failed to obtain Socket Descriptor!\n");
return (0);
}
/* Fill the socket address struct */
addr_remote.sin_family = AF_INET; // Protocol Family
addr_remote.sin_port = htons(PORT); // Port number
inet_pton(AF_INET, argv[1], &addr_remote.sin_addr); // Net Address
bzero(&(addr_remote.sin_zero), 8); // Flush the rest of struct
/* Try to connect the server */
bzero(sdbuf,LENGTH);
num = sendto(sockfd, sdstr, strlen(sdstr), 0, (struct sockaddr *)&addr_remote, sizeof(struct sockaddr_in));
if( num < 0 )
{
printf ("ERROR: Failed to send your data!\n", argv[1], num);
}
else
{
printf ("OK: Sent to %s total %d bytes !\n", argv[1], num);
}
close (sockfd);
return (0);
}
- 1
- 2
前往页