⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serial_oksend.c

📁 linux下的人机对话编程
💻 C
字号:
#include <stdio.h>  
#include <sys/types.h>
#include <string.h>  
#include <unistd.h>  
#include <fcntl.h>   
#include <errno.h>   
#include <termios.h> 
#include <sys/wait.h>
#include <sys/ioctl.h>
#define BUFFER_SIZE 2048
/* These defination is from SLIP protocol. */
#define END             0300
#define ESC             0333
#define ESC_END         0334            
#define ESC_ESC         0335 
 
/* open the ttyS0 serial port */
int open_port(void)
{
      int fd; 
      fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY );
      if(fd==-1)
      {
	perror("open_port: Unable to open /dev/ttyS0 - ");
	return -1;
      }
      else
	fcntl(fd, F_SETFL, 0);
      return (fd);
 }

/* set up the options for serial port */
int setup_com(int fd){
    struct termios options; 
    tcgetattr(fd, &options);
    /*
     * Set the baud rates to 19200...
     */
    cfsetispeed(&options, B38400);
    cfsetospeed(&options, B38400);

    /*
     * Enable the receiver and set local mode...
     */

    options.c_cflag |= (CLOCAL | CREAD);

    /*
     * Set global options.
     */
    options.c_cflag |= PARENB;
    options.c_cflag &= ~PARODD;
    options.c_cflag &= ~CSTOPB;
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;
    
    /* set the input options */

    options.c_iflag &=~(IXON | IXOFF | IXANY);
    options.c_iflag &=~(INLCR | IGNCR | ICRNL);
    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

    /* set the output options */
    options.c_oflag &= ~OPOST;   

    /* set the timeout options */
    options.c_cc[VMIN]  = 0;
    options.c_cc[VTIME] = 10;

    tcsetattr(fd, TCSANOW, &options);
    return 1;



}

/*
 * pack the frame and prepare for sending the packets.
 */
int frame_pack(unsigned char *src, unsigned char *des, int len)
{
    unsigned char *ptr = des;
    unsigned char c;
    *ptr++ = END;                                                                                               
    while (len-->0){
        switch(c=*src++){
            case END:
                *ptr++=ESC;
                *ptr++=ESC_END;
            break;
            case ESC:
                *ptr++=ESC;
                *ptr++=ESC_ESC;
            break;
            default:
                *ptr++=c;
                break;
           }
       }
       *ptr++=END;
       return(ptr-des);
}

/*
 * unpack the frame.
 */
int frame_unpack(unsigned char *src, unsigned char *des)
{
    unsigned char *ptr = des;
    unsigned char c;
    int count = 0;
    int esc_flag = 0;
    if(*src++!=END)
        return 0;
    while(((c=*src++)!=END)&&(count<BUFFER_SIZE))
    {
        switch(c){
            case ESC_END:
                if(esc_flag==1){
                   *ptr++ = END;
                   esc_flag = 0;
                } 
                else
                   *ptr++=c;
                break;
            case ESC_ESC:
                if(esc_flag ==1){
                    *ptr++=ESC;
                    esc_flag=0;
                }else
                    *ptr++=c;
                break;
            case END:
                esc_flag=0; 
                break;
            case ESC:
                esc_flag=1;
 		break;	
            default:
                *ptr++=c;  
                esc_flag=0;              
                break;
        } 
    }
    if(count>= BUFFER_SIZE){
        printf("some error happen in frame unpack().\n");
        return BUFFER_SIZE;
    }
    return (ptr-des);

}
/*void p()
{
	printf("iokdsadsad\n");
}*/
int main(int argc ,char *argv[]){
    int fd;
    unsigned char buffer_tx[BUFFER_SIZE]="12345678 0x8001 0x8002 0x8003 0x8004 0x8005 0x22 0x55 0x33 0x44 0x8008";
    unsigned char buffer_tx_pack[BUFFER_SIZE];
    unsigned char *tx_ptr;
    int flag;
    int tx_length;
    int i;
	int length;
    tx_ptr = buffer_tx;
    int tx_count;
    fd = open_port();
    if(fd < 0)
        return 0;
    setup_com(fd);
        for (;;)
        {
	   	tx_ptr=buffer_tx;
                tx_count=frame_pack(tx_ptr,buffer_tx_pack,strlen(buffer_tx));   
                printf("\ntx_count=%d\n",tx_count);
		printf("%s\n",buffer_tx_pack);
              //  for(i=0;i<tx_count;i++)
                //    printf("%",buffer_tx_pack[i]);
		//	printf("%c",buffer_tx_pack[i]);
                tx_length=0;
                tx_ptr=buffer_tx_pack;
                while(tx_length<tx_count)
		{
	           length=write(fd,tx_ptr,tx_count-tx_length);
                   tx_length=tx_length+length;
                   tx_ptr+=tx_length;
     sleep(1);
                }                   
            }
     close(fd);
}

⌨️ 快捷键说明

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