COM端口通信中CRC中缺少字节

时间:2017-12-06 18:10:55

标签: c# port crc

我通过COM端口与分配器进行通信。

发送ACK。如果2字节CRC不同,则使用0x11 0x31进行响应,否则使用0x31 0x10进行响应。到现在为止还挺好。但是如果缺少2字节CRC中的字节会怎样。那我该怎么办?它也包含两个字节。

例如,预期CRC为DLE-STX-DATA-DLE-ETX-CRC,但它返回NAK(其中0x10已经尝试返回DLE-STX-DATA-DLE-ETX-CRC1-CRC2的一部分,即使我没有发送NAK!)

如何在CRC本身中丢失字节?

如何恢复?

预期行为

  • 设备:DLE-STX-DATA-DLE-ETX-CRC1-CRC2
  • CRC不匹配
  • me:DLE-STX-DATA-DLE-ETX-(missing byte)-CRC2-DLE
  • 设备:NAK

实际行为

  • 设备:STX-DATA-DLE-ETX-CRC1-CRC2
  • CRC不匹配
  • me:"Process finished with exit code 0"
  • 设备:import socket from struct import * import datetime import pcapy import sys def main(argv): devices = pcapy.findalldevs() print (devices) for d in devices: print "Available devices are ", d #Device to be sniffed input_dev = raw_input("Enter deice name to sniff") print "Following device would be printed", input_dev #open #device # Arguments here are: # device # snaplen (maximum number of bytes to capture _per_packet_) # promiscious mode (1 for true) # timeout (in milliseconds) cap = pcapy.open_live(input_dev,65536,1,0) #Interating all packets captured in to a variable while(1): (header, packet) = cap.next() parse_packet(packet) #Convert a string of 6 characters of ethernet address into a dash separated hex string def eth_addr(a): b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]), ord(a[1]), ord(a[2]), ord(a[3]), ord(a[4]), ord(a[5])) # Parsing captured packets def parse_packet(packet): eth_length = 14 # Get the protocol used from the captured packet eth_header = packet[:eth_length] eth = unpack('!6s6sH', eth_header) eth_protocol = socket.ntohs(eth[2]) print 'Destination MAC : ' + eth_addr(packet[0:6])+'Source MAC: ' + eth_addr(packet[0:12]) + 'Protocol: ' +str(eth_protocol) # Parse the IP packet # if eth_protocol == 8: #Parse IP header #take first 20 characters for the ip header ip_header = packet[eth_length:20+eth_length] #Now unpack them iph = unpack('!BBHHHBBH4s4s', ip_header) version_ihl = iph[0] version = version_ihl >> 4 ihl = version_ihl & oxf # need to know what is the oxf iph_length = ihl * 4 ttl = iph[5] print "ttl value is = ", str(ttl) protocol = ip[6] print "protocol = ", str(protocol) s_addr = socket.inet_ntoa(iph[8]); print "source address = ", str(s_addr) d_address = socket.inet_ntoa(iph[9]) print "Destination address = ", str(d_address) #Parsing TCP header if protocol == 6: #if protocol = 6, it would be TCP t = iph_length + eth_length tcp_heder = packet #Unpack the packet tcph = unpack('!HHLLBBHHH', tcp_heder) source_port = tcph[0] print "Source port =", source_port dst_port = tcph[1] print "Dst port =", dst_port ack_no = tcph[3] print "ack_No = ", ack_no doff_deserverved = tcph[4] print "doff_deserved = ", doff_deserverved tcp_length = doff_deserverved >> 4 h_size = eth_length + iph_length + tcp_length * 4 data_size = len(packet) - h_size #get the data from the packet data = packet[data_size:] print "Data is = ", data elif protocol == 1: u = iph_length + eth_length icmp_length = 4 icmp_header= packet[u:u+4] #Now unpack them icmp = unpack('!BBH' , icmp_header) icmp_type = icmp[0] print "Type is = ", icmp code = icmp[1] print "Code is = ", code checkSum = icmp[3] print "Check sum is ", checkSum h_size = eth_length + iph_length + icmp_length data_sze = len(packet)- h_size print "Data size = ", data_size #Get Data from the packet data2 = packet[h_size:] print "Data is = ", data2 #Parsing UDP packet elif protocol == 17: u = iph_length + eth_length udph_leungth = 8 udp_header = packet[u:u+8] #Now unpack them udp_packet = unpack('!HHHH', udp_header) s_port = udp_packet[0] print "Source Port = ", s_port d_port = udp_packet[1] print "Udp Packet = ", d_port lenth_of_packet = udp_packet[2] print "Length of packet = ", lenth_of_packet check_sum = udp_packet[3] print "check sum is ", check_sum h_size_of_packet = eth_length + iph_length + udph_leungth actaul_size = len(packet) - h_size_of_packet #Retrieving data packet from the size data = packet[h_size_of_packet] print "Actual Data = ", data #IF there any other some protocol else: print "Protol other than TCP/UDP/ICMP" main(sys.argv)

1 个答案:

答案 0 :(得分:0)

事实证明这是无法解决的。即使是官方的富士通实用程序也无法处理这种情况。即使处理完COM端口变得无法使用,也需要重新启动计算机。您只能从此错误中优雅地失败。