西门子GPRS模块控制变频器代码
来源:互联网 发布:医美网络咨询师话术 编辑:程序博客网 时间:2024/05/01 07:21
自己写的代码,希望对大家有所帮助,欢迎大家指点。
以下直接上代码:
// First, i will finish the GPRS signal sending function.
//All right, let's roll it! 0|0 Mrbrightside
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <time.h>
#include <locale.h>
#include <time.h>
#include <sys/time.h>
#include <sys/epoll.h>
#include "lib.h"
#define MAX_EVENTS 5
unsigned char command_run[8] = {0x01,0x06,0x10,0x00,0x00,0x01,0x4c,0xca};
unsigned char command_stop[8] = {0x01,0x06,0x10,0x00,0x00,0x05,0x4d,0x09};
unsigned char command_run20[8] = {0x01,0x06,0x20,0x00,0x10,0x00,0x8f,0xca};
#ifdef HAVE_TM_GMTOFF
#define TIMEZONE_OFFSET(foo) foo->tm_gmtoff
#else
#define TIMEZONE_OFFSET(foo) timezone
#endif
//timer set
int timer(char *user_time_setup);
unsigned char *showtime(void);
int delay(int duration);
int high_temp(unsigned char high);
int low_temp(unsigned char low);
char* inttochar(int value);
char *get_commonlog_time(void);
time_t current_time;
const char month_tab[48] =
"Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ";
int use_localtime;
// the serial comport setup function
//the function includes the serial number
int open_port(int fd, int comport,char *path);
//the setup parameters for serial /dev/ttyS0, serial 1 in linuxOS
int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop);
//interact with users
void interaction(int GPRS_FD,unsigned char *CONTAINER,char *INTERACTION_TIMESET);
//extract the real msg from the receiving msg
void extracted_msg(unsigned char *,unsigned char *real_msg);
//GPRS msg receiving module
void rec_gprs_module(int fd,unsigned char *realmsg_container);
//GPRS msg sending module
void send_gprs_module(int fd,unsigned char *sending_msg);
int main(void)
{
int fd_fre,fd_sensor,fd_gprs;
int temp_floor;
int temp_ceil;
temp_floor = 10;
temp_ceil = 18;
int nread, checkval_fre,checkval_sensor;
unsigned char temp_user_set[2];
char *interaction_timeset;
char *path_fre = "/dev/ttyUSB0";
char *path_sensor = "/dev/ttyUSB1";
char *path_gprs = "/dev/ttyUSB2";
char red[256];
unsigned char container[50];
interaction_timeset = argv[1];
// red[256] is character that transfer from the GPRS to the master side
if((fd_fre = open_port(fd_fre,1,path_fre)) < 0)
{
perror("Open_port failed! \n");
return -1;
}
if((checkval_fre = set_opt(fd_fre, 9600, 8, 'n', 1 )) < 0)
{
perror("Set_opt failed! \n");
}
sleep(1);
if((fd_sensor = open_port(fd_sensor,2,sensor)) < 0)
{
perror("Open_port failed! \n");
return -1;
}
if((checkval_sensor = set_opt(fd_sensor, 9600, 8, 'n', 1 )) < 0)
{
perror("Set_opt failed! \n");
}
if((fd_gprs = open_port(fd_gprs,1,path_gprs)) < 0)
{
perror("Open_port failed! \n");
return -1;
}
if((checkval_fre = set_opt(fd_gprs, 9600, 8, 'n', 1 )) < 0)
{
perror("Set_opt failed! \n");
}
sleep(1);
int torecord;
if((torecord = open("/home/Record/temp1.txt",O_CREAT | O_RDWR | O_APPEND,0644)) == -1){
perror("open record failed!");
return 1;
}
sleep(2);
write(fd_sensor,"\x1A",2);
int writeloop;
ssize_t writenum;
for(writeloop = 0 ;writeloop < 500;writeloop++)
{
nread = read(fd_sensor,red,3);
unsigned char ret[4];
memcpy(ret,red,4);
/*int ten,single;
ten = high_temp(ret[1]);
single = low_temp(ret[2]);*/
int result;
result = Base[ret[1]][ret[2]];
if(result <= temp_floor){
delay(5000000);
if((writenum = write(fd_fre,command_run,8)) == -1){
perror("write");
exit(EXIT_FAILURE);
}
delay(5000000);
if((writenum = write(fd_fre,command_run20,8)) == -1){
perror("write");
exit(EXIT_FAILURE);
}
delay(5000000);
}
if(result >= temp_ceil){
delay(5000000);
if((writenum = write(fd_fre,command_stop,8)) == -1){
perror("write");
exit(EXIT_FAILURE);
}
delay(5000000);
}
char *intstring;
intstring = inttochar(result);
char *showline = "The current temperature is: ";
unsigned char *currenttime = showtime();
char inputline[80];
bzero(inputline,sizeof(inputline));
memcpy(inputline,currenttime,strlen(currenttime));
memcpy(inputline + strlen(currenttime),showline,strlen(showline));
memcpy(inputline + strlen(currenttime) + strlen(showline),intstring,strlen(intstring));
memcpy(inputline + strlen(currenttime) + strlen(showline) + strlen(intstring),".\n",2);
if((writenum = pwrite(torecord,inputline,strlen(inputline),strlen(inputline) * writeloop)) == -1){
perror("Pwrite failed!");
return 1;
}
bzero(inputline,sizeof(inputline));
sleep(1);
interaction(fd_gprs,container,interaction_timeset);
temp_user_set[0] = container[0];
temp_user_set[1] = container[1];
temp_ceil = atoi(temp_user_set);
}
char *time2 = get_commonlog_time();
return 0;
}
int delay(int max)
{
int i;
while(i < max){
++i;
}
}
int timer(char *user_time_setup)
{
//char *currenttime;
char *timeinmain;
char copy_from_user[8];
memset(copy_from_user,0,8);
memcpy(copy_from_user,user_time_setup,8);
char cmparr[26];
memset(cmparr,0,26);
timeinmain = showtime();
memcpy(cmparr,timeinmain,24);
if(cmparr[14] == copy_from_user[3] && cmparr[15] == copy_from_user[4]){
printf("Times up ,%s!\n",cmparr);
return 1;
}else{
return 0;
}
}
unsigned char *showtime(void)
{
time_t t;
unsigned char *time_ret;
t = time(NULL);
printf("Current time: %s", ctime(&t));
//the local current time
time_ret = (signed char *)ctime(&t);
return time_ret;
}
char* inttochar(int value)
{
char* lib[76] = {"0","1","2","3","4","5",
"6","7","8","9","10",
"11","12","13","14","15",
"16","17","18","19","20",
"21","22","23","24","25",
"26","27","28","29","30",
"31","32","33","34","35",
"36","37","38","39","40",
"41","42","43","44","45",
"46","47","48","49","50",
"51","52","53","54","55",
"56","57","58","59","60",
"61","62","63","64","65",
"66","67","68","69","70",
"71","72","73","74","75"
};
char *retstring;
retstring = lib[value];
return retstring;
}
int high_temp(unsigned char high)
{
int ret_high;
if( high <= 0x01)
ret_high = 2;
else if(high > 0x01)
ret_high = 3;
return ret_high;
}
int low_temp(unsigned char low)
{
int ret_low;
if(low <= 0x55)
ret_low = 3;
else if (low > 0x55 && low <= 0xaa)
ret_low = 4;
//else if (low > 0xaa && low <= 0xff)
//ret_low = 5;
return ret_low;
}
char *get_commonlog_time(void)
{
struct tm *t;
char *p;
unsigned int a;
static char buf[30];
int time_offset;
if (use_localtime) {
t = localtime(¤t_time);
time_offset = TIMEZONE_OFFSET(t);
} else {
t = gmtime(¤t_time);
time_offset = 0;
}
p = buf + 29;
*p-- = '\0';
*p-- = ' ';
*p-- = ']';
a = abs(time_offset / 60);
*p-- = '0' + a % 10;
a /= 10;
*p-- = '0' + a % 6;
a /= 6;
*p-- = '0' + a % 10;
*p-- = '0' + a / 10;
*p-- = (time_offset >= 0) ? '+' : '-';
*p-- = ' ';
//[27/Feb/1998:20:20:04 +0000]
a = t->tm_sec;
*p-- = '0' + a % 10;
*p-- = '0' + a / 10;
*p-- = ':';
a = t->tm_min;
*p-- = '0' + a % 10;
*p-- = '0' + a / 10;
*p-- = ':';
a = t->tm_hour;
*p-- = '0' + a % 10;
*p-- = '0' + a / 10;
*p-- = ':';
a = 1900 + t->tm_year;
while (a) {
*p-- = '0' + a % 10;
a /= 10;
}
/* p points to an unused spot */
*p-- = '/';
p -= 2;
memcpy(p--, month_tab + 4 * (t->tm_mon), 3);
*p-- = '/';
a = t->tm_mday;
*p-- = '0' + a % 10;
*p-- = '0' + a / 10;
*p = '[';
return p; /* should be same as returning buf */
}
// the following programm is really tough for now,
// but ,God knows, if spending more time , I can totally handle it!
int open_port( int fd, int comport,char *compath)
{
//char *dev[] = { "/dev/ttyS0","/dev/ttyhS1","/dev/ttyS2"};
//long vdisable;
//serail comprot 1
if(comport)
{
fd = open(compath, O_RDWR | O_NOCTTY | O_NDELAY);
if(-1 == fd){
perror("Can't open serial port! \n ");
return (-1);
}
}
if(fcntl(fd,F_SETFL, 0) < 0)
printf("Fcntl failed!\n ");
else
printf("Fcntl = %d \n ",fcntl(fd, F_SETFL, 0));
if(isatty(STDIN_FILENO) == 0 )
printf("Standard input is not a termina device \n");
else
printf("isatty successed! \n");
printf("fd = %d \n",fd);
return fd;
}
//setup serial comport parameter; fd, 9600, 8, 'n',1
int set_opt( int fd, int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio, oldtio;
if ( tcgetattr( fd, &oldtio) != 0 )
{
perror("Setup Serial 1 ");
return -1;
}
bzero(&newtio, sizeof(newtio)); //Initiate the termios struct
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;
}
switch(nEvent)
{
case 'O':
//the next two lines is to setup the odd-even testing bit
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP );
break;
//no testing bit
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}
//the following lines is to setup the bits rate
switch(nSpeed)
{
case 2400:
cfsetispeed( &newtio,B2400);
cfsetospeed( &newtio,B2400);
break;
case 4800:
cfsetispeed( &newtio,B4800);
cfsetospeed( &newtio,B4800);
break;
case 9600:
cfsetispeed( &newtio,B9600);
cfsetospeed( &newtio,B9600);
break;
case 115200:
cfsetispeed( &newtio,B115200);
cfsetospeed( &newtio,B115200);
break;
default :
cfsetispeed( &newtio,B9600);
cfsetospeed( &newtio,B9600);
break;
}
// setup the stop bit
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0; //èù?canonical ê?°?oè??aêó?á???a?êó??o?‰a?????àü‰π?‰∏?á?í‰∏∫???‰Ω?
newtio.c_cc[VMIN] = 0; //èù?canonical ê?°?oè??aá??êú??∞è?≠óá¨?ê?∞?o?MIN‰∏a???êò??°?á§∫?éΩêa°??≥readá??êú??∞è?≠ó??éê?∞ ?o?
tcflush(fd, TCIFLUSH);
if( (tcsetattr( fd, TCSANOW, &newtio)) != 0 )
{
perror("Com set failed! \n");
return -1;
}
printf("Comport setup accomplish! \n");
return 0;
}
void send_gprs_module(int fd_for_gprs,unsigned char *sending_msg)
{
int nread, i;
char red[256];
// red[256] is character that transfer from the GPRS to the master side
sleep(2);
write(fd_for_gprs, "AT\r",3);
sleep(1);
nread = read(fd_for_gprs, red, 256);
printf(" Num_in_fd = %d, Content: %s \n", nread , red);
write(fd_for_gprs,"ATE1\r",5);
sleep(2);
nread = read(fd_for_gprs,red,256);
printf("from com:%s",red);
write(fd_for_gprs, "AT+CMGF=1\r",10);
sleep(2);
nread = read(fd_for_gprs, red, 256);
//if ( strstr(red, "OK") == NULL );
//printf("This first step failed! \n");
printf(" Num_in_fd = %d, Content: %s \n", nread , red);
write(fd_for_gprs, "AT+CMGS=\"13810288502\"\r",22);
sleep(2);
nread = read(fd_for_gprs, red, 256);
//if( strstr(red, "ATV1") == NULL );
//printf("This second step failed! \n");
printf(" Num_in_fd = %d, Content: %s \n", nread , red);
write(fd_for_gprs, sending_msg,strlen(sending_msg));
sleep(2);
nread = read(fd_for_gprs, red, 256);
//if( strstr(red, "CMGF") == NULL );
//printf("This third step failed! \n");
printf(" Num_in_fd = %d, Content: %s \n", nread , red);
sleep(1);
//write(fd, "GPRS_WORKS!HAVE_A_WONDERFUL_NIGHT!",35);
write(fd_for_gprs, "\x1A" ,1);
//printf("Data: %s \n", red);
//write(fd, "\x41",2);
sleep(1);
nread = read(fd_for_gprs, red, 256);
//sleep(3);
printf(" Num_in_fd = %d, Content: %s \n", nread , red);
return;
}
void rec_gprs_module(int fd_for_gprs,unsigned char *container_msg)
{
int epfd;
int Wait;
int j;
int ready;
int epoll_condition = 1;
int msg_counter = 0;
int stop_counter = 0;
int readloop = 0;
struct epoll_event ev;
struct epoll_event evlist[MAX_EVENTS];
int numread,numwrite;
// the following lines are AT command line;
char *step1 = "ATE1\r";//4 bytes
char *step2 = "ATV1\r";//4 bytes
char *step3 = "AT+CNMI=2,1\r";//12 bytes
char *step4 = "AT+CMGF=1\r";//10bytes
unsigned char buffer[256];
memset(buffer,0,256);
char step5[17];
memset(step5,0,16);
char order[2];
char step6[11] = "AT+CMGR=";//Attention: unsufficient array space will cause unexpected
//error,such as repeative write;
char msg_buffer[256];
unsigned char stopbit[1] = {0x0d};
int loop;
sleep(1);
//step1:check the connection quality;command line:"ATE1\r"
if((numwrite = write(fd_for_gprs,step1,strlen(step1))) == 0){
perror("Step1 failed!");
exit(EXIT_FAILURE);
}
sleep(1);
if((numread = read(fd_for_gprs,buffer,256)) == 0){
perror("read");
}else{
//memcpy(buffer,"\n",strlen(buffer));
printf("%s\n",buffer);
}
//memset(buffer,0,256);
//step2:connect the gprs net;command line:"ATV1\r"
sleep(1);
if((numwrite = write(fd_for_gprs,step2,strlen(step2))) == 0){
perror("Step2 failed!");
exit(EXIT_FAILURE);
}
sleep(1);
if((numread = read(fd_for_gprs,buffer,256)) == 0){
perror("read");
}else{
//memcpy(buffer,"\n",strlen(buffer));
printf("%s\n",buffer);
}
//memset(buffer,0,256);
//step3:applying the short messages notification function;command line:"AT+CNMI=2,1\r"
sleep(1);
if((numwrite = write(fd_for_gprs,step3,strlen(step3))) == 0){
perror("Step3 failed!");
exit(EXIT_FAILURE);
}
sleep(1);
if((numread = read(fd_for_gprs,buffer,256)) == 0){
perror("read");
}else{
//memcpy(buffer,"\n",strlen(buffer));
printf("%s\n",buffer);
}
//memset(buffer,0,256);
//step4:specify to receive the english format short messages;command line:"AT+CMGF=1"
sleep(1);
if((numwrite = write(fd_for_gprs,step4,strlen(step4))) == 0){
perror("Step4 failed!");
exit(EXIT_FAILURE);
}
sleep(1);
if((numread = read(fd_for_gprs,buffer,256)) == 0){
perror("read");
}else{
//memcpy(buffer,"\n",strlen(buffer));
printf("%s\n",buffer);
}
numread = 0;
//memset(buffer,0,256);
//step5:identify the order of coming short message;command line :pending "+CMTI: "SM",(order)"
for(Wait = 0;Wait < 300;Wait++){
sleep(1);
if((numread = read(fd_for_gprs,buffer,256)) != 0){
memcpy(step5,buffer,16);
memcpy(step5 + strlen(step5),"\n",1);
printf("step5:%s",step5);
order[0] = step5[14];
order[1] = step5[15];
//order[2] = '\n';
//printf("%s",order);
break;
}else{
printf("Not yet received SM! The %d round.\n",Wait);
};
}
//memset(buffer,0,256);
//step6:display the content of the current received short messages
printf("before memcpy step6:%s\n",step6);
printf("order:%s\n",order);
memcpy(step6 + strlen(step6),order,2);
printf("add order step6:%s\n",step6);
memcpy(step6 + strlen(step6),"\r",1);
printf("ready to write step6:%s\n",step6);
write(fd_for_gprs,step6,strlen(step6));
printf("srting wirte to GPRS:%s\n",step6);
epfd = epoll_create(1);
if (epfd == -1)
perror("epoll_create");
ev.events = EPOLLIN;
ev.data.fd = fd_for_gprs;
if (epoll_ctl(epfd, EPOLL_CTL_ADD, fd_for_gprs, &ev) == -1)
perror("epoll_ctl");
numread = 0;
while(fd_for_gprs > 0){
printf("About to epoll_wait()\n");
ready = epoll_wait(epfd, evlist, MAX_EVENTS, -1);
if (ready == -1){
if (errno == EINTR)
continue;
else
perror("epoll_wait");
}
printf("Ready: %d\n", ready);
for (j = 0; j < ready; j++){
printf(" fd=%d; events: %s%s%s\n", evlist[j].data.fd,
(evlist[j].events & EPOLLIN) ? "EPOLLIN" :"",
(evlist[j].events & EPOLLHUP) ? "EPOLLHUP " :"",
(evlist[j].events & EPOLLERR) ? "EPOLLERR" : "" );
if (evlist[j].events & EPOLLIN) {
numread = read(evlist[j].data.fd, buffer, 256);
//msg_counter += numread;
printf("MSG_COUNTER:%d\n",msg_counter);
memcpy(msg_buffer + msg_counter,buffer,numread);
msg_counter += numread;
printf("MSG_BUFFER:%s\n",msg_buffer);
if (numread == -1)
perror("read");
printf(" read %d bytes: %.*s\n", numread, numread, buffer);
} else if (evlist[j].events & (EPOLLHUP | EPOLLERR)){
printf(" closing fd %d\n", evlist[j].data.fd);
if (close(evlist[j].data.fd) == -1)
perror("close");
}
}
for(readloop = 0;readloop < numread;readloop++){
if(buffer[readloop] == stopbit[0])
++stop_counter;
printf("stopbit:%d\n",stop_counter);
}
if(stop_counter == 6)
break;
}
//display the whole msg
printf("The total content in current message:%s\n",msg_buffer);
//dispaly the extracted msg from msg_buffer
extracted_msg(msg_buffer,container_msg);
printf("real_msg:%s\n",container_msg);
return;
}
void interaction(int Gprs_rec_fd,unsigned char *container,char *interaction_timeset)
{
int duration;
int ret_from_timer;
//unsigned char *reply_from_user;
unsigned char *response = "Thanks for your reply,your reply is:";
unsigned char response_buffer[128];
unsigned char *Notification = "Hello world!\nIt's my honor to provide summer-breeze-like servie for you,hope you enjoy it!\nI will wait your reply for 5 minutes.";
//memset(msg_buffer,0,256);
memset(response_buffer,0,128);
//hold and take a rest
sleep(1);
for(duration =0;duration < 10000;duration++){
if(ret_from_timer = timer(interaction_timeset)){
send_gprs_module(Gprs_rec_fd,Notification);
break;
}else{
printf("Not yet come to sending short massege! %d second(s)elapsed!.\n",duration);
};
sleep(1);
}
rec_gprs_module(Gprs_rec_fd,container);
memcpy(response_buffer,response,strlen(response));
memcpy(response_buffer + strlen(response),container,strlen(container));
//last step
send_gprs_module(Gprs_rec_fd,response_buffer);
}
void extracted_msg(unsigned char *extracted_str,unsigned char *real_msg)
{
unsigned char *ptr;
int real_counter = 0;
int stopchar_counter = 0;
memset(real_msg,0,50);
ptr = extracted_str;
while((*ptr != 0x00)){
if(*ptr == 0x0a){
++stopchar_counter;
printf("stopchar_counter:%d\n",stopchar_counter);
}
if(stopchar_counter ==2 && stopchar_counter != 3){
real_msg[real_counter] = *(ptr + 1);
printf("Real_msg[%d]:0x%x\n",real_counter,real_msg[real_counter]);
++real_counter;
}
++ptr;
}
}
- 西门子GPRS模块控制变频器代码
- 西门子GPRS模块开发详解
- 西门子MC55 GPRS模块的怪事
- 西门子变频器参数设置【转】
- 全球最小的GPRS模块-西门子BGS2模块
- AT 指令控制GSM/GPRS 模块
- mini2440串口控制GPRS模块程序
- mini2440串口控制GPRS模块程序
- GPRS模块AT呼叫控制命令
- 上海西门子v20变频器维修帝衡西门子v20变频器维修13918419688
- Cinterion(原西门子)于2011年2月14日情人节发布全球最小的GPRS模块---BGS2
- 西门子6SE70变频器跳F008故障处理方法
- 西门子模块一级代理
- 西门子243模块通信
- MSP430与GPRS模块的串口通信硬件流控制
- 变频器
- 变频器
- 西门子高速WCDMA模块---PH8
- 解决IE8下jQueryUI的dialog组件CSS变形
- The Rox Java NIO Tutorial
- java去掉html标签
- Android 自动化测试—robotium(八)拖拽
- 日期处理bean
- 西门子GPRS模块控制变频器代码
- Android Jni代码示例讲解
- CentOS 查找软件是否安装
- swift+keystone 部署小记
- 自带设备(BYOD)的三个黑暗面 – 隐私、个人数据遗失和设备遭窃
- android编程小技巧集锦
- list和数组的转换
- android:添加usb键盘+按键布局和映射的修改
- 编程中无穷大常量的设定技巧