Serial port 可靠通信的简单算法

Serial port 可靠通信的简单算法,serial-port,communication,Serial Port,Communication,因此,我过去曾在大型系统上工作过,比如iso堆栈会话层,类似的东西对于我所需要的东西来说太大了,但我确实对全局有一些了解。我现在拥有的是一个串行点对点通信链路,其中一些组件正在(经常)丢弃数据 因此,我将不得不编写自己的、可靠的传输系统,使用它进行传输。有人能给我指出基本算法的方向吗,或者甚至给我一个关于它们叫什么的线索吗?我试过谷歌,但最终得到的是关于遗传算法之类的研究生理论。我需要基础知识。e、 g.10-20行纯C.这个问题(IMO)有两个方面 首先,如果数据被丢弃,那么我会首先考虑解决硬

因此,我过去曾在大型系统上工作过,比如iso堆栈会话层,类似的东西对于我所需要的东西来说太大了,但我确实对全局有一些了解。我现在拥有的是一个串行点对点通信链路,其中一些组件正在(经常)丢弃数据

因此,我将不得不编写自己的、可靠的传输系统,使用它进行传输。有人能给我指出基本算法的方向吗,或者甚至给我一个关于它们叫什么的线索吗?我试过谷歌,但最终得到的是关于遗传算法之类的研究生理论。我需要基础知识。e、 g.10-20行纯C.

这个问题(IMO)有两个方面

首先,如果数据被丢弃,那么我会首先考虑解决硬件问题,否则你会有GIGO

至于通信协议,你的帖子建议使用一个相当简单的系统?您是想要验证数据(奇偶校验、sumcheck?)还是想要包含错误更正

如果只需要验证,我就可以使用RS232和CRC8 sumchecks运行可靠的系统-在这种情况下可能会有所帮助

(IMO)这个问题有两个方面

首先,如果数据被丢弃,那么我会首先考虑解决硬件问题,否则你会有GIGO

至于通信协议,你的帖子建议使用一个相当简单的系统?您是想要验证数据(奇偶校验、sumcheck?)还是想要包含错误更正


如果只需要验证,我就可以使用RS232和CRC8 sumchecks运行可靠的系统-在这种情况下可能会有所帮助

如果某些组件在串行点对点链接中丢失数据,那么代码中一定存在一些错误

首先,您应该确认物理层的通信没有问题


其次,您需要一些关于数据通信理论的知识,如ARQ(自动请求重传)

如果某些组件在串行点对点链接中丢失数据,那么您的代码中一定存在一些错误

首先,您应该确认物理层的通信没有问题


其次,您需要了解一些数据通信理论,如ARQ(自动请求重传)

在考虑您对前两个答案的回答后,需要进一步思考。。。这确实表明存在硬件问题,再聪明的代码也无法解决这个问题

我建议您在链路上安装示波器,这将有助于确定故障所在。特别是查看两侧(Tx、Rx)的波特率,以确保它们在规格范围内。。。自动波特率经常是个问题


但是,看看退学是否正常,或者是否可以与任何其他活动同步。

在考虑你对前两个答案的反应后,进一步思考。。。这确实表明存在硬件问题,再聪明的代码也无法解决这个问题

我建议您在链路上安装示波器,这将有助于确定故障所在。特别是查看两侧(Tx、Rx)的波特率,以确保它们在规格范围内。。。自动波特率经常是个问题


但是看看退学是否正常,或者是否可以与任何其他活动同步

/////////////////////////////////////////  XBee logging
void dataLog(int idx, int t, float f)
{
    ubyte stx[2] = { 0x10, 0x02 };
    ubyte etx[2] = { 0x10, 0x03 };

    nxtWriteRawHS(stx, 2, 1);
    wait1Msec(1);

    nxtWriteRawHS(idx, 2, 1);
    wait1Msec(1);

    nxtWriteRawHS(t, 2, 1);
    wait1Msec(1);

    nxtWriteRawHS(f, 4, 1);
    wait1Msec(1);

    nxtWriteRawHS(etx, 2, 1);
    wait1Msec(1);
}
接受方

void XBeeMonitorTask()
        {
            int[] lastTick = Enumerable.Repeat<int>(int.MaxValue, 10).ToArray();
            int[] wrapCounter = new int[10];


            while (!XBeeMonitorEnd)
            {
                if (XBee != null && XBee.BytesToRead >= expectedMessageSize)
                {
                    // read a data element, parse, add it to collection, see above for message format
                    if (XBee.BaseStream.Read(XBeeIncoming, 0, expectedMessageSize) != expectedMessageSize)
                        throw new InvalidProgramException();

                    //System.Diagnostics.Trace.WriteLine(BitConverter.ToString(XBeeIncoming, 0, expectedMessageSize));

                    if ((XBeeIncoming[0] != 0x10 && XBeeIncoming[1] != 0x02) ||  // dle stx
                        (XBeeIncoming[10] != 0x10 && XBeeIncoming[11] != 0x03))   // dle etx
                    {
                        System.Diagnostics.Trace.WriteLine("recover sync");
                        while (true)
                        {
                            int b = XBee.BaseStream.ReadByte();
                            if (b == 0x10)
                            {
                                int c = XBee.BaseStream.ReadByte();
                                if (c == 0x03)
                                    break;     // realigned (maybe)
                            }
                        }
                        continue;   // resume at loop start
                    }

                    UInt16 idx = BitConverter.ToUInt16(XBeeIncoming, 2);
                    UInt16 tick = BitConverter.ToUInt16(XBeeIncoming, 4);
                    Single val = BitConverter.ToSingle(XBeeIncoming, 6);

                    if (tick < lastTick[idx])
                        wrapCounter[idx]++;
                    lastTick[idx] = tick;

                    Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => DataAdd(idx, tick * wrapCounter[idx], val)));
                }
                Thread.Sleep(2);  // surely we can up with the NXT
            }
        }
