views:

886

answers:

3

Hello, I am trying to send something to serial port (r232) with PHP. I am using this class: http://www.phpclasses.org/browse/package/3679.html

The problem is that I am allowed to send only 1 byte. But if I send something like "1", I am actually sending 49 (ASCII for 1). Instead of send("1"), I tried with send(1) but it is no good, because this is integer which has 2 bytes. So is there a way to send a "real" char, not ASCII equivalent?

+4  A: 

The chr() function returns a character given by the integer for the corresponding ascii character.

Tony k
It returns a character, but as a PHP string. That won't help the OP
Alan Storm
Doesn't the library take a PHP string? It passes it into fwrite...
Mark
To be more precise: `chr` turns an integer into a byte.
Gumbo
A: 

It looks like the library is expecting characters as input. If you need to send the character which would encode to 0x01, you just send "\001". The function chr() would convert characters to integer values and would be no use here.

One more thing: The byte size of integers depends on the underlying system and is mostly 4 bytes.

soulmerge
Not so. ord() converts characters to integer values, and chr() does the opposite.
Mark
A: 

I'm not sure what you are trying to accomplish. Are you trying to to send the integer 1? Not being familiar with the class, have you tried to give just the value 1 as an argument? If that doesn't work, try to wrap it with the chr() function.

Henrik Paul