搜索
bottom↓
回复: 15

有做过TCP/IP协议的吗?进来帮忙看看

[复制链接]

出0入0汤圆

发表于 2009-6-25 11:30:50 | 显示全部楼层 |阅读模式
单片机以50ms的间隔向网络发送128字节数据包,现在电脑会收到256或512字节数据包,也就是两个或多个数据包拼在一起了,这样实时性不好,不知有没有解决办法??

用EtherPeek监视网络数据发现单片机发过来的数据确实是每包128字节的,但VB编写的软件收到数据包就是有拼包的现象,怀疑是电脑网卡驱动接收到数据后没有马上发给应用程序。

有没有哪位遇到到类似的问题??


VB编写的程序收到的数据 (原文件名:1.JPG)


EtherPeek检测到的数据 (原文件名:2.JPG)


push标志已经置位 (原文件名:3.JPG)

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2009-6-25 11:46:55 | 显示全部楼层
对于TCP的调用者来说,TCP是基于字节流的,不是严格基于包的,所以在TCP之上的程序,最保险的方法是处理字节流。这是由TCP协议本身决定的。

出0入0汤圆

 楼主| 发表于 2009-6-25 11:55:04 | 显示全部楼层
按字节流处理有一个问题就是反应的信息会滞后机器动作,目前这个应用是用来监视机器状态。

出0入0汤圆

发表于 2009-6-25 12:06:09 | 显示全部楼层
是PC机上的协议栈弄的吧。。。。。

出0入0汤圆

发表于 2009-6-25 13:39:36 | 显示全部楼层
哈哈,这是所谓的报文粘连的问题。

这种解决办法,好像就是要禁止掉NAGGLE算法的。通常为了网络的效率,协议栈会对小报文进行优化处理的。

出0入0汤圆

 楼主| 发表于 2009-6-25 14:02:34 | 显示全部楼层
oldtom:  能讲详细点吗?是不是单片机发送的数据每要增加什么标志,不让PC Delayed Ack,或在PC机禁用NAGGLE算法???


看到解决问题的希望了,呵呵

出0入0汤圆

 楼主| 发表于 2009-6-25 14:26:04 | 显示全部楼层
1 - It might have side effects, like slowing down your download speed or affecting your performance on other softwares. (In my case, it didn't change anything)
2 - If you're not sure about what you're doing, just don't do it. I don't want to be responsible because you crashed your computer in some way because you wasted your registry.
2.1 - Seriously, if you don't know what you're doing, don't do it. It will be fixed in one or two weeks anyway. (and the official fix won't have any impact on other softwares)
3 - If you don't have the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSMQ\ directory, you can download and apply this file to your registry.
4 - Windows Vista users might want to check this post too.

Source - Elitistjerks.com

"1 - TcpAckFrequency - NOTE if you are running Windows Vista this setting may not have any effect - a hotfix is needed which i'm tracking down. This works fine under Windows XP

Type "regedit" in windows "run.." dialog to bring up registry menu

Then find:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\

There will be multiple NIC interfaces listed in there, find the one you use to connect to the internet, there will be several interfaces listed (they have long names like {7DBA6DCA-FFE8-4002-A28F-4D2B57AE8383}. Click each one, the right one will have lots of settings in it and you will see your machines IP address listed there somewhere. Right-click in the right hand pane and add a new DWORD value, name it TcpAckFrequency, then right click the entry and click Modify and assign a value of 1.

You can change it back to 2 (default) at a later stage if it affects your other TCP application performance. it tells windows how many TCP packets to wait before sending ACK. if the value is 1, windows will send ACK every time it receives a TCP package.

2 - TCPNoDelay
This one is pretty simple (Discussed here)

Type "regedit" in windows "run.." dialog to bring up registry menu

Then find:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSMQ\Parameters

Right-click in the right hand pane and add a new DWORD value, name it TCPNoDelay, then right click the entry and click Modify and assign a value of 1.

Click Ok and close the registry editor, then reboot your PC."

出0入0汤圆

 楼主| 发表于 2009-6-25 14:52:34 | 显示全部楼层
按上面修改后有效果,但还是偶尔会出现粘连的数据包,基本能达到要求吧。








(原文件名:4.JPG)

出0入0汤圆

发表于 2009-6-25 16:13:25 | 显示全部楼层
其实解决问题,我觉得没有必要这么复杂。
关键是你的要求,你难道真的要求到50ms吗?


有一种思路,就是从你的上位机软件来解决。
报文粘连不是一个坏事,禁用掉NAGGLE算法的话,对网络并不好,一个网络中小报文过多,会造成网络风暴的,反而引起实时性的问题。

另外:顺问下,你的MCU是什么片子?你的MCU的tcp/ip是跑的哪个协议栈?

出0入0汤圆

发表于 2009-6-25 16:17:39 | 显示全部楼层
其实,对windows的定时器,sleep等最低的分辨率是55ms左右。所以,你的这个实时性,我觉得有值得商榷的地方。

我不太懂VB。
对于解决报文粘连的问题,可以有这样一个思路,专门搞一个线程,一直在循环接收数据,然后,按照报文的格式来拆分处理。这样的实时性还好一点。

出1070入962汤圆

发表于 2009-6-25 16:35:18 | 显示全部楼层
55ms的实时性没有实际意义,在异构网络中数据包的生存周期就大于55ms。楼主实验一下ping命令,看到返回里边有个TTL数值,就是数据包生存期,远大于55ms。

C:\Documents and Settings\user>ping www.google.com

Pinging www.l.google.com [64.233.189.147] with 32 bytes of data:

Reply from 64.233.189.147: bytes=32 time=63ms TTL=242
Reply from 64.233.189.147: bytes=32 time=63ms TTL=242
Reply from 64.233.189.147: bytes=32 time=65ms TTL=242
Reply from 64.233.189.147: bytes=32 time=60ms TTL=242

Ping statistics for 64.233.189.147:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 60ms, Maximum = 65ms, Average = 62ms


随便一个ping,时间就超过了55ms

出0入0汤圆

发表于 2009-6-25 17:43:19 | 显示全部楼层
用EtherPeek监视网络数据发现单片机发过来的数据确实是每包128字节的
-----------------------------------
好像说NAGGLE算法是用在发送时的吧?上面已经说了发送过来的确是128字节,好和这个算法就没有用了吧。。。。

出0入0汤圆

 楼主| 发表于 2009-6-25 19:41:14 | 显示全部楼层
我用的是ARM9,协议栈用的是lwip。

这个系统是个机器的监视系统,是一个独立的网络,不与internet直连的。所以产生小数据包是没关系的。

经过上面的修改主要是让PC机收到数据马上应答。


机器转速是1500rpm,所以最好40ms就能接收到一个数据包。

出0入0汤圆

发表于 2009-6-25 20:55:45 | 显示全部楼层
在listen之前禁止Nagle算法

出0入0汤圆

发表于 2009-6-25 21:41:34 | 显示全部楼层
我们在控制上使用UDP,不用TCP,正确性问题在应用层处理,UDP反应快

出0入0汤圆

发表于 2009-6-25 22:41:44 | 显示全部楼层
这种情况下,我倒是建议用串口,可能速度更快。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-5-18 09:20

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表