Hi,
In my project I need to out data through the serial port . But the OS
is using the serial port for receiving commands. How to disconnect it
and use it for other purposes??
Also can GPIO be controlled using C ?
Thanks
Hi,
In my project I need to out data through the serial port . But the OS
is using the serial port for receiving commands. How to disconnect it
and use it for other purposes??
Also can GPIO be controlled using C ?
Thanks
You don’t specify which platform you are using, which OS, or which serial port you want to use.
My system is a BeagleBoard-C4 running Angstrom Linux. I’ll assume that you want to use the RS232 port that’s populated on the board, serial port 2. (Serial port 0 and serial port 1 are available via expansion connector pins)
In /etc/inittab there is a line that runs getty to provide login services on ttyS2. The line looks like: “S:2345:respawn:/sbin/getty 115200 ttyS2” if you comment it out, then getty will not run on serial port 2.
The default kernel command line from u-boot is: “console=ttyS2,115200n8 mpurate=720 buddy=none camera=lbcm3m1 vram=12M omapfb.mode=dvi:640x480MR-16@60 omapdss.def_disp=dvi root=/dev/mmcblk0p2 rw rootfstype=ext3 rootwait”
If you change the console statement in the command line to something other than ttyS2 default console messages will not be sent to the serial port.
GPIO can be controlled via C. It can be done via user space programs similar to the method here: http://wh1t3s.com/2009/05/14/reading-beagleboard-gpio/ or via kernel modules for interrupt driven methods.
Arun wrote:
Hi,
In my project I need to out data through the serial port . But the OS
is using the serial port for receiving commands. How to disconnect it
and use it for other purposes??Thanks
Hi,
If you want to output date through the serial port, you can disable
the CONTROL TTY function of the serial temporarily.
Following is a example for you. I just write the getl() section, you
need to call your write() function to ouput the data.
You do NOT have to edit the /etc/inttab file, which would make you
unable to login to the console.
May it helps you.
--------------file demo.c----------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <termios.h>
#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyS2"
static unsigned char buf[255];
int fd; //fd--serial port
static int status; //the serial port status, 1 for specified, 0 for
console
struct termios oldtio,newtio;
void err(int no)
{
printf("err num:%d", no);
exit(EXIT_FAILURE);
}
void ttys(int type)
{
if (1 == type) { //To specify the use of the serial port
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY); //open it, and release
the CONTROL TTY function.
if (fd < 0)
err(3);
tcgetattr(fd, &oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
//if you want to output something, you need to change some figures
of the console.
newtio.c_cc[VINTR] = 0; /* Ctrl-c */
newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VKILL] = 0; /* @ */
newtio.c_cc[VEOF] = 4; /* Ctrl-d */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character
arrives */
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl-q */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
//write(fd, "status = 1\n", 11);
status = 1;
}
else {
//turn it back to console. Then you can use the console.
tcsetattr(fd, TCSANOW, &oldtio);
close(fd);
status = 0;
}
}
//get something. If you want to ouput data, just call puts(through
standard output) or write(through serial port.)
void getl(void)
{
int ret;
memset(buf, 0, 100);
if (0 == status) { //read from standard input.
gets(buf);
}
else if (1 == status) { //read from specified serial port.
ret = read(fd, buf, 200);
buf[ret] = 0;
/*
if (ret > 0)
buf[ret - 1] = 0;
else
buf[0] = 0;
*/
}
}
int main()
{
}
---------------file end------------------
You can get some demos from google.
- In my project I need to out data through the serial port . But the OS
- is using the serial port for receiving commands. How to disconnect it
- and use it for other purposes??
If you have a USB/serial-port adapter hanging around, you can use that as a second serial port, accessed as /dev/ttyUSB0.
Jan
Are all usb-serial adapters autodetected the same way and associated
with /dev/ttyUSB0? Just wondering what usb/serial device you're using
-michael
Are all usb-serial adapters autodetected the same way and associated
with /dev/ttyUSB0? Just wondering what usb/serial device you're using
Most off-the-shelf USB/serial adapters use chips from FTDI or Prolific. They require different drivers, but I believe the angstrom distribution and others support both and will autodetect the first adapter as /dev/ttyUSB0. I used an FTDI adapter and I might have used one from Prolific (don't recall for sure).
Jan