智能停车管理系统C语言

Linux下虚拟机服务器:

main.c

C语言``>

#include <stdio.h>
#include “PLot.h”
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include “db.h”

#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#define LEDON _IO(‘L’, 0)
#define LEDOFF _IO(‘L’, 2)

DB database;
PE Personal;
DB database1;
int main()
{
int fd_4 = open("/dev/led3", O_RDWR);
int fd_3 = open("/dev/led2", O_RDWR);
// 初始化
s.top = -1;
b.top = 0;
p.rear = 0;
p.count = 0;
p.front = 0;
//1. 向操作系统申请网卡功能
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == sockfd)
{
perror(“socket”);
return -1;
}
db_init(&database);

db_load(&database, "my.db");
//2. 被动准备被客户端连接
struct sockaddr_in serveraddr = {0};
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = inet_addr("0.0.0.0");
serveraddr.sin_port = htons(8888);
int len = sizeof(serveraddr);
if (-1 == bind(sockfd, (struct sockaddr *)&serveraddr, len))
{
	perror("connect");
	return -1;
}
listen(sockfd, 10); //开启监听

//3. 提取一路客户端的接入
int clientfd = accept(sockfd, NULL, NULL);
char bufa[200] = {0};
sprintf(bufa, "欢迎来到长江智星智能停车场!\n");
write(clientfd, bufa, sizeof(bufa));
char buf[200] = {0};
int i = 0;

bzero(buf,sizeof(buf));
while (sockfd != -1)
{
    sprintf(buf,"停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n", MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front)% MAX_PAVE);


sleep(1);
write(clientfd,buf,sizeof(buf));
    
bzero(buf,sizeof(buf));
	welcome();
	//4.接收请求
	
	read(clientfd, buf, 100);
	printf("recv: %s\n", buf);
	char buf1[100] = {0};

	//5处理请求

	if (0 == strncmp(buf, "1", 1))
	{

		strcpy(buf1, "请输入即将停车的车牌号:");

		write(clientfd, buf1, sizeof(buf1));
		bzero(buf, sizeof(buf));
		read(clientfd, buf, 100);

		car_come(buf);
		bzero(buf1, sizeof(buf1));
		strcpy(buf1, "停车成功!");
		write(clientfd, buf1, sizeof(buf1));
		ioctl(fd_4, LEDON);
		sleep(3);
		ioctl(fd_4, LEDOFF);
		//continue;
	}

	if (0 == strncmp(buf, "2", 1))
	{

		strcpy(buf1, "请输入即将离开的车牌号:");
		write(clientfd, buf1, sizeof(buf1)); // 输入车牌号
		bzero(buf, sizeof(buf));
		read(clientfd, buf, 100);
		car_leave(buf);
		bzero(buf1, sizeof(buf1));
		write(clientfd, buf, sizeof(buf));
		strcpy(buf1, "请扫码缴费");
		write(clientfd, buf1, sizeof(buf1));
		//continue;
		ioctl(fd_3, LEDON);
		sleep(3);
		ioctl(fd_3, LEDOFF);
	}
	if (0 == strncmp(buf, "3", 1))
	{
		//char bufb[100] = {0};
		//bzero(bufb,sizeof(bufb));
		Display();
		continue;
	}
	if (0 == strcmp(buf, "show"))
	{
		bzero(buf, sizeof(buf));
		char buft[100] = {"请输入您的车牌号:"};
		write(clientfd, buft, sizeof(buft));
		read(clientfd, buf, 100);
		Display(buf);
		bzero(buft, sizeof(buft));
		//bzero(buf1,sizeof(buf1));
		db_show(&database1, buf1);
		strcat(buft, buf1);
		write(clientfd, buft, 100);
	}
	if( 0 == strncmp(buf,"save",4))
	{
	db_save(&database,"my.db");
	printf("====%s===\n",buf);
	}
	if( 0 == strncmp(buf,"6",1))
	{
	char bufi[100] = {0};
	sprintf(bufi,"缴费成功!");
	write(clientfd,bufi,sizeof(bufi));
	}		
			
	if (0 == strncmp(buf, "4", 1))
		break;

	//5处理请求
	char a[30] = {"如需其他操作请先返回"};
	write(clientfd, a, 30);
	bzero(buf, sizeof(buf));
	read(clientfd, buf, 100);

	if (0 == strncmp(buf, "0", 1))
	{
		bzero(buf1, sizeof(buf1));
		strcpy(buf1, "返回成功!");
		write(clientfd, buf1, sizeof(buf1));
		continue;
	}
	else
	{
		char bufz[100] = {0};
		printf("您的输入有误,请重新输入\n");
		bzero(buf, sizeof(buf));
		sprintf(bufz, "您的输入有误,请重新输入\n");
		write(clientfd, bufz, sizeof(bufz));
		//  read(clientfd, buf, 100);

		continue;
	}
	//6. 反馈结果
	write(clientfd, buf1, sizeof(buf1));
}
//7. 关闭通道,归还网卡
close(sockfd);
close(clientfd);

