Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sending a binary file out a COM / ttyS0 port

Status
Not open for further replies.

NewtownGuy

Technical User
Jul 27, 2007
146
0
0
US
Hello,

I'm trying to control a robot from a Fedora Core 3 machine running Apache 2. I have a web page on that machine that, when clicked, runs a cgi-script on that machine that is trying to send a binary file out the COM1 / ttyS0 port out that same machine.

I have several problems:

1) for test purposes (not using the script), the command I'm using to send a file out the COM port is not working. Here are two things I've tried:

[root@-001 log]# cp test1.bin /dev/ttyS0
cp: overwrite `/dev/ttyS0'? n
[root@-001 log]# cp test1.bin >/dev/ttyS0
cp: missing destination file
Try `cp --help' for more information.
[root@-001 log]#


Here's a fragment from my actual perl script:

Code:
# Open output file and redirect to COM port
open(OUT, ">/dev/ttyS0") or die "Can't open : ttyS0 $!\n"; 
binmode(OUT);
flock (OUT, 2); # Lock the file to protect it from writing by others

# Output one fixed 8-bit value whose value is 0xFF
print OUT chr(hex('FF'));

2) I've used setserial /dev/ttyS0 baud_rate 9600 to configure the COM port. Is this the proper way to do it ? Do I need to put anything in a startup script so it's always active on boot ?

3) Do I need to set any permissions to write to a COM port ? Do I need to write to a file first, and then copy the entire file (< 10 bytes) to the COM port ?

4) It's important that the entire command string be sent as a contiguous set of bytes without any null bytes in the middle. Are there any timing problems doing this from a perl script that composes and prints each character ? Or, do I need to write the entire string to a file as a buffer, and then write the file to the COM port ?

I know the connection to the robot is good, that the robot is good, and that a test command file are good because I've tested them from a COM port on a Windows machine using the same binary file I've used in Linux.

Thank you.

-- NewtownGuy
 
I assume that /dev/ttys0 already exists so let's check the permissions. Issue this command:

Code:
ls -l /dev/ttys0

Since the script will be running under apache and apache runs as user apache and group apache, let's make the device file accessable to group apache. We will keep the owner root but change the group to apache. Issue this command:

Code:
chown root:apache /dev/ttys0

We could have used chgrp here but I wanted to be sure the owner was root. Now we want both root and group apache to be able to read and write to the device file so we will issue this command:

Code:
chmod 660 /dev/ttys0

If for some reason you need every stinkin' body to be able to write to /dev/ttys0, then change permissions to 666 rather than 660. If your script is not able to write to a file, then you may need to set permissions on that file so the world can read, write and execute it. This would be done by using chmod to set the permissions on the file to 777.
 
I guess you already know that that should be an upper case "S" in ttyS0. Forgive my laziness but two finger typists seem to take a few shortcuts.


 
TO: RhythmAce

Thank you. Here's the result of the commands you specified:


[root@-001 httpd]# ls -l /dev/ttyS0
crw-rw---- 1 root uucp 4, 64 Aug 13 12:10 /dev/ttyS0
[root@-001 httpd]# chown root:apache /dev/ttyS0
[root@-001 httpd]# chmod 660 /dev/ttyS0
[root@-001 httpd]# chmod 666 /dev/ttyS0
[root@-001 httpd]# ls -l /dev/ttyS0
crw-rw-rw- 1 root apache 4, 64 Aug 13 12:10 /dev/ttyS0
[root@-001 httpd]#


How many of these commands need to be run each time the system boots ? What file should I put them in so they're always active ?

-- NewtownGuy
 
These are set. They will not change unless you change them or recreate /dev/ttyS0. Rebooting will not change them either. You can test it by rebooting then running ls -l /dev/ttyS0 again.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top