Unable to read serial data from docker container using UART2 on apalis evaluation imx6

apalis-imx6, linux armv7 , TorizonCore

IM trying to read the serial data using php function dio_read(). But not getting the data which is sent. dio_write works. Here is my php code

$portName = ‘/dev/apalis-uart2’;
$baudRate = 115200;
$bits = 8;
$spotBit = 1;

$bbSerialPort = dio_open($portName, O_RDWR | O_NOCTTY | O_NONBLOCK );
dio_fcntl($bbSerialPort, F_SETFL, O_SYNC);
//we’re on 'nix configure com from php direct io function
dio_tcsetattr($bbSerialPort, array(
‘baud’ => $baudRate,
‘bits’ => $bits,
‘stop’ => $spotBit,
‘parity’ => 0
));
$dataToSend = “HELLO WORLD!”;
echoFlush( “Writing to serial port data: "{$dataToSend}"” );
$bytesSent = dio_write($bbSerialPort, $dataToSend );
echoFlush( “Sent: {$bytesSent} bytes

” );

   $data = dio_read($bbSerialPort, 1); //this is a blocking call
	if ($data) {
		echoFlush(  "Data Recieved: ". $data ."<br>");
	}       
    dio_close($bbSerialPort);

I also tried using the php EXEC command to run thro shell commands

exec(‘stty -F /dev/tty 9600 -echo’, $output, $retval);
echo “
Returned with status $retval and output:\n”;
print_r($output);
unset($output);

exec(‘cat < /dev/tty &’, $output, $retval);
echo “
Returned with status $retval and output:\n”;
print_r($output);
unset($output);

exec(‘echo “Testing UART” > /dev/tty’, $output, $retval);
echo “
Returned with status $retval and output:\n”;
print_r($output);
unset($output);

But works on terminal using the following command

stty -F /dev/ttymxc1 115200 -echo
cat < /dev/ttymxc1 &
echo “Testing UART” > /dev/ttymxc1
Testing UART

Greetings @malarvizhi,

Let me summarize to see if I understand your current situation. Please correct me if I misunderstood anything.

So you are writing PHP code to read data from serial/UART. I assume this is being done inside a container, correct?

How are you starting this container? You typically need to run the container in a certain way to give access and permissions to use hardware interfaces like UART. This is documented in more detail here: Torizon Best Practices Guide | Toradex Developer Center

Finally you said this worked with basic terminal commands. Were these commands also in a container?

Since this worked with basic terminal commands I assume the issue is either with the code itself or with some permissions missing. Unfortunately I don’t know PHP too well.

When you say your PHP code didn’t work were there any errors/warnings?

Best Regards,
Jeremias

Yes you are correct.

Im starting the container using docker-compose.yml. In the yml file i have added "devices:
- “/dev/apalis-uart2:/dev/apalis-uart2"” to add the port from host to container.

Yes I ran basic terminal commands from the ternimal of the container.

command “ls /dev/* -l” shows the apalis-uart2 listed.

On my PHP code im able to open the port and send data, but not able to receive the data. (Please note im doing a loopback testing by connecting the RXD and TXD pins.

Unfortunately there’s not much we can do to provide support here since it appears to be an issue with the PHP code.

Your test with the terminal commands show that Serial UART is working fine on it’s own.

All I can advise is that, are you sure the serial data in PHP is being sent correctly? Maybe the issue is with sending and not receiving. For example if you send data with PHP code and then try to read it out via the terminal commands does that work?

This would help determine whether the issue starts with sending or receiving the data. Which would at least help narrow your debugging efforts.

Best Regards,
Jeremias

It worked when i connected with the board using a putty, serial on the COM port. I was able to send and receive data that way. Anyways thanks for the support.

Huh that sounds a bit odd. But I guess the good news is that it works now.

Let us know if you have any other issues with this.

Best Regards,
Jeremias