简体   繁体   English

通过串口发送文件

[英]sending a file over serial port

using this code, i am trying for sending file to CC430f5137 and receive back from it. 使用此代码,我正在尝试将文件发送到CC430f5137并从中接收回来。 I am using 2 programs, one is the below program( it is for sending and receiving back file to my linux VM and other one is program for the CC430f5137(it contains rx, tx code for my device which is in windows). I could able to send a single byte(for ex,A) from a file to my device but not multiple bytes. 我正在使用2个程序,一个是下面的程序(它用于向Linux VM发送和接收文件,另一个是CC430f5137的程序(它包含Windows中我设备的rx,tx代码)。我可以能够从文件向我的设备发送单个字节(例如,A),但不能发送多个字节。

//sample1.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/signal.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <termios.h>
#define BUFSIZE            1300 //1

int open_serial( char *dev_name, int baud, int vtime, int vmin )
{
    int fd;
    struct termios  newtio;

    fd = open( dev_name, O_RDWR | O_NOCTTY );
    if ( fd < 0 )
    {
        printf( "Device OPEN FAIL %s\n", dev_name );
        return  -1;
    }
    memset(&newtio, 0, sizeof(newtio));
    newtio.c_iflag = IGNPAR|INLCR;  // non-parity void UARTHandler(int fd);
    newtio.c_oflag = 0;
    newtio.c_cflag = CS8 | CLOCAL | CREAD; // NO-rts/cts

    switch( baud )
    {
        case 115200 : newtio.c_cflag |= B115200; break;
        case 57600  : newtio.c_cflag |= B57600;  break;
        case 38400  : newtio.c_cflag |= B38400;  break;
        case 19200  : newtio.c_cflag |= B19200;  break;
        case 9600   : newtio.c_cflag |= B9600;   break;
        case 4800   : newtio.c_cflag |= B4800;   break;
        case 2400   : newtio.c_cflag |= B2400;   break;
        default     : newtio.c_cflag |= B115200; break;
    }
    //set input mode (non-canonical, no echo,.....)
    newtio.c_lflag = 0;
    newtio.c_cc[VTIME] = vtime;
    newtio.c_cc[VMIN]  = vmin;
    tcflush  ( fd, TCIFLUSH );
    tcsetattr( fd, TCSANOW, &newtio );

    return fd;
}

int sendfile(int fd)      //void sendfile(int fd)
{
    int fd1,readc;
    unsigned short fileLength;
    unsigned char buf[BUFSIZE];

    fd1=open("cert.pem",O_RDONLY);

    fileLength=lseek(fd1,0,SEEK_END);
    printf("file length is %d bytes\n",fileLength);
    lseek(fd1,0,SEEK_SET);

    write(fd,(unsigned char*)&fileLength, 2);
           while(fileLength>0)
            {
                if(fileLength>=BUFSIZE)
                {
                    readc=read(fd1,buf,BUFSIZE);
                    if(readc==-1)
                    {
                    printf("read failed!!\n");
                    }
                    if(read>0)
                    {
                    buf[BUFSIZE]='\0';
                    printf("%s\n",buf);
                    if(write(fd,buf,BUFSIZE))
                    fileLength -= BUFSIZE;
                    }
                }
                else
                {
                    readc=read(fd1,buf,fileLength);
                    if(readc==-1)
                    {
                    printf("read failed!!\n");
                    }
                    if(readc>0)
                    {
                    buf[fileLength]='\0';
                    printf("%s\n",buf);
                    if(write(fd, buf, fileLength))
                    fileLength=0;
                    }
                }
            }
    printf("%s\n",fd);
    printf("file sent succssfully\n");
    return fd1;//close(fd1);
    close(fd);

}

void receivefile(int fd)
    {
        char buf[1300],read_byte,buf2[1300];
        int fd2,readc;
        unsigned short fileLength;

        fd2=open("test.pem", O_WRONLY|O_CREAT);

                if(fd2<0)
                {
                printf("file open failed!!\n");
                        }
                while(!(read(fd,(unsigned char*)&fileLength,2)>0));

                printf("i am in receive file\n");

                while(fileLength>0) 
                {
                    readc = read(fd, buf, BUFSIZE);
                    if(readc==-1)
                    {
                    printf("read failed!!\n");
                    }
                printf("read %d bytes, value is %s\n",readc,buf);

                    if(readc >0)
                    {
                    buf[readc]='\0';
                    printf("buf value is%s\n", buf);
                    write(fd2,buf,readc);
                    fileLength -= readc;
                    }
                }
            close(fd2);
            close(fd);
    }

void close_serial( int fd )
{
    close( fd );
    printf("ClosePort!!\n");
}

int  main( int argc, char **argv )
{
    int fd;
    int baud;
    char  dev_name[128];

    if ( argc != 3)
    {
        printf( " sample_serial [device] [baud]\n" \
               "    device : /dev/ttySAC0 ...\n"    \
               "    baud   : 2400 ... 115200\n" );
        return -1;
    }
    printf( " Serial test start...  (%s)\n", __DATE__ );

    strcpy( dev_name, argv[1] );
    baud    = strtoul( argv[2], NULL, 10 );

    fd = open_serial( dev_name, baud, 4, 1);

    sendfile(fd);
    receivefile(fd);        
    close_serial( fd );        
    printf( " Serial test end\n" );

    return  0;
}

this is output of sending 1 byte from the file to serial port 这是从文件向串行端口发送1个字节的输出

  [root@localhost ~]# gcc -std=c99 -std=gnu99 sample1.c -o out1
  sample1.c:151:50: warning: backslash and newline separated by space
  sample1.c:152:45: warning: backslash and newline separated by space
  [root@localhost ~]# ./out1 /dev/ttyUSB20 115200
  Serial test start...  (Mar  6 2013)
  file length is 2 bytes
  A
  wrote 3 bytes
  file sent succssfully
  i am in receive file
  read 1 bytes, value is A
  buf value isA
  read 1 bytes, value is A
  buf value isA
  read 1 bytes, value is A
  buf value isA
  read 1 bytes, value is A
  buf value isA

this is output of sending multiple bytes from the file to serial port 这是从文件向串行端口发送多个字节的输出

  [root@localhost ~]# gcc -std=c99 -std=gnu99 sample1.c -o out1
  sample1.c:151:50: warning: backslash and newline separated by space
  sample1.c:152:45: warning: backslash and newline separated by space
  [root@localhost ~]# ./out1 /dev/ttyUSB20 115200
  Serial test start...  (Mar  6 2013)
  file length is 2 bytes
  i am doing well today
  wrote 3 bytes
  file sent succssfully
  i am in receive file
  read 1 bytes, value is
  buf value is
  read 1 bytes, value is 
  buf value is
  read 1 bytes, value is 
  buf value is
  read 1 bytes, value is 
  buf value is

As you see here, i am not getting file contents, i am receving null characters back from my device, even though i sent multiple bytes to my device. 如您在此处看到的,即使我向设备发送了多个字节,我也没有收到文件内容,我正在从设备中获取空字符。

sendfile() is working properly,i can able to write "cert.pem" to port.I have a problem with reading file from port and writing back to "test.pem".I am getting output of some print statement given in the receiver program, but getting data continuously.flagsettings are in a proper way. sendfile()正常工作,我可以将“ cert.pem”写入端口。我从端口读取文件并将其写回到“ test.pem”时遇到问题。接收器程序,但要连续获取数据。标志设置是正确的。 I have reset all connections also. 我也重置了所有连接。

You are doing it wrong - instead of re-inventing a solution to the problem of sending binary file over serial links, use the already existing standard solutions, such as ZMODEM 您做错了-不用重新发明解决通过串行链接发送二进制文件问题的解决方案,而要使用现有的标准解决方案,例如ZMODEM

When you do, you wont have to write the Linux side program, since a free program to this already exists and you would be able to port an existing program to your CC430f5137, whatever that is. 当您这样做时,您将不必编写Linux端程序,因为已经有一个免费的程序,您可以将现有程序移植到CC430f5137,无论如何。

Let's start with the obvious. 让我们从显而易见的地方开始。

a : Sendfile doesn't have any looping construct in it... a: Sendfile中没有任何循环构造...

b: buffer overruns, like this: b:缓冲区溢出,如下所示:

unsigned char buf[BUFSIZE];
...
buf[BUFSIZE]=0

c: You have debug like this 'read 1 bytes, value is A' but it's not in the code. c:您已经进行了这样的调试:“读取1个字节,值为A”,但它不在代码中。 Difficult to fix code you haven't posted... 难以修复您尚未发布的代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM