/*串口通信
*By YuanPengfei
*Email:ypf1990@foxmail.com
*2014-3-17
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
//#include <sys/poll.h>
#include <linux/poll.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <termios.h>
#include <time.h>
#include <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "serial.h"
#define DEBUG
#ifdef DEBUG
#define DPRINTF( x... ) printf("serial.c: " x)
#else
#define DPRINTF( x... )
#endif
int msqid; //消息队列id
unsigned int out_buf_len;
int intentTo = INTENT_NONE;
unsigned int isgatherData = 0;
void rx_process(int intent)
{
serial_msg_t msg_info;
switch (intent) {
case INTENT_START:
DPRINTF("rx_process start signal send\n");
msg_info.mtype = SERIAL_DISP_MSG_TYPE;
msg_info.info_type = INFO_TYPE_ACTION;
msg_info.info_len = 1;
msg_info.info[0] = ACTION_START;
msgsnd(msqid, &msg_info, (sizeof(serial_msg_t) - sizeof(long)), 0);
intentTo = INTENT_NONE;
isgatherData = 1;
break;
case INTENT_STOP:
DPRINTF("rx_process stop signal send\n");
msg_info.mtype = SERIAL_DISP_MSG_TYPE;
msg_info.info_type = INFO_TYPE_ACTION;
msg_info.info_len = 1;
msg_info.info[0] = ACTION_STOP;
msgsnd(msqid, &msg_info, (sizeof(serial_msg_t) - sizeof(long)), 0);
intentTo = INTENT_NONE;
isgatherData = 0;
break;
}
}
void send_data()
{
serial_msg_t msg_info;
DPRINTF("send_data start send data...\n");
msg_info.mtype = SERIAL_DISP_MSG_TYPE;
msg_info.info_type = INFO_TYPE_DATA;
msg_info.info_len = 8;
msg_info.info[0] = out_buf[0];
msg_info.info[1] = out_buf[1];
msg_info.info[2] = out_buf[2];
msg_info.info[3] = out_buf[3];
msg_info.info[4] = out_buf[4];
msg_info.info[5] = out_buf[5];
msg_info.info[6] = out_buf[6];
msg_info.info[7] = out_buf[7];
msgsnd(msqid, &msg_info, (sizeof(serial_msg_t) - sizeof(long)), 0);
}
void make_tx_data(int fdd, unsigned char *buf, unsigned char len)
{
unsigned char i;
out_buf_len = 0;
//get data
for (i = 0; i < len; i++) {
out_buf[out_buf_len++] = buf[i];
}
//send data
write(fdd, out_buf, out_buf_len);
}
void make_rx_data(int fdd)
{
}
int main(int argc, char *argv[]) {
int fdd, len, sdfd;
struct termios opt;
pid_t pid, gui_pid;
key_t key; //ipc 通信
char cur_dir[128];
unsigned char par_buf[9];
serial_msg_t msg_info;
/* 建立消息队列 */
key = ftok(IPC_KEY_PATH, IPC_MSG_PROJ_ID);//建立ipc通信
if (key == -1) {
perror("serial.c:cannot generate IPC key");
exit(1);
}
msqid = msgget(key, IPC_CREAT|0777);
printf("msqid %d \n", msqid);
if(msqid == -1)
{
perror("comm_if:cannot create message queue");
exit(1);
}
/**********************************************/
/* 打开串口 */
printf("Open Serial_Port... ...\n");
if ((fdd = open(COMM_PORT, O_RDWR | O_NOCTTY | O_NDELAY))== -1) {
printf("ERROR: Cannot open the desired port \n");
return -1;
}
//tcgetattr函数用于获取与终端相关的参数
tcgetattr(fdd, &opt); // get this port termios struct
//cfgetispeed函数用于获得结构体termios_p中的输入波特率信息,而cfgetospeed函数用于获得结构体termios_p中的输出波特率信息
cfsetispeed(&opt, B38400); //input baudrate
cfsetospeed(&opt, B38400); //output baudrate
opt.c_cflag &= ~CSIZE;
opt.c_cflag |= CS8; // 8 bits length
opt.c_cflag &= ~(CSTOPB | PARENB | CRTSCTS); // 1 bit stop, no parity no flow ctrl
opt.c_cflag |= CLOCAL | CREAD;
opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw input
/* 输入字符保持原样 */
opt.c_iflag &= ~(IGNBRK | BRKINT |INPCK | ISTRIP | INLCR | IGNCR | ICRNL | IUCLC);
opt.c_iflag &= ~(IXON | IXOFF | IXANY); // no software flow control
opt.c_oflag &= ~OPOST; // raw output
opt.c_cc[VMIN]=0;
opt.c_cc[VTIME]=0;
//tcsetattr函数用于设置终端参数
tcsetattr(fdd, TCSANOW, &opt);
/**********************************************/
/* 创建子进程打开GUI 人机界面 */
#if 1
//启动GUI
pid = fork();
if (pid < 0) {
perror("serial.c: Cannot Create New Process");
return -1;
} else if (pid == 0) { // in the child process
DPRINTF("start GUI process\n");
//dir=get_current_dir_name();
//printf(" current dir %s\n", dir);
gui_pid = getpid();
if (setpriority(PRIO_PROCESS, gui_pid, -10) == -1) { /* 设置线程调度优先级为优先 */
DPRINTF("GUI setprority error \n");
}
//support boot from nandflash
if (sdfd = open("/mnt/sdcard/gui", O_RDONLY)== -1) {
//DSA
DPRINTF("start injector-gui from Nand Flash\n");
strcpy(cur_dir,"/home"); //dir);
strcat(cur_dir,"/analysicpro");
DPRINTF("cur_dir %s\n", cur_dir);
execl(cur_dir, "/analysicpro", "-qws", NULL);
} else {
//support boot from sdcard
DPRINTF("start GUI from SD\n");
close(sdfd);
strcpy(cur_dir, "/mnt/sdcard/");
strcat(cur_dir, "/analysicpro");
execl(cur_dir, "analysicpro", "-qws", NULL);
}
}
#endif
/**********************************************/
/* 查询是否空闲 */
#if 0
out_buf[0] = 0xCC;
out_buf[1] = 0x33;
write(fdd, out_buf, 2);
#endif
/**********************************************/
//监听串口处发来的信息
while (1) {
msg_info.mtype = 0;
if (msgrcv(msqid, &msg_info, (sizeof(serial_msg_t) - sizeof(long)), DISP_SERIAL_MSG_TYPE, IPC_NOWAIT) == -1) {
if(errno != ENOMSG) {
perror("comm_if: cannot receive message from the queue");
exit(1);
}
}
if (msg_info.mtype != 0) {
switch (msg_info.info_type) {
case INFO_TYPE_ACTION:
DPRINTF("INFO_TYPE_ACTION INFO_TYPE_ACTION INFO_TYPE_ACTION...\n");
DPRINTF("msg.info[0] = %d...\n", msg_info.info[0]);
if (msg_info.info[0] == ACTION_START) {
intentTo = INTENT_START;
DPRINTF("msg.info[0] = %d...\n", msg_info.info[0]);
DPRINTF("ACTION_START...\n");
cmd_buf[0] = 0xCC;
cmd_buf[1] = 0x33;
write(fdd, cmd_buf, 2);
} else if (msg_info.info[0] == ACTION_STOP) {
intentTo = INTENT_STOP;
isgatherData = 0;
DPRINTF("msg.info[0] = %d...\n", msg_info.info[0]);
DPRINTF("ACTION_STOP...\n");
cmd_buf[0] = 0xCC;
cmd_buf[1] = 0x33;
write(fdd, cmd_buf, 2);
}
break;
}
}
while((len = read(fdd, in_buf, 8)) > 0) {
DPRINTF("Rx: %x %x %d\n", in_buf[0], in_buf[1], len);
//if start gather data
if (isgatherData) {
int count;
for (count = 0; count < 8; count++) {
out_buf[count] = in_buf[count];
DPRINTF("out_buf[%d] = %d\n", count, out_buf[count]);
}
send_data();
} else {
if (in_buf[0] == 0x01 && in_buf[1] == 0xfe) {
DPRINTF("Not Busy\n");
rx_process(intentTo);
}
}
}
}
return 0;
}
[分享]分享曾经做过的一个嵌入式系统application框架
5星 · 超过95%的资源 需积分: 23 33 浏览量
2015-03-26
01:14:38
上传
评论 13
收藏 12KB RAR 举报
__阿飞__
- 粉丝: 60
- 资源: 105
最新资源
- 基于SSM的电影购票系统 框架:Spring+SpringMVC+MyBatis+JSP
- UE5插件 后台WebApi的Restful接口请求交互
- 易语言简单绘制Demo
- 数值积分-复化梯形求积公式 - 北太天元
- DUmeter-8050exe
- 毕业设计:Java项目之jsp医院药品采购管理系统(源码 + 数据库 + 说明文档)
- JavaScript DOM视频教程包含源码作业及项目实战
- 简历-求职简历-word-文件-简历模版免费分享-应届生-高颜值简历模版-个人简历模版-简约大气-大学生在校生-求职-实习
- UMG图表控件+UE5插件+曲线图、饼图、环状图、柱状图
- 简历-求职简历-word-文件-简历模版免费分享-应届生-高颜值简历模版-个人简历模版-简约大气-大学生在校生-求职-实习
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
前往页