geft
geft

Reputation: 625

RS232 transmitted packet mismatch

I am trying to figure out why a simple RS232 transmission doesn't work. As shown in this picture, I'm running a debugger to view the data values to be transmitted. The problem is that the bytes sent and received are completely different. I have no idea what is wrong here. Please let me know if anybody needs more info.

Here is the code segment:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;

namespace ShimmerAPI
{
    class Transmission
    {
        const short multiplier = -1020; // 10000/-9.81

        byte[] Combine (byte[] a0, byte[] a1, byte[] a2)
        {
            byte[] ret = new byte[a0.Length + a1.Length + a2.Length];
            Array.Copy(a0, 0, ret, 0, a0.Length);
            Array.Copy(a1, 0, ret, a0.Length, a1.Length);
            Array.Copy(a2, 0, ret, a0.Length + a1.Length, a2.Length);
            return ret;
        }

        short Multiply (double x)
        {
            if (x > 25 || x < -25)
                return 0;
            else
                return Convert.ToInt16(x * multiplier);
        }

        public void TransmitData (SerialPort port, ObjectCluster obj)
        {
            double[] data = obj.GetData().ToArray();

            short X = Multiply(data[3]);
            short Y = Multiply(data[5]);
            short Z = Multiply(data[7]);

            byte[] combBytes = Combine(BitConverter.GetBytes(Z), BitConverter.GetBytes(Y), BitConverter.GetBytes(X));

            port.Write(combBytes, 0, 1);
        }
    }
}

screenshot of data capture and code

Upvotes: 0

Views: 301

Answers (1)

Ben Voigt
Ben Voigt

Reputation: 283624

The second and third arguments to SerialPort.Write specify part of the array to send; your code is transmitting only a single byte, because that's what you requested (third argument, count, is set to 1).

I don't know whether the other bytes in your sniffer are coming from, but the byte at index 0, which is 11, is the only byte sent by this function.

You may have meant

port.Write(combBytes, 0, combBytes.Length);

Upvotes: 1

Related Questions