0
\$\begingroup\$

I'm quite new to electrical engineering, so if I didn't post something that may help to solve my problem, ask for it in comments.

I wanted to connect a Raspberry Pi 4B with a board with an STM32F779 on it.

I connected them via USB virtual COM port.

I wrote a program for the Raspberry Pi using Java and Pi4J library.

They are connected and can exchange info with each other, but when I send a byte array from the Raspberry Pi with a zero byte in it every byte after this byte also becomes zero.

Java code:

package MainPRJ;

import com.pi4j.io.serial.*;

import java.io.IOException;

public class COMToCheck
{
    private static byte[] comTxData = new byte[48];

    public static void StartCOM()
    {
        Thread comThread = new Thread(() ->
        {
            // create an instance of the serial communications class
            final Serial serial = SerialFactory.createInstance();

            // create and register the serial data listener
            serial.addListener(new SerialDataEventListener()
            {
                @Override
                public void dataReceived(SerialDataEvent event)
                {
                    try
                    {
                        System.out.println("[HEX DATA]   " + event.getHexByteString());
                    }
                    catch (IOException e)
                    {
                        // logging
                    }
                }
            });

            try
            {
                // create serial config object
                SerialConfig config = new SerialConfig();
                config.device("/dev/ttyACM0")
                        .baud(Baud._57600)
                        .dataBits(DataBits._8)
                        .parity(Parity.NONE)
                        .stopBits(StopBits._1)
                        .flowControl(FlowControl.NONE);

                // open the default serial device/port with the configuration settings
                serial.open(config);

                // continuous loop to keep the program running until the user terminates the program
                while(true)
                {
                    try
                    {
                        // this one is OK
                        // the result on the board: 01,F3,F4,F5,F6,F7,F8,F9,FA,FB,FC,FD,FE,FF...
//                      serial.write((byte)1, (byte)243, (byte)244, (byte)245, (byte)246, (byte)247,
//                                    (byte)248, (byte)249, (byte)250, (byte)251, (byte)252, (byte)253, (byte)254, (byte)255);

                        // this one is NOT OK
                        // the result on the board: 01,F3,F4,F5,F6,00,00,00,00,00,00,00...
                        serial.write((byte)1, (byte)243, (byte)244, (byte)245, (byte)246, (byte)0,
                                    (byte)248, (byte)249, (byte)250, (byte)251, (byte)252, (byte)253, (byte)254, (byte)255);

                    }
                    catch(IllegalStateException ex)
                    {
                        // logging
                    }

                    Thread.sleep(500);
                }

            }
            catch(IOException | InterruptedException | IllegalArgumentException ex)
            {
                // logging
            }
        });
        comThread.setDaemon(false);
        comThread.setPriority(Thread.MAX_PRIORITY);
        comThread.start();
    }
}

I receive data on my board like this:

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */

   USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);

  strncpy(usbRXData,(char*)Buf,*Len);

  usbRXData[*Len]=0;

  usbIsOK = 1;

  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  return (USBD_OK);
  /* USER CODE END 6 */
}

Where could I have made a mistake?

\$\endgroup\$
7
  • 3
    \$\begingroup\$ I suspect this has something to do with null-terminated strings. \$\endgroup\$
    – Hearth
    Sep 12 at 15:03
  • \$\begingroup\$ Сould you tell me anything more about this? \$\endgroup\$ Sep 12 at 15:05
  • 6
    \$\begingroup\$ A string in C is terminated with a NULL which is a value of 0. So your strncpy call stops copying when it gets to the 0 value you sent. If you're trying to copy a block of bytes with a known size you should be using memcpy. \$\endgroup\$
    – brhans
    Sep 12 at 15:13
  • 5
    \$\begingroup\$ @brhans That should be an answer, I think! \$\endgroup\$
    – Hearth
    Sep 12 at 15:21
  • 1
    \$\begingroup\$ @crackanddie: You can write your own answer (and accept it) since you solved the problem. \$\endgroup\$
    – JRE
    Sep 13 at 7:30
1
\$\begingroup\$

Thanks to @Hearth and @brhans for their help in the comments, but for some reason @brhans doesn't leave their answer here.

As @brhans said:

A string in C is terminated with a NULL which is a value of 0. So your strncpy call stops copying when it gets to the 0 value you sent. If you're trying to copy a block of bytes with a known size you should be using memcpy.

So I changed my code like this:

static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
  /* USER CODE BEGIN 6 */

   USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &Buf[0]);

  memcpy(usbRXData,(char*)Buf,*Len);

  usbRXData[*Len]=0;

  usbIsOK = 1;

  USBD_CDC_ReceivePacket(&hUsbDeviceFS);
  return (USBD_OK);
  /* USER CODE END 6 */
}
``` 
\$\endgroup\$
4
  • \$\begingroup\$ So did this work for you now? \$\endgroup\$
    – Mitu Raj
    Sep 13 at 10:43
  • \$\begingroup\$ yes. But it was really hard to understand that the problem was right here. I even thought that it could be because of multithreading on java and so I tried to use thread locks but it didn't help. So I spent 2 days to understand that the problem should be in board firmware. \$\endgroup\$ Sep 13 at 12:05
  • \$\begingroup\$ Okay, then you can mark this answer as solution. \$\endgroup\$
    – Mitu Raj
    Sep 13 at 12:43
  • \$\begingroup\$ yes, but I can mark it only after a day after answering :) so do not worry about it \$\endgroup\$ Sep 13 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.