What would be the best way to send char's and strings to a serial port?
My problem is that i would like to send commands to a robotic arm that ive played about with a few months ago. At the moment the electronics teacher is using QBASIC to work it... I used to love QBASIC but I don't have a Linux equivalent and to be really honest I don't want one (BASIC == S...L...O...O...O...W...W...). In his current program he's opening the serial port and then using the print statement to send the strings to the robotic arm. How would I do this on Linux in c?
Would I use the file handling commands and open /dev/S0 (what ever the Comm ports are called under Linux) and then use fprintf(serial_port, "command")??? Would i have any problems doing this f5rom a normal user account on my system or would I need to be root?
It would be nice if anyone could give me some example code or give me the address of a good web site on the subject:). I'm Not so hot on file handling in c so even some examples of this would be nice.
Thanx for the help :)
Dennis Dryden
********************************************************************** IMPORTANT: This email and its attachments are intended for the addressee only and may be confidential. If they have come to you in error you must take no action based on them nor must you copy or show them to anyone; please telephone us immediately.
The contents of this email and attachments represent the views of the sender and are not necessarily the representative of the views of The College of West Anglia **********************************************************************
on Fri, Apr 19, 2002 at 12:09:59PM +0000, Dryden wrote:
What would be the best way to send char's and strings to a serial port?
[...]
Would I use the file handling commands and open /dev/S0 (what ever the Comm ports are called under Linux) and then use fprintf(serial_port, "command")??? Would i have any problems doing this f5rom a normal user account on my system or would I need to be root?
You don't need to be root. Check the permissions for /dev/ttySx. The UID that you want to access the port needs read and write permissions.
#include <unistd.h> #include <stdio.h> #include <fcntl.h>
int main() { FILE *fp; int fd; char dev[] = "/dev/ttyS0";
/* * O_NOCTTY is needed under Linux and non-BSD unices otherwise the * tty device might become the processes controlling tty. */ if ((fd = open(dev, O_RDWR|O_NOCTTY, 0)) == -1 || (fp = fdopen(fd, "w+")) == NULL) err(1, "unable to open `%s'", dev);
fprintf(fp, "hello world\n");
fclose(fp); return 0; }
xs@kittenz.org xs@kittenz.org wrote:
You don't need to be root. Check the permissions for /dev/ttySx. The UID that you want to access the port needs read and write permissions.
I think the normal way of doing this is for there to be a "serial" group or similar, the devices belong to this group and are group-read-writeable and all users who are permitted to use the serial devices are listed in /etc/group
You can check a particular user's groups quickly with: id username or, for the current user: id will should the current groups. Note that changes to /etc/group may require you to log out and back in before taking effect, or at least to start a new shell.
Hope that helps, MJR
Hi Dennis,
This may be of interest to you. It's a little Perl script which sends text to an LCD display. It actually displays the curent IP address of the ppp0 interface. The principle is the same as for your robot arm. Where it gets interesting (complicated) is reading and writing the serial port at the same time. If you're interested I have a script which responds to text messages, which cause the computer to bring the modem up.
/dev/ttyS0 = COM1 in Windows
Sorry for the poor coding style... but it works :-)
#!/usr/bin/perl -w
# Set serial port ttyS0 to 9600 baud system("/bin/stty -F /dev/ttyS0 9600");
# Open ttyS0 for writing open (LCD,">/dev/ttyS0");
# Get IP address of ppp0
$ip =`/sbin/ifconfig ppp0|grep inet`;
# Removes everything except ip address
$ip =~ s/\s+\D+addr:(\d+.\d+.\d+.\d+).*?\n/$1/;
# Hopefully write data to LCD # \xFE (254d) tells LCD to accept a control character # \x01 (1d) Clears LCD display # \x0 (0d) Move cursor to home posistion # \xC0 (192) Move cursor to start of second line
print LCD "\xFE","\x01","\xFE","\x0","Modem Connected"; print LCD "\xFE","\xC0","IP: ",$ip;
close (LCD);
Hope that helps
Chris
Chris *************************************************************************** E Mail Chris@glovercc.clara.co.uk WWW http://www.glovercc.clara.co.uk ICQ 18054759 Someday, we'll look back on this, laugh nervously and change the subject. -Anon
On Fri, 19 Apr 2002, Dryden wrote:
What would be the best way to send char's and strings to a serial port?
My problem is that i would like to send commands to a robotic arm that ive played about with a few months ago. At the moment the electronics teacher is using QBASIC to work it... I used to love QBASIC but I don't have a Linux equivalent and to be really honest I don't want one (BASIC == S...L...O...O...O...W...W...). In his current program he's opening the serial port and then using the print statement to send the strings to the robotic arm. How would I do this on Linux in c?
Would I use the file handling commands and open /dev/S0 (what ever the Comm ports are called under Linux) and then use fprintf(serial_port, "command")??? Would i have any problems doing this f5rom a normal user account on my system or would I need to be root?
It would be nice if anyone could give me some example code or give me the address of a good web site on the subject:). I'm Not so hot on file handling in c so even some examples of this would be nice.
Thanx for the help :)
Dennis Dryden
IMPORTANT: This email and its attachments are intended for the addressee only and may be confidential. If they have come to you in error you must take no action based on them nor must you copy or show them to anyone; please telephone us immediately.
The contents of this email and attachments represent the views of the sender and are not necessarily the representative of the views of The College of West Anglia
main@lists.alug.org.uk http://www.anglian.lug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
Hi Dennis
Ooooo, Robotic arms. This kind of stuff is not that far removed from a project I've been working on for some time now. The software I have is designed for realtime motion contral systems running under linux -This could be anything from a simple CNC milling machine through to a stewart platform (Flight simulator). Buried in the source code is some kinematics for robotic arms
Applying it to serial port communications shouldn't be too difficult (if you like C). If you just want a simple application to talk to the arm through ttyS0, why not use Tcl.
Regards, Paul.
On Friday 19 April 2002 13:09, Dryden wrote:
My problem is that i would like to send commands to a robotic arm that ive played about with a few months ago.