serial.c

来自「vxworks中串口操作」· C语言 代码 · 共 69 行

C
69
字号
#include "vxworks.h"
#include "stdio.h"
#include "ioLib.h"
#include "sioLib.h"
int fd;
char buf[100];
void OpenS(int num)
{
	char tyName[20];
	char consoleName[20];
    
    	sprintf (tyName, "%s%d", "/tyCo/", num);
    	strcpy (consoleName, tyName);
    	
        fd=open(consoleName,O_RDWR,0);
	if(fd<0)
		printf("open serial error\n");
	else
		printf("open serial succeed\n");
        ioctl(fd,FIOSETOPTIONS,OPT_RAW);
        ioctl(fd,FIOBAUDRATE,9600);
	ioctl(fd,SIO_HW_OPTS_SET,CS8);
}

int tty_receive()
{
    int width;
    int bytes_in;
    struct fd_set readFds;

    OpenS(1);
    
    FD_ZERO(&readFds);   
    FD_SET(fd,&readFds);
    width=fd+1;
    if(select(width,&readFds,NULL,NULL,NULL)==ERROR)
          return(ERROR);
    if(FD_ISSET(fd,&readFds))
    {
    	while(1)
    	{
    		while((bytes_in=read(fd,buf,100))>0)
    	        {
		         SetT(buf);
    	        }
    	}
    }
    close(fd);
    return 1;
}

int app()
{
	int taskIdOne;
	taskIdOne = taskSpawn("t1",90,0x100,2000,(FUNCPTR)tty_receive,0,0,0,0,0,0,0,0,0,0);
}


int tty_send(char* buff)
{
    int bytes_out;
    OpenS(1);
    bytes_out=write(fd,buff,strlen(buff));
    printf("output chars total: %d\n",bytes_out);
    close(fd);
    return 1;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?