資源簡介
NC600串口調試工具,NC600:tcpserver,tcpclient指令,源代碼等
代碼片段和文件信息
/*?
?*?this?is?a?sample?when?ntd?acts?as?tcp?client
?*/
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?“nser.h“
void?set_param(int?fd);
void?set_report(int?fd);
void?do_communicate(int?fd);
void?send_time_break(fd);
void?send_close(fd);
void?err_exit();
?
#define?LOCAL_IPADDR “192.168.0.91“ //?sckcfg?N?-rip?LOCAL_IPADDR
#define?LOCAL_PORT 5000 //?sckcfg?N?-rt?LOCAL_PORT
int?main(int?argc?char?**?argv)
{
int socket_fd?listen_fd;
struct?sockaddr_in local_ip;
int nbio?=?1;
WSADATA wsadata;
WSAStartup(0x101?&wsadata);
memset(&local_ip?0?sizeof(local_ip));
local_ip.sin_family?=?AF_INET;
local_ip.sin_port?=?htons((unsigned?short)LOCAL_PORT);
local_ip.sin_addr.s_addr?=?inet_addr(LOCAL_IPADDR);
if?(local_ip.sin_addr.s_addr?==?INADDR_NONE)?
{
printf(“inet_addr?error\n“);
err_exit();
}
if?((listen_fd?=?socket(AF_INET?SOCK_STREAM?0))?0)
{
printf(“tcp?socket?error\n“);
err_exit();
}
if?(bind(listen_fd?(struct?sockaddr?*)&local_ip?sizeof(local_ip))?0)
{
printf(“tcp?bind?error\n“);
err_exit();
}
listen(listen_fd?1);
socket_fd?=?accept(listen_fd?NULL?NULL);
nbio?=?1;
ioctlsocket(socket_fdFIONBIO&nbio);
set_param(socket_fd);
set_report(socket_fd);
do_communicate(socket_fd);
WSACleanup();
return?0;
}
void?set_param(int?fd)
{
unsigned?char mask;
unsigned?char mode;
unsigned?char flow;
unsigned?char ctrl;
unsigned?long baud;
unsigned?char buf[32];
mode?=?CS8?|?STOP1?|?PAR_NONE; //?8?bits?data?1?bit?stop?no?parity
flow?=?FLOW_NONE; //?no?flowctrl
ctrl?=?C_DTR?|?C_RTS; //?DTR?on?RTS?on
baud?=?9600; //?baud?is?9600bps
mask?=?MASK_MODE?|?MASK_FLOW?|?MASK_CTRL?|?MASK_BAUD;
buf[0]?=?0xff;
buf[1]?=?SET_SERIAL;
buf[2]?=?mask;
buf[3]?=?mode;
buf[4]?=?flow;
buf[5]?=?ctrl;
buf[6]?=?baud?>>?24;
buf[7]?=?baud?>>?16;
buf[8]?=?baud?>>?8;
buf[9]?=?baud;
send(fd?(char?*)buf?10?0);
}
void?send_break(int?fd?int?set_break)
{
unsigned?char mask;
unsigned?char mode;
unsigned?char flow;
unsigned?char ctrl;
unsigned?long baud;
unsigned?char buf[10];
if?(set_break)
mode?=?SET_BREAK; //?8?bits?data?1?bit?stop?no?parity
else
mode?=?CLEAR_BREAK;
flow?=?FLOW_NONE; //?no?flowctrl
ctrl?=?C_DTR??|?C_RTS; //?DTR?on?RTS?on
baud?=?9600; //?baud?is?9600bps
mask?=?MASK_BRK;
buf[0]?=?0xff;
buf[1]?=?SET_SERIAL;
buf[2]?=?mask;
buf[3]?=?mode;
buf[4]?=?flow;
buf[5]?=?ctrl;
buf[6]?=?baud?>>?24;
buf[7]?=?baud?>>?16;
buf[8]?=?baud?>>?8;
buf[9]?=?baud;
send(fd?(char?*)buf?10?0);
}
void?set_report(int?fd)
{
unsigned?char type;
unsigned?char buf[3];
type?=?REPORT_IF_CHANGED; //?after?do?this??ntd?will?report?status?w
- 上一篇:STM32F4+USBHID雙向通訊
- 下一篇:Qt學生成績管理系統源碼可運行
評論
共有 條評論