--- ser2net 源码分析: 将TCP数据转发到serial
- static char *config_file = "/etc/ser2net.conf";
- --- readconfig.c: 用fgets读取配置文件的内容并进行解析(handle_config_line)
- --- selector.c: 利用回调函数
- typedef void (*t_sighup_handler)(void);
- static t_sighup_handler user_sighup_handler = NULL;
- /* The main loop for the program. This will select on the various sets, then scan for any available I/O to process.
- It also monitors the time and call the timeout handlers periodically. 被调用端传递数据, 循环判断有无数据*/
- void sel_select_loop(selector_t *sel)
- {
- for (;;) {
- memcpy(&tmp_read_set, &sel->read_set, sizeof(tmp_read_set));
- memcpy(&tmp_write_set, &sel->write_set, sizeof(tmp_write_set));
- memcpy(&tmp_except_set, &sel->except_set, sizeof(tmp_except_set));
- err = select(sel->maxfd+1, &tmp_read_set, &tmp_write_set, &tmp_except_set, to_time);
- if (err == 0) {
- /* A timeout occurred. */
- } else if (err < 0) {
- /* An error occurred. */
- } else {
- /* We got some I/O. */
- for (i=0; i<=sel->maxfd; i++) {
- if (FD_ISSET(i, &tmp_read_set)) {
- if (sel->fds[i].handle_read == NULL) {
- /* Somehow we don't have a handler for this. Just shut it down. */
- sel_set_fd_read_handler(sel, i, SEL_FD_HANDLER_DISABLED);
- } else {
- sel->fds[i].handle_read(i, sel->fds[i].data);
- }
- }
- if (FD_ISSET(i, &tmp_write_set)) {
- if (sel->fds[i].handle_write == NULL) {
- /* Somehow we don't have a handler for this. Just shut it down. */
- sel_set_fd_write_handler(sel, i, SEL_FD_HANDLER_DISABLED);
- } else {
- sel->fds[i].handle_write(i, sel->fds[i].data);
- }
- }
- if (FD_ISSET(i, &tmp_except_set)) {
- if (sel->fds[i].handle_except == NULL) {
- /* Somehow we don't have a handler for this. Just shut it down. */
- sel_set_fd_except_handler(sel, i, SEL_FD_HANDLER_DISABLED);
- } else {
- sel->fds[i].handle_except(i, sel->fds[i].data);
- }
- }
- }
- }
- if (got_sighup) {
- got_sighup = 0;
- if (user_sighup_handler != NULL) {
- user_sighup_handler();
- }
- }
- } //for (;;)
- }
- //设置原型
- void set_sighup_handler(t_sighup_handler handler)
- {
- user_sighup_handler = handler;
- }
- //调用端处理数据
- void reread_config(void) {}
- set_sighup_handler(reread_config);
- sel_select_loop(ser2net_sel);
--- dataxfer.c == 串口部分的操作
//--- openwrt中的ipk应用
opkg install /tmp/ser2net_2.7-2_brcm63xx.ipk
看到安装成功的信息后启动ser2net
Ser2net –c /etc/ser2net.conf
设置随机启动
命令vi /etc/rc.local 打开它,i进入编辑,在exit0的上一行添加一条ser2net,esc退出编辑,wq保存
命令vi /etc/init.d/ser2net,,i进入编辑
#!/bin/sh /etc/rc.common
# /init.d/my-ser2net
START=99
start() {
Ser2net –c /etc/ser2net.conf
}
Esc退出编辑,wq 保存。
命令reboot重启路由器,ok
vi /etc/init.d/ser2net,,a进入编辑
#!/bin/sh /etc/rc.common
# /init.d/my-plugin
START=99
start() {
ser2net
}
Esc退出编辑,ZZ 保存。