PHP Classes

Transmitt Raw Values

Recommend this page to a friend!

      PHP Serial  >  All threads  >  Transmitt Raw Values  >  (Un) Subscribe thread alerts  
Subject:Transmitt Raw Values
Summary:I would like to transmitt the raw hex, not ascii
Messages:7
Author:Tim
Date:2009-10-06 15:23:59
Update:2011-10-09 20:20:57
 

  1. Transmitt Raw Values   Reply   Report abuse  
Picture of Tim Tim - 2009-10-06 15:24:00
This script is a god send so far. Thank you very much.

However, I am having some trouble.

When I send a message, for example @serial->sendMessage(7); the output from the serial port is the ascii representation of '7', that is $37, when what I need is $07. Another example 0x1c, or $1c is needed, not $32$38 (28).

Is there a way to convince the script to output the actual number value rather the ascii?

I'm trying to interact with a lighting control system, which uses 8, 8 bit values. eg. $1c$0a$20$0$0$0$ff$bb

I have tried using \007, which works!! But that is a high as I can go. I'm assuming thats because its only 4 bit?

At the moment I am only going to worry about one byte, say $1c. I'll move onto the complete command later.

My code so far:

<?php
include "php_serial.class.php"; //Include serial class

//**************** Start Port Set Up
$serial = new phpSerial; //New serial port class
$serial->deviceSet("COM1"); //Set the comport (/dev/ttyS0)
$serial->confBaudRate(9600); //Set the baurd rate
$serial->confParity(none); //Set the Parity
$serial->confCharacterLength(8);//Set the word length
$serial->confStopBits(1); //Set Stop Bit
$serial->confFlowControl(none); /Set FLow Control


//**************** Data Setup
if(isset($_POST["newPacket"]))
{
$packet = $_POST["newPacket"];
}


//**************** Start Transmitt ************************************************
$serial->deviceOpen(); //Open the port ready for transmitt
$serial->sendMessage($packet); //Send packet

$dataResults = sprintf($packet);
echo $dataResults;

?>

I would very much appreciate any assistance.

Thank you for your time!

Tim

  2. Re: Transmitt Raw Values   Reply   Report abuse  
Picture of Tim Tim - 2009-10-07 01:58:55 - In reply to message 1 from Tim
I just realised how stupid that was. I don't know where I got the 4 bit thing from.

  3. Re: Transmitt Raw Values   Reply   Report abuse  
Picture of Tim Tim - 2009-10-07 02:58:04 - In reply to message 1 from Tim
I've been doing some reading, and I came across this:
andreavb.com/forum/viewtopic.php?To ...

It seems suing &H() would work with VB at least. Is there a PHP equivalent?

  4. Re: Transmitt Raw Values   Reply   Report abuse  
Picture of Tim Tim - 2009-10-07 14:06:24 - In reply to message 3 from Tim
All right!! Got it working!!! Yay me. I don't think I really understand what I've done, but hey, its working.

So: Here goes.

I'm passing the php '0x1c,0x0a,0x20,0x00,0x00,0x00,0xff'
Using the explode() function to convert the string to an array
Then using the hexdec() function on each element in the array and concating them together at the same time as using the chr() function on each value.

This new... something (I have no idea what it is, a string, a big number or what) is sent to the serial port.

I'm also doing some maths stuff to work out the checksum and adding it on the end before its transmitted, but thats not all that important in this case.

Here is the script. This is probably the worst code ever. But it is my first attempt and any php or server side scripting. Variable names suck too!!

<?php
include "php_serial.class.php"; //Include serial class

//**************** Start Port Set Up
$serial = new phpSerial; //New serial port class
$serial->deviceClose("COM1"); //Make sure port is closed
$serial->deviceSet("COM1"); //Set the comport (/dev/ttyS0)
$serial->confBaudRate(9600); //Set the baurd rate
$serial->confParity(none); //Set the Parity
$serial->confCharacterLength(8); //Set the word length
$serial->confStopBits(1); //Set Stop Bit
$serial->confFlowControl(none); //Set FLow Control


if(isset($_POST["newPacket"]))
{
$packet = $_POST["newPacket"];
$explodedPacket = explode(",",$packet);

$sumOfArray = 0;
for($i=0; $i<sizeof($explodedPacket); $i++)
{
$sumOfArray += $explodedPacket[$i];
}
$checksum = 255&(~($sumOfArray))+1;

$concat = "";

for($i=0; $i<sizeof($explodedPacket); $i++)
{
$temp = hexdec($explodedPacket[$i]);
$concat .= chr($temp);
}
$concat = $concat.chr($checksum);
//**************** Start Transmitt
$serial->deviceOpen(); //Open the port ready for transmitt
$serial->sendMessage($concat);
}
?>

  5. Re: Transmitt Raw Values   Reply   Report abuse  
Picture of Sami Sipponen Sami Sipponen - 2011-08-08 19:19:14 - In reply to message 4 from Tim
At Linux environment you might want to add these stty parameters to PHP Serial class:

-ignbrk -brkint -isig -icanon -iexten -noflsh

They will enable more characters that are needed in hexadecimal communication. Without those for example newline chr(10) will prevent transmission.

  6. Re: Transmitt Raw Values   Reply   Report abuse  
Picture of atanas markov atanas markov - 2011-10-09 20:08:11 - In reply to message 5 from Sami Sipponen
He can just setup serial port with no dtr(this is for modems) and xon(has some characters involved in it) flow control(rts only or none). This way the phpSerial class will send raw data(no line feeds and so on). I used this settings before I had to write a PECL extension on my own, because I need Windows support for serial comunication for some client machines(In a web app I call local server on client machines that prints on serial devices and clients are not Linux only.), and they work fine on scales(simple raw protocol with sending 1 byte only), cash register(complicated with checksums and based on raw hex). I had no problems at all with phpSerial except reading responses on Windows systems.

  7. Re: Transmitt Raw Values   Reply   Report abuse  
Picture of atanas markov atanas markov - 2011-10-09 20:20:57 - In reply to message 4 from Tim
You send a PHP string(not null terminated so it can contain 0x00 chars). ;)
You've done it the right way if you have to send data from server. A more native aproach would be to make the string with:
$binary_string = pack("H*" , $hex_string);
where hex_string is 'ea000809aabbcc' and then just make the checksum for the new binary string iterating it:
for ($i=0; $i<strlen($binary_string); $i++){
$checksum+= ord($binary_string[$i]);//let is be the pure sum
}