简体   繁体   中英

C# splits the Payload when sending a byte array with Serial Port

I am currently trying to send a 64 byte long command to a microchip that is connected via the USB Port. I tried using the SerialPort.Write Command to send a byte array, but found out, that it actually splits the Payload into 16 byte packets, sends these individually, with empty NAKs inbetween. My code is as follows:

SerialPort Chip = new SerialPort("COM4");
byte[] Reset = new byte[64];
byte[] Command = new byte[64];
Reset[0] = 0x70;
Reset[1] = 0xAB;
Reset[2] = 0xCD;
Reset[3] = 0xEF;
for (int z = 4; z < 64; z++)
{
    Reset[z] = 0x00;
}
for (int z = 0; z < 64; z++)
{
    Command[z] = 0x00;
}
Command[0] = 0x60;
Command[7] = 0x80;
Command[8] = 0x02;
Command[9] = 0x03;
Command[10] = 0x10;
Command[11] = 0x01;
Chip.Open();
Chip.Write(Reset,0,64);
Thread.Sleep(25);
Chip.Write(Command,0,64);

Does anyone know of a way to send my command in a single packet?
Furthermore, the same happens when I try to send the 64 byte command with a program called HTerm, while a program someone else wrote in C++ seems to work just fine, but I have very little understanding of C++, so that doesn't help me.

This is what I used for my application that sends byte-arrays:

bytesToSend = new byte[6]
                {
                    0, 5, 0,1, 13,10
                };

                _spManager.sendData(bytesToSend);

sendData function:

 public void sendData(byte[] bytestosend)
        {
          _serialPort.Open();
            StartListening();
            _serialPort.Write(bytestosend, 0, bytestosend.Length);

           System.Threading.Thread.Sleep(500);
        }

I also had a function that allows to convert hexadecimal stuff (from a textbox) to bytes (in case I had a command that changes ( so an array without a predfined length ):

var bytes = tbInput.Text.Split(' ').Select(h => byte.Parse(h, NumberStyles.AllowHexSpecifier)).ToArray();


                _spManager.sendData(bytes);

StartListening() :

            /// <summary>
            /// Connects to a serial port defined through the current settings
            /// </summary>
            public void StartListening()
            {
                // Closing serial port if it is open
                if (_serialPort != null && _serialPort.IsOpen)
                        _serialPort.Close();

            // Setting serial port settings
            _serialPort = new SerialPort(
                _currentSerialSettings.PortName,
                _currentSerialSettings.BaudRate,
                _currentSerialSettings.Parity,
                _currentSerialSettings.DataBits,
                _currentSerialSettings.StopBits);

            // Subscribe to event and open serial port for data
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(_serialPort_DataReceived);
            _serialPort.Open();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM