485ser.c

来源:互联网 发布:淘宝钢笔店 编辑:程序博客网 时间:2024/05/20 07:54

include

include

include

include

include

include

include

include

include

include

include

include “netinet/in.h”

include “sys/socket.h”

include “sys/un.h”

include “sqlite3.h”

static sqlite3 *syssetdb = NULL;

define UART_DEVICE “/dev/ttySAC3”

static int fd_uart;
static int socket_fd;
static int nwrite;
static int nreads;
static int i, res, n, len;
static short CRC_DAT;

static int id_adr;
unsigned char id_adr_char[2];
static sqlite3 *db=NULL;
static int result,nRow,nColumn;
static char *errmsg=NULL;
static char **dbresult;
static char sql[128];

unsigned char uart_buf[256];
unsigned char sock_buf[257];
fd_set readfd;
struct timeval timeout;

static int BAUDRATE;

int speed_arr[] = {B9600,B2400,B4800,B9600,B19200,B38400,B57600,B115200};

static struct sockaddr_in server_addr;

static int uart_open(void)
{
struct termios oldtio, newtio;
printf(“\n”);
fd_uart = open(UART_DEVICE, O_RDWR | O_NOCTTY);
if(fd_uart < 0)
{
perror(“error: can’t open serial port!\n”);
exit(1);
}

tcgetattr(fd_uart, &oldtio);bzero(&newtio, sizeof(newtio));//BAUDRATE = B19200;//printf("1111111BAUDRATE=%d",BAUDRATE);cfsetispeed(&newtio, BAUDRATE);cfsetospeed(&newtio, BAUDRATE);newtio.c_cflag |= CS8 | CLOCAL | CREAD;newtio.c_iflag = 0;newtio.c_oflag = 0;newtio.c_lflag = 0;newtio.c_cc[VINTR] = 0;newtio.c_cc[VQUIT] = 0;newtio.c_cc[VERASE] = 0;newtio.c_cc[VKILL] = 0;newtio.c_cc[VEOF] = 0;newtio.c_cc[VTIME] = 0;newtio.c_cc[VMIN] = 1;newtio.c_cc[VSWTC] = 0;newtio.c_cc[VSTART] = 0;newtio.c_cc[VSTOP] = 0;newtio.c_cc[VSUSP] = 0;newtio.c_cc[VEOL] = 0;newtio.c_cc[VREPRINT] = 0;newtio.c_cc[VDISCARD] = 0;newtio.c_cc[VWERASE] = 0;newtio.c_cc[VLNEXT] = 0;newtio.c_cc[VEOL2] = 0;tcflush(fd_uart, TCIOFLUSH);tcsetattr(fd_uart, TCSANOW, &newtio);return 0;

}

short CRC16(unsigned char *Pushdata, unsigned char length)
{
unsigned short Reg_CRC = 0xffff;
unsigned char Temp_reg = 0x00;
unsigned char i, j;
for(i = 0; i < length; i++)
{
Reg_CRC ^= *Pushdata++;
for (j = 0; j < 8; j++)
{
if(Reg_CRC & 0x0001)
Reg_CRC=Reg_CRC >> 1^0xA001;
else
Reg_CRC >>= 1;
}
}
Temp_reg = Reg_CRC >> 8;
return (Reg_CRC << 8 | Temp_reg); //楂樹綆瀛楄妭鎹綅锛岃繑鍥炲€硷細楂樺瓧鑺傚湪鍚庯紝浣庡瓧鑺傚湪鍓?
//浣庡瓧鑺傚湪鍓? 楂樺瓧鑺傚湪鍚庡垰濂界鍚坢odbus-rtu鎶ユ枃鏍煎紡
}

void get_db_data()
{

sprintf(sql,”select * from GetTable where id=%d;”,1);
//printf(“%s”,sql);
result=sqlite3_get_table(db,sql,&dbresult,&nRow,&nColumn,&errmsg);

if(SQLITE_OK==result)
{
id_adr = atoi(dbresult[3]);
printf(“id_adr=%d\n”,id_adr);
printf(“nRow=%d nColumn=%d dbresult=%s %s %s %s \n”,nRow,nColumn,dbresult[0],dbresult[1],dbresult[2],dbresult[3]);

id_adr_char[0] = (id_adr>> 8) & 0xff;id_adr_char[1] = id_adr & 0xff;//printf("id_adr_char[0]=%d  id_adr_char[1]=%d\n",id_adr_char[0],id_adr_char[1]);

}
else
{
printf(“open err\n”);
}

sqlite3_free_table(dbresult);

}

