資源簡(jiǎn)介
串口轉(zhuǎn)TCP組件 (Ubuntu、openwrt實(shí)測(cè)可用),采用C語(yǔ)言編寫,單文件,可直接編譯。
同時(shí)包含python的服務(wù)器端。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include? ?//標(biāo)準(zhǔn)輸入輸出定義
#include? //標(biāo)準(zhǔn)庫(kù)函數(shù)定義
#include? //UNIX標(biāo)準(zhǔn)函數(shù)定義
#include??//基本系統(tǒng)數(shù)據(jù)類型
#include???//獲取一些文件相關(guān)的信息
#include? ?//文件控制定義
#include?
#include???//錯(cuò)誤號(hào)定義
#include???//標(biāo)準(zhǔn)輸入輸出定義
#include??//標(biāo)準(zhǔn)函數(shù)庫(kù)定義
#include??//Unix標(biāo)準(zhǔn)函數(shù)定義
#include?
#include?
#include????//文件控制定義
#include??//POSIX中斷控制定義
#include????//錯(cuò)誤號(hào)定義
#include?
#include?
//?#define?DEST_PORT?9999?//目標(biāo)地址端口號(hào)
//?#define?DEST_IP?“61.153.226.170“?/*目標(biāo)地址IP,這里設(shè)為本機(jī)*/
//?#define?DEST_IP?“101.6.65.194“?/*目標(biāo)地址IP,這里設(shè)為本機(jī)*/
//?#define?MAX_DATA?256?//接收到的數(shù)據(jù)最大程度
#define?serial_device?“/dev/ttyS1“
struct?thread_info{
int?sockfd;
int?fd;
char*?buff;
};
//打開串口
int?open_port(void)
{
int?fd;?//串口的標(biāo)識(shí)符
//O_NOCTTY用來(lái)告訴Linux這個(gè)程序不會(huì)成為“控制終端”
//O_NDELAY用來(lái)告訴Linux這個(gè)程序不關(guān)心DCD信號(hào)
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;??//?串口配置結(jié)構(gòu)體
tcgetattr(fd?&options);?//獲取當(dāng)前設(shè)置
bzero(&options?sizeof(options));
options.c_cflag?|=?B115200?|?CLOCAL?|?CREAD;?//?設(shè)置波特率,本地連接,接收使能
options.c_cflag?&=?~CSIZE; ?//屏蔽數(shù)據(jù)位
options.c_cflag?|=?CS8; ?//?數(shù)據(jù)位為?8?,CS7?for?7
options.c_cflag?&=?~CSTOPB; ?//?一位停止位,?兩位停止為?|=?CSTOPB
options.c_cflag?&=?~PARENB; ?//?無(wú)校驗(yàn)
?//options.c_cflag?|=?PARENB;?//有校驗(yàn)
//options.c_cflag?&=?~PARODD?//?偶校驗(yàn)
//options.c_cflag?|=??PARODD????//?奇校驗(yàn)
options.c_cc[VTIME]?=?6; ??//?等待時(shí)間,單位百毫秒?(讀)。后有詳細(xì)說(shuō)明
options.c_cc[VMIN]?=?0; ??//?最小字節(jié)數(shù)?(讀)。后有詳細(xì)說(shuō)明
tcflush(fd?TCIOFLUSH); ??//?TCIFLUSH刷清輸入隊(duì)列。
tcsetattr(fd?TCSANOW?&options);?//?TCSANOW立即生效;
}
int?serialport()
{
int?fd;
//打開串口
if?((fd?=?open_port())?0)
{
perror(“open_port?error“);
return?0;
}
//設(shè)置波特率和校驗(yàn)位
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發(fā)送數(shù)據(jù)
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?接收數(shù)據(jù)
sleep(1);
nread?=?recv(sockfd?buff?1024?0);?//將接收數(shù)據(jù)打入buf,參數(shù)分別是句柄,儲(chǔ)存處,最大長(zhǎng)度,其他信息(設(shè)為0即可)。
//?printf(“TCP?nLen?%dn\n“?nread);
//寫回串口
write(fd?buff?nread);
}
}
void?TCP2Ser_thread(void?*Tinfo){
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????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
評(píng)論
共有 條評(píng)論