void XBeeMonitorTask()
{
int[]lastTick=Enumerable.Repeat(int.MaxValue,10.ToArray();
int[]wrapCounter=新int[10];
而(!XBeeMonitorEnd)
{
if(XBee!=null&&XBee.BytesToRead>=expectedMessageSize)
{
//读取一个数据元素,解析,将其添加到集合中,请参见上面的消息格式
if(XBee.BaseStream.Read(XBeeIncoming,0,expectedMessageSize)!=expectedMessageSize)
抛出新的InvalidProgrameException();
//System.Diagnostics.Trace.WriteLine(BitConverter.ToString(XBeeIncoming,0,expectedMessageSize));
如果((XBeeIncoming[0]!=0x10&&XBeeIncoming[1]!=0x02)|//dle stx
(XBeeIncoming[10]!=0x10&&XBeeIncoming[11]!=0x03))//dle etx
{
System.Diagnostics.Trace.WriteLine(“恢复同步”);
while(true)
{
int b=XBee.BaseStream.ReadByte();
如果(b==0x10)
{
int c=XBee.BaseStream.ReadByte();
如果(c==0x03)
break;//重新对齐(可能)
}
}
continue;//在循环开始时继续
}
UInt16 idx=位转换器.ToUInt16(XBeeIncoming,2);
UInt16 tick=BitConverter.ToUInt16(XBeeIncoming,4);
Single val=位转换器.ToSingle(XBEE输入,6);
如果(勾选<最后勾选[idx])
wrapCounter[idx]++;
lastTick[idx]=勾号;
Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle,新操作(()=>DataAdd(idx,tick*wrapCounter[idx],val));
}
Thread.Sleep(2);//我们肯定能想出NXT
}
}

在发送端

/////////////////////////////////////////  XBee logging
void dataLog(int idx, int t, float f)
{
    ubyte stx[2] = { 0x10, 0x02 };
    ubyte etx[2] = { 0x10, 0x03 };

    nxtWriteRawHS(stx, 2, 1);
    wait1Msec(1);

    nxtWriteRawHS(idx, 2, 1);
    wait1Msec(1);

    nxtWriteRawHS(t, 2, 1);
    wait1Msec(1);

    nxtWriteRawHS(f, 4, 1);
    wait1Msec(1);

    nxtWriteRawHS(etx, 2, 1);
    wait1Msec(1);
}
接受方

void XBeeMonitorTask()
        {
            int[] lastTick = Enumerable.Repeat<int>(int.MaxValue, 10).ToArray();
            int[] wrapCounter = new int[10];


            while (!XBeeMonitorEnd)
            {
                if (XBee != null && XBee.BytesToRead >= expectedMessageSize)
                {
                    // read a data element, parse, add it to collection, see above for message format
                    if (XBee.BaseStream.Read(XBeeIncoming, 0, expectedMessageSize) != expectedMessageSize)
                        throw new InvalidProgramException();

                    //System.Diagnostics.Trace.WriteLine(BitConverter.ToString(XBeeIncoming, 0, expectedMessageSize));

                    if ((XBeeIncoming[0] != 0x10 && XBeeIncoming[1] != 0x02) ||  // dle stx
                        (XBeeIncoming[10] != 0x10 && XBeeIncoming[11] != 0x03))   // dle etx
                    {
                        System.Diagnostics.Trace.WriteLine("recover sync");
                        while (true)
                        {
                            int b = XBee.BaseStream.ReadByte();
                            if (b == 0x10)
                            {
                                int c = XBee.BaseStream.ReadByte();
                                if (c == 0x03)
                                    break;     // realigned (maybe)
                            }
                        }
                        continue;   // resume at loop start
                    }

                    UInt16 idx = BitConverter.ToUInt16(XBeeIncoming, 2);
                    UInt16 tick = BitConverter.ToUInt16(XBeeIncoming, 4);
                    Single val = BitConverter.ToSingle(XBeeIncoming, 6);

                    if (tick < lastTick[idx])
                        wrapCounter[idx]++;
                    lastTick[idx] = tick;

                    Dispatcher.BeginInvoke(DispatcherPriority.ApplicationIdle, new Action(() => DataAdd(idx, tick * wrapCounter[idx], val)));
                }
                Thread.Sleep(2);  // surely we can up with the NXT
            }
        }
void XBeeMonitorTask()
{
int[]lastTick=Enumerable.Repeat(int.MaxValue,10.ToArray();
int[]wrapCounter=新int[10];
而(!XBeeMonitorEnd)
{
if(XBee!=null&&XBee.BytesToRead>=expectedMessageSize)
{
//读取一个数据元素,解析,将其添加到集合中,有关详细信息,请参见上文