#include"haeundae.h"
static int convbaud(unsigned long int baudrate)
{
switch (baudrate)
{
case 2400:
return B2400;
case 4800:
return B4800;
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
default:
return B9600;
}
}
static int PortSet(int fdcom, const pportinfo_t pportinfo)
{
struct termios termios_old, termios_new;
int baudrate, tmp;
char databit, stopbit, parity, fctl;
bzero(&termios_old, sizeof(termios_old));
bzero(&termios_new, sizeof(termios_new));
cfmakeraw(&termios_new);
tcgetattr(fdcom, &termios_old); // get the serial port attributions
/*------------设置端口属性----------------*/
// baudrates
baudrate = convbaud(pportinfo->baudrate);
cfsetispeed(&termios_new, baudrate); //填入串口输入端的波特率
cfsetospeed(&termios_new, baudrate); //填入串口输出端的波特率
termios_new.c_cflag |= CLOCAL; //控制模式,保证程序不会成为端口的占有者
termios_new.c_cflag |= CREAD; //控制模式,使能端口读取输入的数据
// 控制模式,flow control
fctl = pportinfo->fctl;
switch (fctl)
{
case '0':
{
termios_new.c_cflag &= ~CRTSCTS; // no flow control
}
break;
case '1':
{
termios_new.c_cflag |= CRTSCTS; // hardware flow control
}
break;
case '2':
{
termios_new.c_iflag |= IXON | IXOFF | IXANY; // software flow control
}
break;
}
//控制模式,data bits
termios_new.c_cflag &= ~CSIZE; //控制模式,屏蔽字符大小位
databit = pportinfo->databit;
switch (databit)
{
case '5':
termios_new.c_cflag |= CS5;
case '6':
termios_new.c_cflag |= CS6;
case '7':
termios_new.c_cflag |= CS7;
default:
termios_new.c_cflag |= CS8;
}
//控制模式 parity check
parity = pportinfo->parity;
switch (parity)
{
case '0':
{
termios_new.c_cflag &= ~PARENB; // no parity check
}
break;
case '1':
{
termios_new.c_cflag |= PARENB; // odd check
termios_new.c_cflag &= ~PARODD;
}
break;
case '2':
{
termios_new.c_cflag |= PARENB; // even check
termios_new.c_cflag |= PARODD;
}
break;
}
//控制模式,stop bits
stopbit = pportinfo->stopbit;
if (stopbit == '2')
{
termios_new.c_cflag |= CSTOPB; // 2 stop bits
}
else
{
termios_new.c_cflag &= ~CSTOPB; // 1 stop bits
}
// other attributions default
termios_new.c_oflag &= ~OPOST; //输出模式,原始数据输出
termios_new.c_cc[VMIN] = 1; //控制字符, 所要读取字符的最小数量
termios_new.c_cc[VTIME] = 1; //控制字符, 读取第一个字符的等待时间 unit: (1/10)second
tcflush(fdcom, TCIFLUSH); //溢出的数据可以接收,但不读
tmp = tcsetattr(fdcom, TCSANOW, &termios_new); //设置新属性,TCSANOW:所有改变立即生效 tcgetattr(fdcom, &termios_old);
return (tmp);
}
int haeundaeControl(const char *usbName,const char *control_order, int order_len)
{
unsigned char status = 1;
int ufd = -3;
int ret = -1;
char checkBuf[8] = { 0xAA, 0x00, 0x06, 0x01, 0xBB }; // check power
char openBuf[8] = { 0xAA, 0x00, 0x03, 0x01, 0xBB }; // open power
char closeBuf[8] = { 0xAA, 0x00, 0x02, 0x01, 0xBB }; // close power
char recvBuf[16] = { 0 };
struct timeval tv;
fd_set set, rset;
FD_ZERO(&set);
portinfo_t portinfo = {
'0', // print prompt after receiving
9600, // baudrate: 9600
'8', // databit: 8
'0', // debug: off
'0', // echo: off
'0', // flow control: software
'0', // default tty: COM1
'0', // parity: none
'1', // stopbit: 1
0 // reserved
};
char rUsbName[24] = { 0 };
sprintf(rUsbName, "/dev/%s", usbName);
ufd = open(rUsbName, O_RDWR | O_NOCTTY);
if (ufd < 0)
{
perror("open power tty");
return -3;
}
PortSet(ufd, &portinfo);
write(ufd,control_order,order_len);
/*
FD_SET(ufd, &set);
CLOSE:
write(ufd, closeBuf, 5);
sleep(1);
write(ufd, checkBuf, 5);
memset(recvBuf, 0, 16);
*/
/* rset = set;
tv.tv_sec = 5;
tv.tv_usec = 0;
ret = select(ufd + 1, &rset, NULL, NULL, &tv);
if (ret == 0)
{
return -4; // timeout for read
}
read(ufd, recvBuf, 16);
status = recvBuf[3];
if (status == 0xFF) // power closed
{
sleep(sleepNum);
OPEN:
write(ufd, openBuf, 5);
sleep(1);
write(ufd, checkBuf, 5);
memset(recvBuf, 0, 16);
tv.tv_sec = 5;
tv.tv_usec = 0;
rset = set;
ret = select(ufd + 1, &rset, NULL, NULL, &tv);
if (ret == 0)
{
return -4; // timeout for read
}
read(ufd, recvBuf, 16);
status = recvBuf[3];
if (status == 0x00) // power opened
{
close(ufd);
return 0;
}
else if (status == 0xFF)
{
goto OPEN;
}
else
{
close(ufd);
return -1; // open status error
}
}
else if (status == 0x00)
{
goto CLOSE;
}
else
{
close(ufd);
return -2; // close status error
}*/
}
.h:
#ifndef _HAEUNDAE_H_
#define _HAEUNDAE_H_
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h> //termios, tcgetattr(), tcsetattr()
#include <unistd.h>
#include <sys/ioctl.h>
typedef struct
{
char prompt; // prompt after reciving data
int baudrate; // baudrate
char databit; // data bits, 5, 6, 7, 8
char debug; // debug mode, 0: none, 1: debug
char echo; // echo mode, 0: none, 1: echo
char fctl; // flow control, 0: none, 1: hardware, 2: software
char tty; // tty: 0, 1, 2, 3, 4, 5, 6, 7
char parity; // parity 0: none, 1: odd, 2: even
char stopbit; // stop bits, 1, 2
const int reserved; // reserved, must be zero
} portinfo_t;
typedef portinfo_t *pportinfo_t;
int haeundaeControl(const char *usbName,const char *control_order, int order_len);
#endif
main:
#include "haeundae.h"
int main()
{
char order[]={0xff,0x01,0x00,0x02,0x20,0x00,0x23};
haeundaeControl("ttyUSB0",order,7);
}