📄 serialconnect.cpp
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/socket.h>
struct serial_port_info
{
int serialno;
int portno;
char serialname[20];
int baudrate;
int databit;
char paritybit[20];
char stopbit[20];
char insoftflowctrl[20];
char outsoftflowctrl[20];
char inhardflowctrl[20];
char outhardflowctrl[20];
pid_t chd_pid;
struct serial_port_info *prev;
struct serial_port_info *next;
};
struct serial_port_manager
{
int port_num;
struct serial_port_info *head;
struct serial_port_info *tail;
};
struct baud_rate_map
{
int rate_num;
int rate_const;
};
struct baud_rate_map g_baud_map[] = {{0, B0},
{50, B50},
{75, B75},
{110, B110},
{134, B134},
{150, B150},
{200, B200},
{300, B300},
{600, B600},
{1200, B1200},
{1800, B1800},
{2400, B2400},
{4800, B4800},
{9600, B9600},
{19200, B19200},
{38400, B38400},
{57600, B57600},
{76800, B57600},
{115200, B115200},
{230400, B230400},
{-1, -1}};
struct serial_port_manager g_serial_port_mgr;
void PrintPortInfo (struct serial_port_info *port_info)
{
if (!port_info)
return;
printf ("Serial port number %d:\n", port_info->serialno);
printf (" port %d name %s\n", port_info->portno, port_info->serialname);
printf (" %d %d %s %s\n", port_info->baudrate, port_info->databit,
port_info->paritybit, port_info->stopbit);
printf ("\n");
}
void InitSerialPortMgr (struct serial_port_manager *port_mgr)
{
port_mgr->port_num = 0;
port_mgr->head = NULL;
port_mgr->tail = NULL;
}
void DeinitSerialPortMgr (struct serial_port_manager *port_mgr)
{
struct serial_port_info *p, *q;
if (port_mgr->port_num <= 0)
return;
p = port_mgr->head;
while (p)
{
q = p;
p = p->next;
delete q;
}
return;
}
char *GetNodeContent (char *node_name, char *buf, char **next)
{
char *open_node, *close_node, *result;
char *p, *q;
int len;
len = strlen (node_name);
open_node = new char[len + 3];
close_node = new char[len + 4];
sprintf (open_node, "<%s>", node_name);
sprintf (close_node, "</%s>", node_name);
p = strstr (buf, open_node);
if (!p)
{
//printf ("GetNodeContent: cannot find start position\n");
result = NULL;
goto GET_NODE_EXIT;
}
q = strstr (p, close_node);
if (!q)
{
//printf ("GetNodeContent: cannot find end position\n");
result = NULL;
*next = NULL;
goto GET_NODE_EXIT;
}
p += strlen (open_node);
len = q - p;
if (len < 0)
{
printf ("GetNodeContent: content error\n");
result = NULL;
*next = NULL;
goto GET_NODE_EXIT;
}
result = new char[len + 1];
memset (result, 0, len + 1);
memcpy (result, p, len);
if (next)
{
q += strlen (close_node);
*next = q;
}
GET_NODE_EXIT:
delete [] open_node;
delete [] close_node;
return result;
}
char *TrimWord (char *word_str)
{
char *p, *q;
p = word_str;
while (*p)
{
if (!isspace (*p))
break;
p ++;
}
q = p;
while (*q)
{
if (isspace (*q))
break;
q ++;
}
*q = '\0';
return p;
}
int AddPortInfo (struct serial_port_info *port_info)
{
struct serial_port_info *p;
if (!port_info)
return -1;
p = g_serial_port_mgr.head;
while (p)
{
if (p->serialno == port_info->serialno)
{
printf ("AddPortInfo: port already exist\n");
return -1;
}
p = p->next;
}
p = g_serial_port_mgr.tail;
g_serial_port_mgr.tail = port_info;
if (p)
p->next = port_info;
if (!g_serial_port_mgr.head)
g_serial_port_mgr.head = port_info;
port_info->prev = p;
port_info->next = NULL;
g_serial_port_mgr.port_num ++;
return 0;
}
void DelPortInfo (struct serial_port_info *port_info)
{
if (!port_info->prev && !port_info->next)
{
g_serial_port_mgr.head = NULL;
g_serial_port_mgr.tail = NULL;
}
else if (!port_info->prev)
{
g_serial_port_mgr.head = port_info->next;
port_info->next->prev = NULL;
}
else if (!port_info->next)
{
g_serial_port_mgr.tail = port_info->prev;
port_info->prev->next = NULL;
}
else
{
port_info->prev->next = port_info->next;
port_info->next->prev = port_info->prev;
}
delete port_info;
g_serial_port_mgr.port_num --;
}
int GetPortConfig (char *buffer)
{
struct serial_port_info *port_info;
char *node_value;
int res;
port_info = new struct serial_port_info;
node_value = GetNodeContent ("SerialNo", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
port_info->serialno = atoi (TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("PortNno", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
port_info->portno = atoi (TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("SerialName", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
strcpy (port_info->serialname, TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("BaudRate", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
port_info->baudrate = atoi (TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("DateBit", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
port_info->databit = atoi (TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("ParityBit", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
strcpy (port_info->paritybit, TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("StopBit", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
strcpy (port_info->stopbit, TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("inSoftFlowCtrl", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
strcpy (port_info->insoftflowctrl, TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("outSoftFlowCtrl", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
strcpy (port_info->outsoftflowctrl, TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("inHardFlowCtrl", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
strcpy (port_info->inhardflowctrl, TrimWord (node_value));
delete [] node_value;
node_value = GetNodeContent ("outHardFlowCtrl", buffer, NULL);
if (!node_value)
{
printf ("GetPortConfig: get node value error\n");
delete port_info;
return -1;
}
strcpy (port_info->outhardflowctrl, TrimWord (node_value));
delete [] node_value;
AddPortInfo (port_info);
return 0;
}
int ReadConfigStr (char *str_buf)
{
char *p, *q;
int res = 0;
if (!str_buf)
return -1;
p = str_buf;
while (p)
{
p = GetNodeContent ("SerialPort", p, &q);
if (p)
{
res = GetPortConfig (p);
if (res < 0)
{
delete [] p;
break;
}
delete [] p;
p = q;
}
else
break;
}
return res;
}
int ReadConfigFile (char *file_name)
{
FILE *fp;
char *file_text;
int res, len;
if (!file_name)
{
printf ("ReadConfigFile: input error\n");
return -1;
}
fp = fopen (file_name, "r");
if (fp == NULL)
{
printf ("ReadConfigFile: file open error\n");
return -1;
}
res = fseek (fp, 0, SEEK_END);
if (res < 0)
{
printf ("ReadConfigFile: fseek error\n");
fclose (fp);
return -1;
}
len = ftell (fp);
if (len < 0)
{
printf ("ReadConfigFile: ftell error\n");
fclose (fp);
return -1;
}
rewind (fp);
file_text = new char[len + 1];
memset (file_text, 0, len + 1);
res = fread (file_text, len, 1, fp);
if (res < 0)
{
printf ("ReadConfigFile: fread error\n");
fclose (fp);
return -1;
}
fclose (fp);
res = ReadConfigStr (file_text);
delete [] file_text;
return res;
}
struct serial_port_info *GetPortInfo (int serial_no)
{
struct serial_port_info *p;
p = g_serial_port_mgr.head;
while (p)
{
if (p->serialno == serial_no)
break;
p = p->next;
}
return p;
}
/**
选择一个最接近输入数值的波特率
**/
int GetBaudRate (int num)
{
int i, res = B0, n1, n2;
if (num <= 0)
return B0;
if (num < g_baud_map[1].rate_num)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -