/*******************************************************
* filename: Receive.c* Description:Receive data from Serial_Port
* Date:
*******************************************************/
/*********************头文件定义***********************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include "math.h"
#define max_buffer_size 1
/*定义缓冲区最大宽度*/
/*********************************************************/
int fd,s;
int Getsignal(int signal,int itty)
{
unsigned char hd[max_buffer_size]; /*定义接收缓冲区*/
int flag_close, retv;
struct termios opt;
int realdata=0;
if(open_serial(itty)!=0)
{ signal=-1;return -1;}
// printf("Open Serial OK!\n");
/*******************************************************************/
tcgetattr(fd,&opt);
cfmakeraw(&opt);
/*****************************************************************/
cfsetispeed(&opt,B9600); /*波特率设置为 9600bps*/
cfsetospeed(&opt,B9600);
/*************************开始接收数据******************************/
tcsetattr(fd,TCSANOW,&opt);
//printf("ready for receiving data...\n");
memset(hd,0,sizeof(hd));
retv=read(fd,hd,1);
signal=(int)hd[0];
//printf("%d\n",signo);
flag_close =close(fd);
return signal;
}
int open_serial(int k)
{
/*串口选择*/
char port[10];
sprintf(port,"%d",k);
char ttrstr[15]= "/dev/ttyS";
strcat(ttrstr,port);
printf("%s Will be Open!\n",ttrstr);
fd = open(ttrstr,O_RDWR|O_NOCTTY); /*读写方式打开串口*/
if(fd == -1) /*打开失败*/
return -1;
else
return 0;
}
/=========================================================/
root@pfkj-desktop:/wls/jianggq# cat Send.c
/********************************************************
File Name: Send.c
* Description: send data to serial_Port
* Date:
*******************************************************/
/******************头文件定义******************/
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#define max_buffer_size 100
/*定义缓冲区最大宽度*/
/*******************************************/
int fd; /*定义设备文件描述符*/
int flag_close;
int open_serial(int k)
{
/*串口选择*/
char port[10];
sprintf(port,"%d",k);
char ttrstr[15]= "/dev/ttyS";
strcat(ttrstr,port);
fd = open(ttrstr,O_RDWR|O_NOCTTY); /*读写方式打开串口*/
printf("%s Open\n",ttrstr);
if(fd == -1) /*打开失败*/
return -1;
else
return 0;
}
/********************************************************************/
int main(int argc, char *argv[ ] )
{
char sbuf[]="Hello,this is a Serial_Port test!";
/*待发送的内容,以\n 为结束标志*/
int sfd,retv,i;
struct termios option;
int length=sizeof(sbuf);/*发送缓冲区数据宽度*/
/*******************************************************************/
open_serial(1);
/*打开串口 1*/
/*******************************************************************/
printf("ready for sending data...\n"); /*准备开始发送数据*/
while(1)
{
tcgetattr(fd,&option);
cfmakeraw(&option);
/*****************************************************************/
cfsetispeed(&option,B9600); /*波特率设置为 9600bps*/
cfsetospeed(&option,B9600);
/*******************************************************************/
tcsetattr(fd,TCSANOW,&option);
memset(sbuf,0,sizeof(sbuf));
printf("Please input send words:\n");
scanf("%s",sbuf);
//if(sbuf[0]=='@')
//break;
retv=write(fd,sbuf,length); /*接收数据*/
if(retv==-1)
{
perror("write");
}
else
{
printf("the number of char sent is %d\n",retv);
}
}
flag_close =close(fd);
if(flag_close ==-1)
printf("Close the Device failur!\n");
return 0;
}
/****************************结束***********************************/
gcc Receive.c -I../include -fPIC -shared -o libReceive.so
root@pfkj-desktop:/wls/jianggq# cat test.c
#include <stdio.h>
#include <dlfcn.h>
int main(void){
int (*myadd)(int a,int b);//fuction pointer
void *handle;
handle=dlopen("./libGetSerialPort.so",RTLD_LAZY);//open lib file
myadd=dlsym(handle,"Getsignal");//call dlsym function
int res;
while(1)
{
int result=myadd(res,0);
printf("%d\n",result);
}
dlclose(handle);
}
gcc -o test test.c -ldl