#include "serial.h"
static int fd_uart;
static pthread_t pid;
/**
* 初始化串口/dev/ttyUSB0
* @return 0成功 -1失败
*/
int init_usb()
{
struct termios options;
//打开串口设备
fd_uart = open("/dev/ttyUSB0", O_RDWR);
if(fd_uart < 0){
perror("open err");
return -1;
}
//设置串口属性
tcgetattr(fd_uart, &options);
options.c_cflag |= ( CLOCAL | CREAD );
options.c_cflag &= ~CSIZE;
options.c_cflag &= ~CRTSCTS;
options.c_cflag |= CS8;
options.c_cflag &= ~CSTOPB;
options.c_iflag |= IGNPAR;
options.c_iflag &= ~(ICRNL | IXON);
options.c_oflag = 0;
options.c_lflag = 0;
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
tcsetattr(fd_uart, TCSANOW, &options);
return 0;
}
static float dota_atof (char unitl)
{
if (unitl > 100)
{
return unitl / 1000.0;
}
else if (unitl > 10)
{
return unitl / 100.0;
}
else
{
return unitl / 10.0;
}
}
static int dota_atoi (const char *cDecade)
{
int result = 0;
if (' ' != cDecade[0])
{
result = (cDecade[0] - 48) * 10;
}
result += cDecade[1] - 48;
return result;
}
static float dota_adc (unsigned int ratio)
{
return ((ratio * 3.3) / 1024);
}
void *pthread_serial (void *arg)
{
struct env_info envinfo;
int ret;
while(1){
ret = read(fd_uart, &envinfo, sizeof(struct env_info));
if(ret != sizeof(envinfo)){
continue;
}
env_msg.x = envinfo.x;
env_msg.y = envinfo.y;
env_msg.z = envinfo.z;
env_msg.temperature = envinfo.temp[0] + dota_atof(envinfo.temp[1]);
env_msg.humidity = envinfo.hum[0] + dota_atof(envinfo.hum[1]);
env_msg.ill = envinfo.ill;
env_msg.bet = dota_adc(envinfo.bet);
env_msg.adc = dota_adc(envinfo.adc);
raise(DATA_ARRIVE);
}
}
int start_recv()
{
return pthread_create(&pid, NULL, pthread_serial, NULL);
}
/**
* @brief 等待串口线程
* @return
*/
int wait_recv()
{
return pthread_join(pid, NULL);
}
void led_control(int on)
{
uint8_t cmd;
if(on){
cmd = LED_ON;
}else{
cmd = LED_OFF;
}
write(fd_uart, &cmd, 1);
}
void beep_control(int on)
{
uint8_t cmd;
if(on){
cmd = BEEP_ON;
}else{
cmd = BEEP_OFF;
}
write(fd_uart, &cmd, 1);
}
void fan_control(int speed)
{
uint8_t cmd;
switch(speed){
case 0:
cmd = FAN_OFF;
break;
case 1:
cmd = FAN_1;
break;
case 2:
cmd = FAN_2;
break;
case 3:
cmd = FAN_3;
break;
default:
break;
}
write(fd_uart, &cmd, 1);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
智能家居项目资料.zip (57个子文件)
智能家居客户端
qt_cs.qm 124KB
Qt5Widgets.dll 5.99MB
qt_uk.qm 148KB
icuuc51.dll 1.89MB
iconengines
qsvgicon.dll 60KB
qsvgicond.dll 809KB
qt_sk.qm 147KB
libwinpthread-1.dll 72KB
Qt5Gui.dll 4.41MB
Qt5Core.dll 4.39MB
qt_hu.qm 149KB
smarthome2.exe 950KB
qt_ru.qm 148KB
qt_it.qm 153KB
icuin51.dll 3.21MB
libgcc_s_dw2-1.dll 532KB
gpsvc.dll 776KB
platforms
qwindows.dll 1.21MB
Qt5Network.dll 1.33MB
qt_de.qm 160KB
libstdc++-6.dll 967KB
smart-home-qt.exe 1.82MB
bearer
qgenericbearerd.dll 1.57MB
qgenericbearer.dll 73KB
qnativewifibearer.dll 78KB
qnativewifibearerd.dll 1.53MB
accessible
qtaccessiblequick.dll 65KB
qtaccessiblewidgetsd.dll 3.65MB
qtaccessiblequickd.dll 2.09MB
qtaccessiblewidgets.dll 198KB
IEShims.dll 287KB
icudt51.dll 21.34MB
qt_ja.qm 123KB
imageformats
qtga.dll 46KB
qgif.dll 50KB
qgifd.dll 643KB
qwbmpd.dll 508KB
qtiff.dll 432KB
qtiffd.dll 1.43MB
qsvgd.dll 666KB
qmngd.dll 1.32MB
qicod.dll 652KB
qmng.dll 373KB
qwbmp.dll 44KB
qsvg.dll 45KB
qtgad.dll 673KB
qjpeg.dll 255KB
qjpegd.dll 1.51MB
qico.dll 51KB
src
data_global.c 75B
data_global.h 1KB
Makefile 363B
serial.h 576B
server.c 2KB
server.h 166B
serial.c 3KB
main.c 2KB
共 57 条
- 1
Sunqk5665
- 粉丝: 2319
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0