C 通过串行端口发送文件
我需要一些通过串行连接发送文件的帮助。我有两条RS232到USB的电缆,我正在测试我的代码,一条用来发送数据,另一条用来接收数据。我让他们两人在身体上互相联系 因此,我编写了一些改编自一些源代码的代码,我可以成功地传输一系列字符。一个程序接收数据,另一个程序发送数据。我在两个独立的终端中打开了这两个。找到下面的两段代码: serialout.cC 通过串行端口发送文件,c,linux,file,serial-port,C,Linux,File,Serial Port,我需要一些通过串行连接发送文件的帮助。我有两条RS232到USB的电缆,我正在测试我的代码,一条用来发送数据,另一条用来接收数据。我让他们两人在身体上互相联系 因此,我编写了一些改编自一些源代码的代码,我可以成功地传输一系列字符。一个程序接收数据,另一个程序发送数据。我在两个独立的终端中打开了这两个。找到下面的两段代码: serialout.c #include <stdio.h> /* Standard input/output definitions */ #include
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int main()
{
//writing
int writeport = open_port("/dev/ttyUSB0");
char str[] = "hello how are you?";
int n = write(writeport, str, strlen(str));
if (n < 0)
fputs("write() of bytes failed!\n", stderr);
//closing ports
close(writeport);
}
int open_port(char str[])
{
int fd = open(str, O_RDWR | O_NOCTTY | O_NONBLOCK); // ?? NDELAY or NONBLOCK?
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options); //this gets the current options set for the port
// setting the options
cfsetispeed(&options, B9600); //input baudrate
cfsetospeed(&options, B9600); // output baudrate
options.c_cflag |= (CLOCAL | CREAD); // ?? enable receicer and set local mode
//options.c_cflag &= ~CSIZE; /* mask the character size bits */
options.c_cflag |= CS8; /* select 8 data bits */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // choosing raw input
options.c_iflag &= ~INPCK; // disable parity check
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
options.c_oflag |= OPOST; // ?? choosing processed output
options.c_cc[VMIN] = 0; // Wait until x bytes read (blocks!)
options.c_cc[VTIME] = 0; // Wait x * 0.1s for input (unblocks!)
// settings for no parity bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options); //set the new options ... TCSANOW specifies all option changes to occur immediately
return (fd);
}
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int main()
{
//reading
int readport = open_port("/dev/ttyUSB1");
//trying to read one character at a time
char buff;
int n = 1;
while (n > 0)
{
n = read(readport, &buff, 1);
printf("%c", buff, buff);
}
printf("\n");
//closing ports
close(readport);
}
int open_port(char str[])
{
int fd = open(str, O_RDWR | O_NOCTTY | O_NONBLOCK); // ?? NDELAY or NONBLOCK?
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options); //this gets the current options set for the port
// setting the options
cfsetispeed(&options, B9600); //input baudrate
cfsetospeed(&options, B9600); // output baudrate
options.c_cflag |= (CLOCAL | CREAD); // ?? enable receicer and set local mode
//options.c_cflag &= ~CSIZE; /* mask the character size bits */
options.c_cflag |= CS8; /* select 8 data bits */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // choosing raw input
options.c_iflag &= ~INPCK; // disable parity check
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
options.c_oflag |= OPOST; // ?? choosing processed output
options.c_cc[VMIN] = 0; // Wait until x bytes read (blocks!)
options.c_cc[VTIME] = 0; // Wait x * 0.1s for input (unblocks!)
// settings for no parity bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options); //set the new options ... TCSANOW specifies all option changes to occur immediately
return (fd);
}
#包括/*标准输入/输出定义*/
#include/*字符串函数定义*/
#include/*UNIX标准函数定义*/
#包含/*文件控制定义*/
#包括/*错误号定义*/
#包括/*POSIX终端控制定义*/
int main()
{
//书写
int writeport=open_端口(“/dev/ttyUSB0”);
char str[]=“你好吗?”;
int n=写入(writeport,str,strlen(str));
if(n<0)
fputs(“写入()字节失败!\n”,stderr);
//关闭端口
关闭(写端口);
}
int open_端口(字符str[])
{
int fd=打开(str,O|RDWR | O|NOCTTY | O|U NONBLOCK);/?
如果(fd==-1)
{
perror(“打开_端口:无法打开/dev/ttyS0-”;
}
其他的
fcntl(fd,F_SETFL,0);
结构termios选项;
tcgetattr(fd,&options);//这将获取端口的当前选项集
//设置选项
cfsetispeed(&options,B9600);//输入波特率
cfsetospeed(&options,B9600);//输出波特率
options.c|u cflag |=(CLOCAL | CREAD);/?启用接收器并设置本地模式
//options.c_cflag&=~CSIZE;/*屏蔽字符大小位*/
options.c_cflag |=CS8;/*选择8个数据位*/
options.c|lflag&=~(ICANON | ECHO | ECHO | ISIG)//选择原始输入
options.c_iflag&=~INPCK;//禁用奇偶校验
options.c|u iflag&=~(IXON | IXOFF | IXANY);//禁用软件流控制
options.c|u of lag |=OPOST;//?选择已处理的输出
options.c_cc[VMIN]=0;//等待读取x字节(块!)
options.c_cc[VTIME]=0;//等待x*0.1s输入(取消阻止!)
//无奇偶校验位的设置
options.c_cflag&=~PARENB;
options.c_cflag&=~CSTOPB;
options.c_cflag&=~CSIZE;
选项c|u cflag |=CS8;
tcsettr(fd、TCSANOW和options);//设置新选项…TCSANOW指定立即发生所有选项更改
返回(fd);
}
serialin.c
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int main()
{
//writing
int writeport = open_port("/dev/ttyUSB0");
char str[] = "hello how are you?";
int n = write(writeport, str, strlen(str));
if (n < 0)
fputs("write() of bytes failed!\n", stderr);
//closing ports
close(writeport);
}
int open_port(char str[])
{
int fd = open(str, O_RDWR | O_NOCTTY | O_NONBLOCK); // ?? NDELAY or NONBLOCK?
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options); //this gets the current options set for the port
// setting the options
cfsetispeed(&options, B9600); //input baudrate
cfsetospeed(&options, B9600); // output baudrate
options.c_cflag |= (CLOCAL | CREAD); // ?? enable receicer and set local mode
//options.c_cflag &= ~CSIZE; /* mask the character size bits */
options.c_cflag |= CS8; /* select 8 data bits */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // choosing raw input
options.c_iflag &= ~INPCK; // disable parity check
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
options.c_oflag |= OPOST; // ?? choosing processed output
options.c_cc[VMIN] = 0; // Wait until x bytes read (blocks!)
options.c_cc[VTIME] = 0; // Wait x * 0.1s for input (unblocks!)
// settings for no parity bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options); //set the new options ... TCSANOW specifies all option changes to occur immediately
return (fd);
}
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
int main()
{
//reading
int readport = open_port("/dev/ttyUSB1");
//trying to read one character at a time
char buff;
int n = 1;
while (n > 0)
{
n = read(readport, &buff, 1);
printf("%c", buff, buff);
}
printf("\n");
//closing ports
close(readport);
}
int open_port(char str[])
{
int fd = open(str, O_RDWR | O_NOCTTY | O_NONBLOCK); // ?? NDELAY or NONBLOCK?
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS0 - ");
}
else
fcntl(fd, F_SETFL, 0);
struct termios options;
tcgetattr(fd, &options); //this gets the current options set for the port
// setting the options
cfsetispeed(&options, B9600); //input baudrate
cfsetospeed(&options, B9600); // output baudrate
options.c_cflag |= (CLOCAL | CREAD); // ?? enable receicer and set local mode
//options.c_cflag &= ~CSIZE; /* mask the character size bits */
options.c_cflag |= CS8; /* select 8 data bits */
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // choosing raw input
options.c_iflag &= ~INPCK; // disable parity check
options.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
options.c_oflag |= OPOST; // ?? choosing processed output
options.c_cc[VMIN] = 0; // Wait until x bytes read (blocks!)
options.c_cc[VTIME] = 0; // Wait x * 0.1s for input (unblocks!)
// settings for no parity bit
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options); //set the new options ... TCSANOW specifies all option changes to occur immediately
return (fd);
}
#包括/*标准输入/输出定义*/
#include/*字符串函数定义*/
#include/*UNIX标准函数定义*/
#包含/*文件控制定义*/
#包括/*错误号定义*/
#包括/*POSIX终端控制定义*/
int main()
{
//阅读
int readport=open_port(“/dev/ttyUSB1”);
//试图一次读一个字符
半焦;
int n=1;
而(n>0)
{
n=读取(readport和buff,1);
printf(“%c”,浅黄色,浅黄色);
}
printf(“\n”);
//关闭端口
关闭(读取端口);
}
int open_端口(字符str[])
{
int fd=打开(str,O|RDWR | O|NOCTTY | O|U NONBLOCK);/?
如果(fd==-1)
{
perror(“打开_端口:无法打开/dev/ttyS0-”;
}
其他的
fcntl(fd,F_SETFL,0);
结构termios选项;
tcgetattr(fd,&options);//这将获取端口的当前选项集
//设置选项
cfsetispeed(&options,B9600);//输入波特率
cfsetospeed(&options,B9600);//输出波特率
options.c|u cflag |=(CLOCAL | CREAD);/?启用接收器并设置本地模式
//options.c_cflag&=~CSIZE;/*屏蔽字符大小位*/
options.c_cflag |=CS8;/*选择8个数据位*/
options.c|lflag&=~(ICANON | ECHO | ECHO | ISIG)//选择原始输入
options.c_iflag&=~INPCK;//禁用奇偶校验
options.c|u iflag&=~(IXON | IXOFF | IXANY);//禁用软件流控制
options.c|u of lag |=OPOST;//?选择已处理的输出
options.c_cc[VMIN]=0;//等待读取x字节(块!)
options.c_cc[VTIME]=0;//等待x*0.1s输入(取消阻止!)
//无奇偶校验位的设置
options.c_cflag&=~PARENB;
options.c_cflag&=~CSTOPB;
options.c_cflag&=~CSIZE;
选项c|u cflag |=CS8;
tcsettr(fd、TCSANOW和options);//设置新选项…TCSANOW指定立即发生所有选项更改
返回(fd);
}
所以我想做的是发送一个文件。例如,一个jpeg文件。有没有办法将它转换成字节码并重新组装成jpeg格式?我四处搜索,但不幸的是,几乎没有找到相关信息——也许我搜索的是错误的术语
更好的做法是在文件压缩后发送。最后,我使用gzip压缩jpeg文件,然后通过串行连接发送它们。谢谢大家 如果要传输文件,应将文件分成块,并在每个块上使用校验和;然后在重新连接另一侧的块时验证校验和 这不是一个新问题。其他人已经为你解决了这个问题。您应该获得一个现有的可靠文件传输程序,并通过串行链接运行它 首选是rsync。这就是GPL,因此如果您从事专有工作,许可证可能会阻止您使用它 另一个好的选择是XMODEM(或YMODEM或ZMODEM)。我发现了一个带有BSD许可证的XMODEM的C实现,因此您可以肯定地使用它。这也比rsync更小更简单
我还打算建议使用Z-Modem或X-Modem进行文件传输。但你要明白的是,文件没有什么特别之处。就传输计算机和接收终端而言,发送的文件只是