Traceback (most recent call last):
File ".\SP2WS.py", line 5, in <module>
import serial
ModuleNotFoundError: No module named 'serial'
pip insatll pyserial
Traceback (most recent call last):
File ".\SP2WS.py", line 1, in <module>
import win32pipe, win32file
ModuleNotFoundError: No module named 'win32pipe'
pip install pypiwin32
struct pcap_file_header
{
uint32_t magic_number; /* magic number */
uint16_t version_major; /* major version number */
uint16_t version_minor; /* minor version number */
int32_t thiszone; /* GMT to local correction */
uint32_t sigfigs; /* accuracy of timestamps */
uint32_t snaplen; /* max length of captured packets, in octets */
uint32_t linktype; /* data link type */
}
字段 | 大小(Byte) | 含义 |
---|---|---|
magic_number | 4 | 用来标示文件的开始(值为: 0xA1, 0xB2, 0xC3, 0xD4) |
version_major | 2 | 当前文件主要的版本号(值为:0x00, 0x02) |
version_minor | 2 | 当前文件次要的版本号(值为:0x00, 0x04) |
thiszone | 4 | 当地的标准时间(值为:0) |
sigfigs | 4 | 时间戳的精度(值为:0) |
snaplen | 4 | 捕获数据包的最大长度(值为:0) |
linktype | 4 | 链路类型(值为:1) 0: BSD loopback devices, except for later OpenBSD 1: Ethernet, and Linux loopback devices 6: 802.5 Token Ring 7: ARCnet 8: SLIP 9: PPP 10: FDDI 100: LLC/SNAP-encapsulated ATM 101: "raw IP", with no link 102: BSD/OS SLIP 103: BSD/OS PPP 104: Cisco HDLC 105: 802.11 108: later OpenBSD loopback devices (with the AF_value in network byte order) 113: special Linux "cooked" capture 114: LocalTalk |
struct pcaprec_hdr {
uint32_t ts_sec; /* timestamp seconds */
uint32_t ts_usec; /* timestamp microseconds (nsecs for PCAP_NSEC_MAGIC) */
uint32_t incl_len; /* number of octets of packet saved in file*/
uint32_t orig_len; /* actual length of packet */
};
字段 | 大小(Byte) | 含义 |
---|---|---|
ts_sec | 4 | 时间戳高位,精确到seconds(值是自从January 1, 1970 00:00:00 GMT以来的秒数来记) |
ts_usec | 4 | 时间戳低位,精确到microseconds(数据包被捕获时候的微秒(microseconds)数,是自ts-sec的偏移量) |
incl_len | 4 | 当前数据区的长度,即抓取到的数据帧长度,由此可以得到下一个数据帧的位置。 |
orig_len | 4 | 网络中实际数据帧的长度,一般不大于caplen,多数情况下和Caplen数值相等。 |
#define PIPE_CAP_HDR_SIZE 3
#define PIPE_CAP_TAIL_SIZE 3
static void pipe_cap_dump(char *buf, int len)
{
char pipe_hdr[PIPE_CAP_HDR_SIZE] = {114, 116, 116};
char pipe_end[PIPE_CAP_TAIL_SIZE] = {101, 110, 100};
char *pipe_data = (char *)malloc(len + PIPE_CAP_HDR_SIZE + PIPE_CAP_TAIL_SIZE);
if(pipe_data != NULL)
{
memcpy(pipe_data, pipe_hdr, PIPE_CAP_HDR_SIZE);
memcpy(&pipe_data[PIPE_CAP_HDR_SIZE], buf, len);
memcpy(&pipe_data[PIPE_CAP_HDR_SIZE + len], pipe_end, PIPE_CAP_TAIL_SIZE);
uart_send(pipe_data, len + PIPE_CAP_HDR_SIZE + PIPE_CAP_TAIL_SIZE);
free(pipe_data);
pipe_data = NULL;
}
}
class sp2ws_serial():
def __init__(self, port, baudrate):
self.port = port
self.baudrate = baudrate
def open(self):
self.ser = serial.Serial(self.port, self.baudrate, timeout = 0.5)
def close(self):
self.ser.close()
def read(self, length):
return self.ser.read(length)
def write(self, buff):
self.ser.write(buff)
class sp2ws_pipe():
def __init__(self, pipe_name):
self.magic_num = 0xa1b2c3d4
self.major_ver = 0x02
self.minor_ver = 0x04
self.link_type = 1
self.pipe_name = pipe_name
def create_pipe(self):
self.pipe = win32pipe.CreateNamedPipe(
self.pipe_name,
win32pipe.PIPE_ACCESS_OUTBOUND,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,
300,
None)
def connect_pipe(self):
win32pipe.ConnectNamedPipe(self.pipe, None)
# 文件头部
global_header = struct.pack('IHHiIII',
self.magic_num, # magic number 4
self.major_ver, # major version number 2
self.minor_ver, # minor version number 2
0, # GMT to local correction 4
0, # accuracy of timestamps 4
0, # max length of captured packets, in octets 4
self.link_type # data link type 4
)
win32file.WriteFile(self.pipe, global_header)
def write_pipe(self, packet):
packet_len = len(packet)
if packet_len <= 0:
return
# 报文头部
packet_header = struct.pack('IIII',
int(time.time()), # timestamp seconds
datetime.datetime.now().microsecond, # timestamp microseconds
packet_len, # number of octets of packet
packet_len # actual length of packet
)
win32file.WriteFile(self.pipe, packet_header)
win32file.WriteFile(self.pipe, packet)
if __name__ == "__main__":
if len(sys.argv) < 3:
print("SP2WS.py com[port_num] wifi_master [ssid] [pwd]")
print("SP2WS.py com[port_num] wifi_slave")
print(r'\\.\pipe\wifi_master')
print(r'\\.\pipe\wifi_slave')
port = sys.argv[1].upper()
if sys.argv[2].find("wifi_master") > -1:
ssid = sys.argv[3]
password = sys.argv[4]
wifi_connect = r'wifi join ' + ssid + ' ' + password
pipe_name = r'\\.\pipe' + '\\' + sys.argv[2]
# 创建串口对象。
sp2ws_serial = sp2ws_serial(port, 115200)
sp2ws_serial.open()
# 创建pipe对象
sp2ws_pipe = sp2ws_pipe(pipe_name)
sp2ws_pipe.create_pipe()
print("start connect pipi...")
# 连接pipe
sp2ws_pipe.connect_pipe()
print("pipe connect success!!!")
raw_buf = bytearray()
start = 0
init = 0
if sys.argv[2].find("wifi_master") > -1:
sp2ws_serial.write(bytes(wifi_connect + "\r\n", encoding='utf-8'))
sp2ws_serial.write(bytes("pipe_start\r\n", encoding='utf-8'))
while True:
# 读取串口数据
raw = sp2ws_serial.read(1024)
raw_len = len(raw)
if raw_len > 0:
raw_buf = raw_buf + raw
# 抓取有效网络报文
while True:
raw_len = len(raw_buf)
# find packet header
for index in range(raw_len):
if (index + 2) < (raw_len - 1):
if raw_buf[index] == 114 and raw_buf[index + 1] == 116 and raw_buf[index + 2] == 116:
start = index + 3
break
start = 0
else:
start = 0
break
if start == 0:
break
# find packet tail
for index in range(start, raw_len):
if (index + 2) < (raw_len - 1):
if raw_buf[index] == 101 and raw_buf[index + 1] == 110 and raw_buf[index + 2] == 100:
end = index
break
end = 0
else:
end = 0
break
if end == 0:
break
frame = raw_buf[start : end]
# 将网络报文发送到pipe中
sp2ws_pipe.write_pipe(frame)
end += 3
raw_buf = raw_buf[end : ]
本文分享自 Rice 嵌入式开发技术分享 微信公众号,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文参与 腾讯云自媒体同步曝光计划 ,欢迎热爱写作的你一起参与!