当前位置: 技术问答>linux和unix
帮个忙!!终端串口通信的程序怎么写
来源: 互联网 发布时间:2015-01-13
本文导语: 对方发的数据我能接受到,我发的字符对方也能接受就行了!! 急用!!!! | 程序你自己写吧,但我告诉你怎们写: 1. ioctl ( termerio ....) 2. write 3. read 4. select 5.open example: struct termi...
对方发的数据我能接受到,我发的字符对方也能接受就行了!!
急用!!!!
急用!!!!
|
程序你自己写吧,但我告诉你怎们写:
1. ioctl ( termerio ....)
2. write
3. read
4. select
5.open
example:
struct termio TermConfig;
PortID = open( FullDeviceName, O_RDWR );
ioctl( PortID, TCSETA, &TermConfig );
select( p_iFd + 1, &l_sFds, NULL, NULL, &l_sTimeOut );
len = read( p_iFd, p_cpBuf, p_iLen );
write(p_iFd, p_cpBuf, p_iLen );
1. ioctl ( termerio ....)
2. write
3. read
4. select
5.open
example:
struct termio TermConfig;
PortID = open( FullDeviceName, O_RDWR );
ioctl( PortID, TCSETA, &TermConfig );
select( p_iFd + 1, &l_sFds, NULL, NULL, &l_sTimeOut );
len = read( p_iFd, p_cpBuf, p_iLen );
write(p_iFd, p_cpBuf, p_iLen );
|
快结帐!!!!!!!
|
see this
#include
#include
#include
#include
#include
#include
#define BAUDRATE B115200
open_port(void)
{
int fd,speed;
struct termios newtio;
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd==-1)
{
perror("open_port:Unable to open /dev/ttyS0");
}
tcgetattr(fd,&newtio);
bzero(&newtio,sizeof(newtio));
//setting c_cflag
newtio.c_cflag |= (CLOCAL|CREAD);
newtio.c_cflag &=~PARENB;
newtio.c_cflag &=~PARODD;
newtio.c_cflag &=~CSTOPB;
newtio.c_cflag &=~CSIZE;
newtio.c_cflag |=CS8;
newtio.c_oflag|=OPOST;
//setting c_iflag
newtio.c_iflag &=~(IXON|IXOFF|IXANY);
cfsetispeed(&newtio,BAUDRATE);
cfsetospeed(&newtio,BAUDRATE);
printf("speed=%dn",BAUDRATE);
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
return(fd);
}
main()
{
int fd,n,i;
unsigned char buff[256];
fd = open_port();
while(1){
n = read(fd,buff,256);
if(n>0){
printf("n=%dn",n);
for(i=0;i
#include
#include
#include
#include
#include
#include
#define BAUDRATE B115200
open_port(void)
{
int fd,speed;
struct termios newtio;
fd = open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd==-1)
{
perror("open_port:Unable to open /dev/ttyS0");
}
tcgetattr(fd,&newtio);
bzero(&newtio,sizeof(newtio));
//setting c_cflag
newtio.c_cflag |= (CLOCAL|CREAD);
newtio.c_cflag &=~PARENB;
newtio.c_cflag &=~PARODD;
newtio.c_cflag &=~CSTOPB;
newtio.c_cflag &=~CSIZE;
newtio.c_cflag |=CS8;
newtio.c_oflag|=OPOST;
//setting c_iflag
newtio.c_iflag &=~(IXON|IXOFF|IXANY);
cfsetispeed(&newtio,BAUDRATE);
cfsetospeed(&newtio,BAUDRATE);
printf("speed=%dn",BAUDRATE);
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
return(fd);
}
main()
{
int fd,n,i;
unsigned char buff[256];
fd = open_port();
while(1){
n = read(fd,buff,256);
if(n>0){
printf("n=%dn",n);
for(i=0;i