return 0;

}
db.c:
#include “db.h”
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int db_load(DB *database, const char *dbfile)
{
//1. 只读方式打开文件,没有就创建
int fd = open(dbfile, O_RDONLY|O_CREAT, 0666);
if(-1 == fd)
{
perror(“open”);
return -1;
}

//2. 读出文件内容到数据库内存(dbfile---->database)
read(fd, database, sizeof(DB));

//3. 关闭文件
close(fd);

}

int db_save(DB *database, const char *dbfile)
{
//1. 只写方式打开文件
int fd = open(dbfile, O_WRONLY, 0666);
if(-1 == fd)
{
perror(“open”);
return -1;
}

//2. 将数据库内存写入文件(database---->dbfile)
write(fd, database, sizeof(DB));

//3. 关闭文件
close(fd);

}

int db_init(DB *database)
{

database->num = 0;
return 0;

}

/*int db_insert(DB *database, const char *car)
{
if(MAXSIZE == database->num)
return DBFULL;

strcpy(database->users[database->num].car,car);
database->num++;

return 0;

}*/

int db_insert1(PE *Personal, const char *carnum)
{
if(MAXSIZE == Personal->num)
return DBFULL;

strcpy(Personal->Person[Personal->num].carnum,carnum);
Personal->num++;

return 0;

}

int db_delete(DB *database, const char *car)
{
//find it
int i = 0;
for(;inum; i++)
{
if( 0 == strcmp(database->users[i].car, car))

                {
                   //delete
		//move and cover
		while(i < database->num-1)
		{
			database->users[i] = database->users[i+1];
			i++;
		}
		database->num--;
		return 0;
            }
    }

return -1;

}

void db_show(DB *database, char *buf)
{

printf("车牌号\t\t\t停车时间\t\t当前所需金额\n");
int i = 0;

for(;i<database->num; i++)

{
	char bufx[100] = {0};
	sprintf(bufx, "%s\t\n", database->users[i].car);
	printf("%s\t\n",bufx);
     	strcat(buf, bufx);
}



printf("\n");

}

db.h:
#ifndef DB_
#define DB_

#define MAXSIZE 100

//err code
#define DBFULL 1 /* database is full */

typedef struct{
struct{
char car[1000];
}users[MAXSIZE];
int num;
}DB;
typedef struct{
struct{
char car1[6];
}users[MAXSIZE];
int num;
}DB1;
typedef struct{
struct{
char carnum[6];
}Person[MAXSIZE];
int num;
}PE;

int db_load(DB *database, const char *dbfile);
int db_save(DB *database, const char *dbfile);
int db_init(DB *database);
int db_insert(DB *database, char *car);

int db_load1(PE *Persona, const char *dbfile);
int db_save1(PE *Persona, const char *dbfile);
int db_init1(PE *Persona);
//int db_insert1(PE *Personal, char *carnum);
//int db_delete(DB *database, const char *name, const char *pass);
//int db_update(DB *database, const char *name, const char *pass, const char *passnew);
//int db_select(DB *database, const char *name, const char *pass);
void db_show(DB *database, char *buf);

#endif

Plot.c:
//
// Created by PC-Saw on 2018/12/17.
//

#include “PLot.h”
#include<string.h>
#include"db.h"
extern DB database;
extern PE Personal;
extern DB database1;
int db_insert(DB *database, char *car)
{
char *car1 = car;
if(MAXSIZE == database->num)
return DBFULL;

strcpy(database->users[database->num].car,car);
database->num++;


return 0;

}
void stop_to_pave() // 车停入便道
{
// 判断队满
if (p.count > 0 && (p.front == (p.rear + 1) % MAX_PAVE))
{
printf (“便道已满,请下次再来\n”);
}
else
{
strcpy(p.Pave[p.rear].plate, C); // 车进入便道
p.rear = (p.rear + 1) % MAX_PAVE; // 队尾指示器加1
p.count++; // 计数器加1
printf (“牌照为%s的汽车停入便道上的%d的位置\n”, C, p.rear);
}
}

void car_come(char *buf) // 车停入停车位
{

char *bufx = buf;
if (s.top >= MAX_STOP - 1)              // 如果停车位已满,停入便道
{
    stop_to_pave();                     // 停入便道
}
else
{

    s.top++;                            // 停车位栈顶指针加1
    time_t t1;
    long int t = time(&t1);             // 记录进入停车场的时间
    char* t2;
char bufz[100] = {0};
char buf1[10] = {};
    t2 = ctime (&t1);                   // 将当前时间转换为字符串
    s.Stop[s.top].timeIn = t;

    strcpy(s.Stop[s.top].plate, bufx);     // 将车牌号登记
    
  printf ("牌照为%s的汽车停入停车位的%d车位, 当前时间:%s\n", bufx, s.top + 1, t2);
  //sprintf(bufz,"牌照为%s的汽车停入停车位的%d车位, 当前时间:%s\n", bufx, (char)s.top + 1, t2);

  db_insert1(&Personal,bufx);

}

}

void stop_to_buff(char *buf) // 车进入让路栈
{
char *bufy = buf;
//char *bufa[200] = {0};
// 停车位栈压入临时栈,为需要出栈的车辆让出道
while (s.top >= 0)
{
if (0 == strcmp(s.Stop[s.top].plate, bufy))
{
break;
}

    // 让出的车进入让路栈
    strcpy(b.Help[b.top++].plate, s.Stop[s.top].plate);
    printf ("牌照为%s的汽车暂时退出停车场\n", s.Stop[s.top--].plate);
}

// 如果停车位中的车都让了道,说明停车位中无车辆需要出行
if (s.top < 0)
{
    printf ("停车位上无此车消息\n");
}
else
{
    printf ("牌照为%s的汽车从停车场开走\n", s.Stop[s.top].plate);
    time_t t1;
    long int t = time (&t1);
    c.timeOut = t;                        // 标记离开停车场的时间
    char* t2;
    t2 = ctime (&t1);                   // 获取当前时间
    printf ("离开时间%s\n需付%d元\n", t2, (Price * (c.timeOut - s.Stop[s.top].timeIn))/10 );
bzero(bufy,sizeof(bufy));	
sprintf(bufy,"牌照为%s的汽车从停车场开走\n离开时间%s\n需付%d元\n",s.Stop[s.top].plate, t2, (Price * (c.timeOut - s.Stop[s.top].timeIn))/10);
    s.top--;
}

// 将让路栈中的车辆信息压入停车位栈
while (b.top > 0)
{
    strcpy(s.Stop[++s.top].plate, b.Help[--b.top].plate);
    printf ("牌照为%s的汽车停回停车位%d车位\n", b.Help[b.top].plate, s.top);
}

// 从便道中 -> 停车位
while (s.top < MAX_STOP-1)
{
    if (0 == p.count)               // 判断队列是否为空
    {
        break;
    }   // 不为空,将便道中优先级高的车停入停车位
    else
    {
        strcpy(s.Stop[++s.top].plate, p.Pave[p.front].plate);
        printf ("牌照为%s的汽车从便道中进入停车位的%d车位\n", p.Pave[p.front].plate, s.top);
        p.front = (p.front + 1) % MAX_PAVE;
        p.count--;
    }
}

}

void car_leave(char *buf) // 车离开
{
char *bufx = buf ;
if (s.top < 0) // 判断停车位是否有车辆信息
{
printf (“车位已空,无车辆信息!\n”);
}
else
{ db_delete(&database,bufx);
stop_to_buff(bufx);
//db_delete(&database,bufx);
}
//db_delete(&database,bufx);
}

void Display(char *buf)
{
char *bufx = buf;
char *bufy = buf;

char buft[300] = {0};
int i = s.top;
if (-1 == i)
{
    printf ("停车场为空\n");
}
time_t t1;
long int t = time(&t1);             // 标记显示时的时间
while (i != -1)
{
   // printf ("%s\t\t%ld秒\t\t\t%ld元\n", bufx, t - s.Stop[i].timeIn, Price * (t - s.Stop[i].timeIn) / 10);
sprintf(buft,"%s\t\t\t%ld秒\t\t\t%ld元\n", bufx, t - s.Stop[i].timeIn, Price * (t - s.Stop[i].timeIn) / 10);

//db_insert(&database,buft);
    i--;

//s.Stop[i].plate
}
   db_insert(&database1,buft);

}

void welcome()
{
printf ("\t******************目前停车场状况***********************\n");
printf ("\t停车场共有%d个车位,当前停车场共有%d辆车,等候区共有%d辆车\n", MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front)
% MAX_PAVE);
printf ("\t********************************************************\n");
printf ("\t---------------Welcome to our Car Parking---------------\n");
printf ("\t
1.Parking \n");
printf ("\t
2.leaving \n");
printf ("\t
3.situation \n");
printf ("\t
4.exit \n");
printf ("\t
5.show *\n");
printf ("\t--------------------------------------------------------\n");
}
Plot.h:

//
// Created by PC-Saw on 2018/12/17.
//

#ifndef PLOT_H
#define PLOT_H

#define Price 1 // 单价可以自己定义n
#define MAX_STOP 10
#define MAX_PAVE 10

#include <stdlib.h>
#include <stdio.h>

#include <time.h> // 包含时间函数的头文件
#include <string.h>

// 汽车信息
typedef struct
{

int timeIn;              // 进入停车场时间
int timeOut;             // 离开停车场时间
char plate[10];
// 汽车牌照号码,定义一个字符指针类型

}Car;

// 停放栈(用于停放车辆)
typedef struct
{
Car Stop[MAX_STOP]; // 用于停放车辆的栈
int top; // 标记栈顶位置
}Stopping;

// 等候队列
typedef struct
{
int count; // 用来指示队中的数据个数
Car Pave[MAX_PAVE]; // 等候停车的队列
int front, rear; // 标记队头和队尾位置
}Pavement;

// 让路栈
typedef struct
{
Car Help[MAX_STOP]; // 用于让路的队列
int top; // 标记站定位置
}Buffer;

Stopping s;
Pavement p;
Buffer b;
Car c;
char C[10];

void stop_to_pave(); // 车停入便道
void car_come (); // 车停入停车位
void stop_to_buff(); // 车进入让路栈
void car_leave (); // 车离开
void welcome (); // 主界面函数
void Display (); // 显示车辆信息

#endif //PLOT_H

db.h:
#ifndef DB_
#define DB_

#define MAXSIZE 100

//err code
#define DBFULL 1 /* database is full */

typedef struct{
struct{
char car[1000];
}users[MAXSIZE];
int num;
}DB;
typedef struct{
struct{
char car1[6];
}users[MAXSIZE];
int num;
}DB1;
typedef struct{
struct{
char carnum[6];
}Person[MAXSIZE];
int num;
}PE;

int db_load(DB *database, const char *dbfile);
int db_save(DB *database, const char *dbfile);
int db_init(DB *database);
int db_insert(DB *database, char *car);

int db_load1(PE *Persona, const char *dbfile);
int db_save1(PE *Persona, const char *dbfile);
int db_init1(PE *Persona);
//int db_insert1(PE *Personal, char *carnum);
//int db_delete(DB *database, const char *name, const char *pass);
//int db_update(DB *database, const char *name, const char *pass, const char *passnew);
//int db_select(DB *database, const char *name, const char *pass);
void db_show(DB *database, char *buf);

#endif

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于C51单片机的智能停车场车位管理系统是一款可以实现自动检测和管理停车场车位的系统。该系统利用传感器监测每个停车位的状态,并通过C51单片机进行数据处理和控制。 设计中,每个停车位都配备有一个传感器,用于检测是否有车辆停放。当有车辆停放时,传感器会发送信号给C51单片机进行识别和记录。C51单片机会根据传感器的信号判断车位的状态,如果空闲则更新状态为已占用,并将相关信息存储到内存中。同时,LED显示屏会显示车位状态,提供给司机参考。 此外,该系统还具备自动收费功能,当车主离开停车场时,C51单片机会根据停车时间和收费规则计算停车费用,并显示在LED显示屏上供车主查看。车主可以选择通过现金或电子支付方式进行结算。 为了方便管理和查询,该系统还可以连接到一个计算机服务器,将停车位的状态和停车信息进行实时上传。停车场管理员可以通过计算机上的管理软件实时监控停车位的占用情况,也可以查询历史停车记录和收费情况。 基于C51单片机的智能停车场车位管理系统设计可以提高停车场的利用率,减少车辆拥堵现象。通过自动收费和信息管理,可以提高停车场管理员的工作效率,为车主提供更加便捷的停车服务。此外,该系统还可以通过数据分析,统计每个车位的使用率和停车场的收入情况,为停车场的运营提供有力支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值