我有一個簡單的 MCU 網絡,它只支持 ARP、廣播和單播 UDP/IP 協議,我直接連接到 MCU,沒有 PC 網絡(點對點)。在 C# 中,我有兩個 UDP 套接字 - 發送方和偵聽器。偵聽器綁定到端點(偵聽端口 60001)。但是我的程序只有在運行 Wireshark 時才能運行。如果沒有 Wireshark,它只能發送廣播數據包,但不能接收。MCU 實現 ARP 協議(我也在 Windows 中嘗試過靜態 IP。命令 arp -s)。我嘗試關閉 Windows 10 防火墻和防病毒軟件,以管理員身份運行程序,什么也沒有。僅當我運行 Wireshark 時,我的 C# 程序才會接收數據包。IP 標頭校驗和是正確的 - 我在 Wireshark 中啟用了檢查。udp checksum = 0(PC也不計算校驗和)C#代碼:public void UdpConnect() { udpSender = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); udpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); udpListener.Bind(new IPEndPoint(IPAddress.Any, 60001)); // MCU send data to dstIP = PC_IP and dstPort = 60001}int Send() { byte[] dgram = tx_buf.ToArray(); int n = udpSender.SendTo(dgram, SocketFlags.DontRoute, new IPEndPoint(IPAddress.Parse("192.168.0.200"), 60000)); // PC send data to MCU IP 192.168.0.200 and dstPort = 60000 Debug.WriteLine("Send " + n + " bytes"); return n;}byte[] Receive(int timeout_ms = 3000) { byte[] data = new byte[1518]; int byteCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); do { if (udpListener.Available != 0) { Debug.WriteLine("Available: " + udpListener.Available); byteCount = udpListener.Receive(data, data.Length, SocketFlags.None); Debug.WriteLine("Received UDP packet length: " + byteCount); } else Thread.Sleep(100); } while (byteCount == 0 && sw.ElapsedMilliseconds < timeout_ms); return byteCount == 0 ? null : data.Take(byteCount).ToArray();}byte[] SendReceive(int timeout_ms = 3000, int attempts = 3) { byte[] result = null; for (int i = 0; i < attempts; i++) { Send(); result = Receive(timeout_ms); if (result != null) break; else Debug.WriteLine("Attempt " + (i + 1) + " failed"); }
1 回答

紅顏莎娜
TA貢獻1842條經驗 獲得超13個贊
問題在于簡短的 ARP 和 UDP 數據包。當我將設備連接到本地網絡時,我理解了這一點(通常以太網控制器自動添加填充,但不是 XMOS)。
以太網數據包的最小大小為 64 字節(標頭 + 數據 + crc32)。我發送了沒有填充的 ARP,我也發送了短廣播 UDP。
- 1 回答
- 0 關注
- 310 瀏覽
添加回答
舉報
0/150
提交
取消