LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 823|回复: 0

linux下串口通信问题

[复制链接]
发表于 2004-6-10 23:20:00 | 显示全部楼层 |阅读模式
大家好!我按照下面这个进行串口测试,将pc的com1和com2通过3线串口线相连,在windows测试能够互相发送接收文件,但是在linux下,分别对miniterm.c更改了ttyS0,ttyS1,
就是下面这个地方:


#define BAUDRATE B38400
#define MODEMDEVICE "/dev/modem" <=====Change this to "/dev/ttyS0"

但是两个文件(比如分别命名“ttys0”,“ttys1”)我编译之后,键入
./ttys0
或者./ttys0就没反应了,
按照这里说的应该是从一个终端输入的字符会从另外一个终端显示出来,
但是我的却不能,为什么呢?我是菜鸟谢谢!








keylabs_miniterm_test.txt for KeyLabs Test Packages
===============================================================

Testing Notes:   Testing the serial ports

1. About this document

This document describes the testing of serial ports under Linux.  This test is
not by any means exhaustive -- it is used to simply verify the serial ports
work on a given machine.

To do this test, you'll need to have either has two computers with working serial
ports or a single computer with two serial ports.  You will also need one
null-modem serial cable to go between the two serial ports to be tested.

2. Making the miniterm_ttys* programs from scratch

This KeyLabs package comes with both the source code and compiled executables for
the miniterm_ttys0 and miniterm_ttys1 programs.  If you don't have a copy of
miniterm.c and you want the whole coconut, you can get it at:

http://metalab.unc.edu/pub/Linux ... .4.examples.tar.gz.

The miniterm.c program is one of the examples.  Download and save the tarball
above to it's own directory and uncompress.

tar -xvzf lpg-0.4.examples.tar.gz

This will create an examples directory where you'll find a file called miniterm.c.
This is the only file you will need if you want to do these tests.

This editing example assumes you have two standard serial ports in your machine
and they are setup for ttyS0 ttyS1 (com1 & com2 in DOS lingo.)

Using your favorite text editor open miniterm.c.  Modify the modem definition to
ttyS0:

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/modem" <=====Change this to "/dev/ttyS0"

Save the file without closing your text editor.  Compile the saved miniterm.c
code.  

gcc -o miniterm_ttys0 miniterm.c.   

This will produce an executable file called miniterm_ttys0.

Now repeat the process for ttyS1.  Again, change the definition, save and compile
with this command string:

gcc -o miniterm_ttys1 miniterm.c

This will produce an executable file called miniterm_ttys1.

Depending on how the testing system is setup during install, you may need to
change the permissions on the serial port devices to allow data traffic.

chmod a+rw /dev/ttyS0
chmod a+rw /dev/ttyS1


3. Running the test

Steps:

1. If you don't have the miniterm_ttys* programs, make them as described above.
2. Connect the serial ports to be tested according to how you're going to test.
   (See Same Machine or Different Machine tests below.)
3. Run the miniterm_ttys* programs as described below.


There are two ways you can run this test; either on the same machine with a
null modem connecting the two serial ports or across two different machines with
a null modem connecting the serial ports to be tested. KeyLabs testers usually
run this test across the serial ports of two separate machines.

Same machine test

Connect a null modem cable to the two serial ports.  Next, either open two xterms
on your desktop or switch to a different vc.  In the terminals, cd into the
directory which contains the two programs you just compiled.  In one term execute
miniterm_ttys0 and in the other execute miniterm_tys1.  If the connections are
correct you should see anything typed in one term window show in the other term
window.

Across different machines test

Connect a null modem cable to a serial port on each machine.  Next, either open
an xterm or a vc on each machine.  In the terminals, cd into the directory which
contains the two programs you just compiled.  In one term execute miniterm_ttys0
and in the other execute miniterm_tys1.  If the connections are correct you should
see anything typed in one term window show in the other term window.  You may
need to try various combinations of the miniterm programs on each machine until
you find which one is connected to ttyS0 and ttys1.  Once the connections are
correct you should see anything typed in one term window show in the other term
window.



















/*
*  AUTHOR: Sven Goldt (goldt@math.tu-berlin.de)
*
*  This program is free software; you can redistribute it and/or
*  modify it under the terms of the GNU General Public License
*  as published by the Free Software Foundation; either version 2
*  of the License, or (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*/
/*
This is like all programs in the Linux Programmer's Guide meant
as a simple practical demonstration.
It can be used as a base for a real terminal program.
*/

#include <termios.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/signal.h>

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define ENDMINITERM 2 /* ctrl-b to quit miniterm */

#define _POSIX_SOURCE 1 /* POSIX compliant source */

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

void child_handler(int s)
{
   STOP=TRUE;
}

main()
{
int fd,c;
struct termios oldtio,newtio,oldstdtio,newstdtio;
struct sigaction sa;

/*
  Open modem device for reading and writing and not as controlling tty
  because we don't want to get killed if linenoise sends CTRL-C.
*/
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio); /* save current modem settings */

/*
  Set bps rate and hardware flow control and 8n1 (8bit,no parity,1 stopbit).
  Also don't hangup automatically and ignore modem status.
  Finally enable receiving characters.
*/
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

/*
Ignore bytes with parity errors and make terminal raw and dumb.
*/
newtio.c_iflag = IGNPAR;

/*
Raw output.
*/
newtio.c_oflag = 0;

/*
Don't echo characters because if you connect to a host it or your
modem will echo characters for you. Don't generate signals.
*/
newtio.c_lflag = 0;

/* blocking read until 1 char arrives */
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;

/* now clean the modem line and activate the settings for modem */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

/*
  Strange, but if you uncomment this command miniterm will not work
  even if you stop canonical mode for stdout. This is a linux bug.
*/
tcsetattr(1,TCSANOW,&newtio); /* stdout settings like modem settings */

/* next stop echo and buffering for stdin */
tcgetattr(0,&oldstdtio);
tcgetattr(0,&newstdtio); /* get working stdtio */
newstdtio.c_lflag &= ~(ICANON | ECHO);
tcsetattr(0,TCSANOW,&newstdtio);

/* terminal settings done, now handle in/ouput */
switch (fork())
{
  case 0: /* child */
   /* user input */
   close(1); /* stdout not needed */
   for (c=getchar(); c!= ENDMINITERM ; c=getchar()) write(fd,&c,1);
   tcsetattr(fd,TCSANOW,&oldtio); /* restore old modem setings */
   tcsetattr(0,TCSANOW,&oldstdtio); /* restore old tty setings */
   close(fd);
   exit(0); /* will send a SIGCHLD to the parent */
   break;
  case -1:
   perror("fork");
   tcsetattr(fd,TCSANOW,&oldtio);
   close(fd);
   exit(-1);
  default: /* parent */
   close(0); /* stdin not needed */
   sa.sa_handler = child_handler;
   sa.sa_flags = 0;
   sigaction(SIGCHLD,&sa,NULL); /* handle dying child */
   while (STOP==FALSE) /* modem input handler */
   {
    read(fd,&c,1); /* modem */
    write(1,&c,1); /* stdout */
   }
   wait(NULL); /* wait for child to die or it will become a zombie */
   break;
}
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表