资源简介
串口转TCP组件 (Ubuntu、openwrt实测可用),采用C语言编写,单文件,可直接编译。
同时包含python的服务器端。

代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include //标准输入输出定义
#include //标准库函数定义
#include //UNIX标准函数定义
#include //基本系统数据类型
#include //获取一些文件相关的信息
#include //文件控制定义
#include
#include //错误号定义
#include //标准输入输出定义
#include //标准函数库定义
#include //Unix标准函数定义
#include
#include
#include //文件控制定义
#include //POSIX中断控制定义
#include //错误号定义
#include
#include
// #define DEST_PORT 9999 //目标地址端口号
// #define DEST_IP “61.153.226.170“ /*目标地址IP,这里设为本机*/
// #define DEST_IP “101.6.65.194“ /*目标地址IP,这里设为本机*/
// #define MAX_DATA 256 //接收到的数据最大程度
#define serial_device “/dev/ttyS1“
struct thread_info{
int sockfd;
int fd;
char* buff;
};
//打开串口
int open_port(void)
{
int fd; //串口的标识符
//O_NOCTTY用来告诉Linux这个程序不会成为“控制终端”
//O_NDELAY用来告诉Linux这个程序不关心DCD信号
fd = open(serial_device O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
//不能打开串口
perror(“open_port: Unable to open /dev/ttyS0 -“);
return (fd);
}
else
{
fcntl(fd F_SETFL 0);
printf(“open ttys1 .....\n“);
return (fd);
}
}
void set_speed_and_parity(int fd int speed)
{
struct termios options; // 串口配置结构体
tcgetattr(fd &options); //获取当前设置
bzero(&options sizeof(options));
options.c_cflag |= B115200 | CLOCAL | CREAD; // 设置波特率,本地连接,接收使能
options.c_cflag &= ~CSIZE; //屏蔽数据位
options.c_cflag |= CS8; // 数据位为 8 ,CS7 for 7
options.c_cflag &= ~CSTOPB; // 一位停止位, 两位停止为 |= CSTOPB
options.c_cflag &= ~PARENB; // 无校验
//options.c_cflag |= PARENB; //有校验
//options.c_cflag &= ~PARODD // 偶校验
//options.c_cflag |= PARODD // 奇校验
options.c_cc[VTIME] = 6; // 等待时间,单位百毫秒 (读)。后有详细说明
options.c_cc[VMIN] = 0; // 最小字节数 (读)。后有详细说明
tcflush(fd TCIOFLUSH); // TCIFLUSH刷清输入队列。
tcsetattr(fd TCSANOW &options); // TCSANOW立即生效;
}
int serialport()
{
int fd;
//打开串口
if ((fd = open_port()) < 0)
{
perror(“open_port error“);
return 0;
}
//设置波特率和校验位
set_speed_and_parity(fd 115200);
return (fd);
}
void Ser2TCP(int sockfd int fd char *buff)
{
int nread;
while (1)
{
//读取串口
nread = read(fd buff 128);
if (nread > 0)
{
// printf(“nLen %dn\n“ nread);
buff[nread + 1] = 0;
// printf(“n%s\n“ buff);
// TCP发送数据
send(sockfd buff nread 0);
}
else
{
sleep(1);
}
}
}
void Ser2TCP_thread(void *Tinfo){
Ser2TCP(((struct thread_info *)Tinfo)->sockfd ((struct thread_info *)Tinfo)->fd((struct thread_info *)Tinfo)->buff);
}
void TCP2Ser(int sockfd int fd char *buff)
{
int nread;
while (1)
{
// TCP 接收数据
sleep(1);
nread = recv(sockfd buff 1024 0); //将接收数据打入buf,参数分别是句柄,储存处,最大长度,其他信息(设为0即可)。
// printf(“TCP nLen %dn\n“ nread);
//写回串口
write(fd buff nread);
}
}
void TCP2Ser_thread(void *Tinfo){
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1114 2018-07-26 08:33 README.md
文件 5417 2018-07-26 08:32 tcpclient.c
文件 495 2018-07-26 02:08 tcpclient.py
文件 721 2018-07-26 02:09 tcpserver.py
相关资源
- 基于MFC的TCP调试助手源码95706
- 串口通讯技术实现--两台pc机通过串口
- C++语言编写串口调试助手
- Quectel_EC200xEG912YEC600NEC600S系列_TCP(I
- GD32F103 在线串口Ymodem协议升级IAP
- qt 串口助手源码
- 使用QWT库实现接收串口数据,并根据
- Qt5串口通信-windows
- 串口实验(接收与发送)
- 程序案例 利用LabVIEW实现串口通讯
- STM32(神舟III号 串口1发送实验程序)
- STM32429的串口收发程序
- atmega128 串口通讯(RS485.c)
- arduino I2C设备扫描并串口返回地址(
- 基于STM32F407的W5500 tcpserver(官网例程
- 串口调试助手(测试STM32串口)
- N76E003串口收发
- openwrt深入学习笔记
- 基于CS的TCP文件传输程序设计
- TCP/IP客户端和服务器端源代码,好用
- modbus tcp/ip 简单通讯
- libnet发送udp和tcp包
- socket tcp应用
- TCP/IP与串口调试
- Tcp自定义命令调试工具
- Qt TCP聊天室demo
- LabwindowsCVI 串口编程及事例.docx
- TCP服务端和TCP客户端通讯
- vc_串口通讯
- VC++ 串口
评论
共有 条评论