![Baud Baud](/uploads/1/2/6/4/126466487/371275319.png)
When using a serial port with the Linux serial module 8250, you are usually limited to the predefined baud rates in the driver. If you are using a serial card with a non-standard clock, most applications will not be able to use the non-standard baud rates without some modifications. Linux uses a dirty method for non-standard baud rates, called “baud rate aliasing”. Basically, you tell the serial driver to interpret the value B38400 differently. This is controlled with the ASYNCSPDCUST flag in serialstruct member flags. You need to manually calculate the divisor for the custom speed as follows.
I'm using PHP to interface my AVR microcontroller in /dev/ttyS0. I bet someone else does the same.
Here's some hint :
- dio_tcsetattr -> is set to enable :
- RTS / CTS hardware control
- ICANON mode
(means that dio_read will wait until 0x0A/LF or other control character is entered in /dev/ttyS0 before it returns reading result, when you use dio_write it will also send 0x0A/LF automatically in the end of the message to your device).
For those who dont need RTS/CTS and/or ICANON, you can use linux command : stty.
Here's mine :
<?php
exec('stty -F /dev/ttyS0 4800 raw');
$fd=dio_open('/dev/ttyS0',O_RDWR | O_NOCTTY | O_NDELAY);
dio_fcntl($fd,F_SETFL,0);
dio_write($fd,'x41',1); // write 0x41 or 'A' to /dev/ttyS0
// Replace result_length with your expected command result length
for ($i=0;$i < result_length;$i++) {
$result .=dio_read($fd, 1);
}
echo $result;
?>
Refer to :
- Serial Programming Guide for POSIX Operating Systems, http://www.easysw.com/~mike/serial/
- stty man pages