Hello

Im trying to read from ttyUSB0 on Ubuntu using the following code:

<?php
// Create the context
set_time_limit(0);
@ob_end_flush();
ob_implicit_flush(true);

$c = stream_context_create(array('dio' =>
array('data_rate' => 1200,
'data_bits' => 7,
'stop_bits' => 2,
'parity' => 0,
'flow_control' => 0,
'is_blocking' => 0,
'canonical' => 1)));
// You can change the properties above, matched with your device

$handle = fopen("dio.serial:///dev/ttyUSB0", "r+", false, $c); //format: dio.serial://[device-name]
if ($handle) {
	echo "Initiate port";

	while(!feof($handle)) {

		$data = fgets($handle, 1024); // try to make long, so it will detect the newline automatically
		echo $data;
		sleep(1);
	}

	fclose($handle);
}
?>

It only echos $data sometimes and is not really reliable. Im not sure why it stops/starts working. Ive been using cutecom sometimes at the same time to compare the returned data.

Would cutecom interfere with the reading? Why would it only echo $data sometimes?

How can I make this reliable?

CAN ANYONE HELP ME!!

reading info from cutecom looks like this:

301 6500 MPU 01.04\0x14
301 6500 MPU 01.04\0x14
3011D6541 LC 01.07\0x14
3011D6541 LC 01.07\0x14

i bet it's too late but it can help someone else...
You must check group permissions of ttyUSB0 device
and u add this group at www-data user
so www-data user can rw on ttyUSB0
after doing that, reboot the system and try the script
(work fine as is, set only the array of proprieties to fit your device)

$ ls -l /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 2011-03-30 22:11 /dev/ttyUSB0

$ sudo usermod -G www-data,dialout www-data

bye
tingonatliberodotit

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.