void get_bundrate()
{

int ret_sys, nRow_sys, nColumn_sys;char *errmsg_sys = NULL;char **dbResult_sys;int flag;ret_sys = sqlite3_open("/pqm/pqm/sysset.db", &syssetdb);while(ret_sys != SQLITE_OK){    printf("open syssetdb error\n");    ret_sys = sqlite3_open("/pqm/pqm/sysset.db", &syssetdb);    sleep(1);}ret_sys = sqlite3_get_table(syssetdb, "select * from PqmsetTable", &dbResult_sys, &nRow_sys, &nColumn_sys, &errmsg_sys);if(SQLITE_OK == ret_sys){    flag =  atoi(dbResult_sys[11]);    BAUDRATE = speed_arr[flag];    //printf("*******************\n");    printf("BAUDRATE=%d\n",BAUDRATE);    //printf("********************\n");    }sqlite3_free_table(dbResult_sys);sqlite3_close(syssetdb);

}

void * serial_ser_thread()
{
sleep(1);

get_bundrate();uart_open();        //鎵撳紑涓插彛while(1){    memset(uart_buf, 0, 256);                    //uart_buf 娓呴浂    n = 0;    len = 0;    n = read(fd_uart, uart_buf, 1);    printf("n= %d uart_buf[0]=%d\n", n ,uart_buf[0]);    if((n == 1) && (uart_buf[0]==id_adr_char[1]))            //姣旇緝璁惧鍦板潃, 榛樿涓?        n = read(fd_uart, &uart_buf[1], 1);    else    {        continue;    }    //鍔熻兘鐮? 鎴栬€?    if((n == 1) && ((uart_buf[1] == 3) || (uart_buf[1] == 4)))    {        n = read(fd_uart, &uart_buf[2], 6); //璇?瀵勫瓨鍣ㄥ湴鍧€锛屽瘎瀛樺櫒鏁伴噺锛孋RC鏍¢獙        if(n == 6)        {            CRC_DAT = ((uart_buf[6] << 8) | uart_buf[7]);            if(CRC_DAT == CRC16(uart_buf, 6))    //CRC 鏍¢獙            {                //CRC鏍¢獙閫氳繃, 姝e紡寮€濮嬪鐞嗘暟鎹?                sock_buf[0] = 0;                sock_buf[1] = 0;                sock_buf[2] = 0;                sock_buf[3] = 0;                sock_buf[4] = 0;                sock_buf[5] = 6;                sock_buf[6] = uart_buf[0];                sock_buf[7] = uart_buf[1];                sock_buf[8] = uart_buf[2];                sock_buf[9] = uart_buf[3];                sock_buf[10] = uart_buf[4];                sock_buf[11] = uart_buf[5];                if((socket_fd = socket(AF_INET,SOCK_STREAM, 0)) == -1)                {                    close(socket_fd);                    continue;                }                bzero(&server_addr, sizeof(server_addr));                server_addr.sin_family = AF_INET;                server_addr.sin_port = htons(1000);                server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");                if(connect(socket_fd, (struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1)                {                    close(socket_fd);                    continue;                }                timeout.tv_sec = 1;                timeout.tv_usec = 0;                FD_ZERO(&readfd);                FD_SET(socket_fd, &readfd);                if((nwrite = write(socket_fd, sock_buf, 12)) == -1) {                    close(socket_fd);                    continue;                }                res = select(socket_fd + 1, &readfd, NULL, NULL, &timeout);                switch(res) {                    case -1:                        break;                    case 0:                        break;                    default:                        if(FD_ISSET(socket_fd, &readfd)) {                            if((nreads = read(socket_fd, sock_buf, 256)) == -1) {                                close(socket_fd);                                continue;                            }                            //璇诲埌浜嗘暟鎹? 杩斿洖缁欎覆鍙?                            CRC_DAT = CRC16(&sock_buf[6], (sock_buf[8] + 3));                            sock_buf[sock_buf[8] + 9] = (CRC_DAT >> 8) & 0xff;                            sock_buf[sock_buf[8] + 10] = CRC_DAT & 0xff;                            write(fd_uart, &sock_buf[6], sock_buf[8] + 5);                            printf("sock_buf*****%s",sock_buf);                        }                        break;                }                close(socket_fd);            }        }    }    //鍔熻兘鐮?    else if((n == 1) && (uart_buf[1] == 6))    {        n = read(fd_uart, &uart_buf[2], 6); //璇?瀵勫瓨鍣ㄥ湴鍧€锛岃缃€硷紝CRC鏍¢獙        if(n == 6)        {            CRC_DAT = ((uart_buf[6] << 8) | uart_buf[7]);            if(CRC_DAT == CRC16(uart_buf, 6))    //CRC 鏍¢獙            {                //CRC鏍¢獙閫氳繃, 姝e紡寮€濮嬪鐞嗘暟鎹?                sock_buf[0] = 0;                sock_buf[1] = 0;                sock_buf[2] = 0;                sock_buf[3] = 0;                sock_buf[4] = 0;                sock_buf[5] = 6;                sock_buf[6] = uart_buf[0];                sock_buf[7] = uart_buf[1];                sock_buf[8] = uart_buf[2];                sock_buf[9] = uart_buf[3];                sock_buf[10] = uart_buf[4];                sock_buf[11] = uart_buf[5];                if((socket_fd = socket(AF_INET,SOCK_STREAM, 0)) == -1)                {                    close(socket_fd);                    continue;                }                bzero(&server_addr, sizeof(server_addr));                server_addr.sin_family = AF_INET;                server_addr.sin_port = htons(1000);                server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");                if(connect(socket_fd, (struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1)                {                    close(socket_fd);                    continue;                }                timeout.tv_sec = 1;                timeout.tv_usec = 0;                FD_ZERO(&readfd);                FD_SET(socket_fd, &readfd);                if((nwrite = write(socket_fd, sock_buf, 12)) == -1) {                    close(socket_fd);                    continue;                }                res = select(socket_fd + 1, &readfd, NULL, NULL, &timeout);                switch(res) {                    case -1:                        break;                    case 0:                        break;                    default:                        if(FD_ISSET(socket_fd, &readfd)) {                            if((nreads = read(socket_fd, sock_buf, 256)) == -1) {                                close(socket_fd);                                continue;                            }                            //璇诲埌浜嗘暟鎹? 杩斿洖缁欎覆鍙?                            if(nreads == 12)                            {                                CRC_DAT = CRC16(&sock_buf[6], 6);                                sock_buf[12] = (CRC_DAT >> 8) & 0xff;                                sock_buf[13] = CRC_DAT & 0xff;                                write(fd_uart, &sock_buf[6], 8);                            }                        }                        break;                }                close(socket_fd);            }        }    }    //鍔熻兘鐮?6    else if((n == 1) && (uart_buf[1] == 16))    {        n = read(fd_uart, &uart_buf[2], 5);    //瀵勫瓨鍣ㄥ湴鍧€, 瀵勫瓨鍣ㄦ暟閲? 瀛楄妭鏁板叡5 涓瓧鑺?        if(n == 5)        {            for(i = 0; i < uart_buf[6] + 2; i ++)            {                n = read(fd_uart, &uart_buf[7 + i], 1);                if(n != 1)                    continue;            }            if(i == (uart_buf[6] + 2))            {                CRC_DAT = ((uart_buf[uart_buf[6] + 7] << 8) | uart_buf[uart_buf[6] + 8]);                if(CRC_DAT == CRC16(uart_buf, uart_buf[6] + 7))    //CRC 鏍¢獙                {                    //CRC鏍¢獙閫氳繃, 姝e紡寮€濮嬪鐞嗘暟鎹?                    sock_buf[0] = 0;                    sock_buf[1] = 0;                    sock_buf[2] = 0;                    sock_buf[3] = 0;                    sock_buf[4] = 0;                    sock_buf[5] = uart_buf[6] + 7;                    sock_buf[6] = uart_buf[0];                    sock_buf[7] = uart_buf[1];                    sock_buf[8] = uart_buf[2];                    sock_buf[9] = uart_buf[3];                    sock_buf[10] = uart_buf[4];                    sock_buf[11] = uart_buf[5];                    sock_buf[12] = uart_buf[6];                    for(i = 0; i < uart_buf[6]; i++)                        sock_buf[13 + i] = uart_buf[7 + i];                    if((socket_fd = socket(AF_INET,SOCK_STREAM, 0)) == -1)                    {                        close(socket_fd);                        continue;                    }                    bzero(&server_addr, sizeof(server_addr));                    server_addr.sin_family = AF_INET;                    server_addr.sin_port = htons(1000);                    server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");                    if(connect(socket_fd, (struct sockaddr *)(&server_addr),sizeof(struct sockaddr)) == -1)                    {                        close(socket_fd);                        continue;                    }                    timeout.tv_sec = 1;                    timeout.tv_usec = 0;                    FD_ZERO(&readfd);                    FD_SET(socket_fd, &readfd);                    if((nwrite = write(socket_fd, sock_buf, uart_buf[6] + 13)) == -1) {                        close(socket_fd);                        continue;                    }                    res = select(socket_fd + 1, &readfd, NULL, NULL, &timeout);                    switch(res) {                        case -1:                            break;                        case 0:                            break;                        default:                            if(FD_ISSET(socket_fd, &readfd)) {                                if((nreads = read(socket_fd, sock_buf, 256)) == -1) {                                    close(socket_fd);                                    continue;                                }                                //璇诲埌浜嗘暟鎹? 杩斿洖缁欎覆鍙?                                if(nreads == 12)                                {                                    CRC_DAT = CRC16(&sock_buf[6], 6);                                    sock_buf[12] = (CRC_DAT >> 8) & 0xff;                                    sock_buf[13] = CRC_DAT & 0xff;                                    write(fd_uart, &sock_buf[6], 8);                                }                            }                            break;                    }                    close(socket_fd);                }            }        }    }    else        continue;}close(fd_uart);return 0;

}

void * adr_ser_thread()
{
result = sqlite3_open(“/pqm/www/test/cgi-bin/pqm.db”,&db);
if(result!=SQLITE_OK)
{
printf(“open error\n”);
return;
}
while(1)
{
sleep(1);
get_db_data();
}
sqlite3_close(db);

}

int main(void)
{
pthread_t serial_thread,adr_thread;
pthread_create(&adr_thread, NULL, adr_ser_thread, NULL); //modbus鏈嶅姟绋嬪簭
pthread_create(&serial_thread, NULL, serial_ser_thread, NULL);

pthread_join(serial_thread, NULL);pthread_join(adr_thread, NULL);

}
/*************************************************************************************************/

0 0
原创粉丝点击