win XP 上用VM 装linux,串口通信问题。
hi,大家好。
我在win xp上用vm装了fedora(linux) 系统,现在需要测串口通信,要在虚拟机上跑串口通信程序。
在同一台虚拟机中能完成测试不?环境如上所述。我已按网上方法设置可达到如下目的:
(1)打开一个终端,执行 cat /dev/ttyS0
(2)打开另一个终端,执行 echo hi everyone > /dev/ttyS1
在第一个终端(即 cat /dev/ttyS0) 能看到 hi everyone,说明两个串口之间通信了。
请问这种情况下,达到了我想用C程序测试串口通信所要的条件了吗?谢谢。。
我写了两个程序,一个send.c 用来发送,一个recv.c,用来接收串口信息,但互相不鸟对方,请教各位老大,我的系统环境有问题不?或我的代码有问题不?
以下是我的代码:
// send.c ..........................................
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#define TTYS0 /dev/ttyS0
#define TTYS1 /dev/ttyS1
#define MAX_BUF_SIZE 1024*20 // define maxinum buffer length
int open_serial_port(int device)
{
int serial_port;
if (0 == device)
{
if (serial_port = open("dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY) < 0)
{
perror("open /dev/ttyS0 encouter an error\n");
exit(1);
}
printf("open /dev/ttyS0 success.serial_port is %d..\n",serial_port);
}
else
{
if (serial_port = open("/dev/ttyS1",O_RDWR | O_NOCTTY | O_NDELAY) < 0 )
{
perror("open /dev/ttyS1 encouter an error\n");
exit(1);
}
printf("open /dev/ttyS1 success.serial_port is %d..\n",serial_port);
}
return serial_port;
}
int main(int argc,char *argv[])
{
char *msg = "hi,everyone...";
int sendlen = strlen(msg);
int serial_port = open_serial_port(1); // open /dev/ttyS0
struct termios opt;
tcgetattr(serial_port,&opt);
cfmakeraw(&opt);// sets the structure specified by opt to raw mode.
// there is no effect on the hardware until a subsequent successful call to tcsetattr()...
cfsetispeed(&opt,B38400); // set input baud as 38400 bps
cfsetospeed(&opt,B38400);
opt.c_cc[VMIN] = 1;
opt.c_cc[VTIME] = 1;
tcsetattr(serial_port,TCSANOW,&opt); //change attr as soon as possible
while (1)
{
int ret_val = write(serial_port,msg,sendlen);
assert(ret_val != -1);
printf("write %d bytes to serial_port %d successfully...\n",ret_val,serial_port);
sleep(3);
}
if (close(serial_port) == -1)
{
printf("close serial_port error\n");
(1);
}
printf("leave...\n");
return 0;
}
//recv.c .........................................................
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#define TTYS0 /dev/ttyS0
#define TTYS1 /dev/ttyS1
#define MAX_BUF_SIZE 1024*20 // define maxinum buffer length
int open_serial_port(int device)
{
int serial_port;
if (0 == device)
{
if (serial_port = open("dev/ttyS0",O_RDWR | O_NOCTTY | O_NDELAY) < 0)
{
perror("open /dev/ttyS0 encouter an error\n");
exit(1);
}
printf("open /dev/ttyS0 success,serial_port is %d...\n",serial_port);
}
else
{
if (serial_port = open("/dev/ttyS1",O_RDWR | O_NDELAY | O_NOCTTY) < 0 ) // O_NOCTTY
{
perror("open /dev/ttyS1 encouter an error\n");
exit(1);
}
printf("open /dev/ttyS1 success,serial_port is %d...\n",serial_port);
}
return serial_port;
}
int main(int argc,char *argv[])
{
char recvbuf[MAX_BUF_SIZE];
int serial_port = open_serial_port(1); // open /dev/ttyS0
struct termios opt;
tcgetattr(serial_port,&opt);
cfmakeraw(&opt);// sets the structure specified by opt to raw mode.
// there is no effect on the hardware until a subsequent successful call to tcsetattr()...
cfsetispeed(&opt,B38400); // set input baud as 38400 bps
cfsetospeed(&opt,B38400);
opt.c_cc[VMIN] = 1;
opt.c_cc[VTIME] = 1;
tcsetattr(serial_port,TCSANOW,&opt); //change attr as soon as possible
while (1)
{
int ret_val = read(serial_port,recvbuf,MAX_BUF_SIZE);
assert(ret_val != -1);
printf("read %d bytes from serial_port %d successfully...\n",ret_val,serial_port);
printf("recv msg is %s...\n",recvbuf);
sleep(3);
}
if (close(serial_port) == -1)
{
printf("close serial_port error\n");
exit(1);
}
printf("leave...\n");
return 0;
}