- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想用下面的代码发送和接收文件。但函数有错误,“xmodem_receive”。
“xmodem_send”函数通过“tty/AMA0”发送“xmodem_chunk”,接收数据为“xmodem_receive”中的“ret = saferead(fd, &chunk, sizeof(chunk))”但是这一行没有读取数据!相反,这一行停止了我的代码。有人教我至少需要运行这段代码吗?
这是代码(略)。
#define X_STX 0x02
#define X_EOT 0x04
#define X_ACK 0x06
#define X_NAK 0x15
#define min(a, b) ((a) < (b) ? (a) : (b))
#define DEBUG 0
struct xmodem_chunk {
uint8_t start;
uint8_t blk;
uint8_t blk_neg;
uint8_t payload[1024];
uint16_t crc;
} __attribute__((packed));
#define CRC_POLY 0x1021
int saferead(int fd, const void *p, size_t want){
int ret;
int ret_sum = 0;
errno = 0;
while (want){
ret = read(fd, (uint8_t*)p, want);
if(ret == 0)
return -1; /* EOF */
if(ret <= 0){
if(errno != EINTR && errno != EAGAIN ) {
return -1;
}
errno = 0;
continue;
}
want -= ret;
p = (uint8_t*) p + ret;
ret_sum += ret;
}
return ret_sum;
}
static int xmodem_send(int serial_fd, const char *filename)
{
int ret, fd;
uint8_t eof = X_EOT;
struct xmodem_chunk chunk;
fd = open(filename, O_RDONLY);
if(fd < 0){
perror("open");
return -errno;
}
fstat(fd, &stat);
len = stat.st_size;
buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
if(!buf){
perror("mmap");
return -errno;
}
printf("Sending %s \n", filename);
chunk.start = X_STX;
while (len){
size_t z = 0;
int next = 0;
char status;
z = min(len, sizeof(chunk.payload));
memcpy(chunk.payload, buf, z);
memset(chunk.payload + z, 0xff, sizeof(chunk.payload) - z);
chunk.crc = swap16(crc16(chunk.payload, sizeof(chunk.payload)));
chunk.blk_neg = 0xff - chunk.blk;
int i;
for(i = 0; i < 3; i++){
ret = safewrite(serial_fd, &chunk, sizeof(chunk));
if(ret != sizeof(chunk)){
return -errno;
}
}
if(next){
chunk.blk++;
len -= z;
buf += z;
}
}
ret = safewrite(serial_fd, &eof, sizeof(eof));
if(ret != sizeof(eof))
return -errno;
return 0;
}
static xmodem_receive(int serial_fd, char* filename){
size_t len;
int ret, fd;
size_t retry;
struct xmodem_chunk chunk;
struct stat stat;
FILE *fp;
uint8_t eof = X_EOT;
int eof_count=0;
retry = 3;
fp = fopen(filename, "ab+");
while(1){
int garbage_count=0;
int correct_count=0;
uint8_t chunk_payload[1024];
uint8_t garbage_payload[3][1024];
uint16_t garbage_crc[3];
size_t z = 0;
while(retry){
int next = 0;
ret = saferead(fd, &chunk, sizeof(chunk)); <--This line is the problem.
z = sizeof(chunk.payload);
if(chunk.start != X_STX){
printf("error STX\n");
return 0; // error
}
printf("retry part\n");
if(chunk.crc != swap16(crc16(chunk.payload, sizeof(chunk.payload)))){
if(garbage_count > 1){
int i;
for(i=0; i < garbage_count; i++){
if(chunk.crc == swap16(crc16(garbage_payload[i], sizeof(chunk.payload)))){
correct_count++;
memcpy(chunk_payload, garbage_payload[i], len);
}
}
if(correct_count < 1){
memcpy(garbage_payload[garbage_count], &chunk.payload, len);
garbage_count++;
}
printf("garbage#1\n");
}else{
memcpy(garbage_payload[0], &chunk.payload, len);
garbage_crc[0] = chunk.crc;
garbage_count++;
printf("garbage#2\n");
}
}else{
printf("correct\n");
correct_count++;
memcpy(chunk_payload , &chunk.payload, len);
}
}
safewrite(fd, &chunk_payload, z);
}
close(fd);
return 0;
}
int main(int argc, char **argv){
int a, ret, serial_fd;
serial_fd = open_serial("/dev/ttyUSB0", 115200);
// serial_fd = open_serial("/dev/ttyAMA0", 115200);
ret = xmodem_receive(serial_fd, "sample.jpg");
// ret = xmodem_send(serial_fd, "sample.jpg");
}
完整代码(抱歉让您久等了)
`
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#define X_STX 0x02
#define X_EOT 0x04
#define X_ACK 0x06
#define X_NAK 0x15
#define min(a, b) ((a) < (b) ? (a) : (b))
#define DEBUG 0
struct xmodem_chunk {
uint8_t start;
uint8_t blk;
uint8_t blk_neg;
uint8_t payload[1024];
uint16_t crc;
} __attribute__((packed));
#define CRC_POLY 0x1021
/* 'Safe' write */
int safewrite(int fd, const void *p, size_t want){
int ret;
int ret_sum = 0;
errno = 0;
while(want){
ret = write(fd, (uint8_t *)p,want);
if(ret <= 0) {
if (errno != EINTR && errno != EAGAIN){
return -1;
}
errno = 0;
continue;
}
want -= ret;
p = (uint8_t*) p + ret;
ret_sum += ret;
}
return ret_sum;
}
int saferead(int fd, const void *p, size_t want){
int ret;
int ret_sum = 0;
errno = 0;
while (want){
ret = read(fd, (uint8_t*)p, want);
if(ret == 0)
return -1; /* EOF */
if(ret <= 0){
if(errno != EINTR && errno != EAGAIN ) {
return -1;
}
errno = 0;
continue;
}
want -= ret;
p = (uint8_t*) p + ret;
ret_sum += ret;
}
return ret_sum;
}
static uint16_t crc_update(uint16_t crc_in, int incr)
{
uint16_t xor = crc_in >> 15;
uint16_t out = crc_in << 1;
if(incr)
out++;
if(xor)
out ^= CRC_POLY; // xor 0b1000000100001
return out;
}
static uint16_t crc16(const uint8_t *data, uint16_t size)
{
uint16_t crc, i;
for(crc = 0; size > 0; size--, data++)
for(i = 0x80; i; i >> 1)
crc = crc_update(crc, *data & i);
for ( i = 0; i < 16; i++)
crc = crc_update(crc, 0);
return crc;
}
static uint16_t swap16(uint16_t in)
{
return (in >> 8) | ((in & 0xff) << 8);
}
enum {
PROTOCOL_XMODEM,
PROTOCOL_YMODEM,
};
static int xymodem_send(int serial_fd, const char *filename)
{
size_t len;
int ret, fd;
uint8_t answer;
struct stat stat;
const uint8_t *buf;
uint8_t eof = X_EOT;
struct xmodem_chunk chunk;
int skip_payload = 0;
fd = open(filename, O_RDONLY);
if(fd < 0){
perror("open");
return -errno;
}
fstat(fd, &stat);
len = stat.st_size;
buf = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
if(!buf){
perror("mmap");
return -errno;
}
if(DEBUG) {
printf("Wainting for receiver ping ...");
fflush(stdout);
do {
ret = read(serial_fd, &answer, sizeof(answer));
if(ret != sizeof(answer)){
perror("read");
return -errno;
}
}while (answer != 'C');
printf("dome.\n");
}
printf("Sending %s ", filename);
/*
if(protocol == PROTOCOL_YMODEM) {
strncpy ((char*) chunk.payload, filename, sizeof(chunk.payload));
chunk.blk = 0;
skip_payload = 1;
}else {
chunk.blk = 1;
}
*/
chunk.start = X_STX;
while (len){
size_t z = 0;
int next = 0;
char status;
z = min(len, sizeof(chunk.payload));
memcpy(chunk.payload, buf, z);
memset(chunk.payload + z, 0xff, sizeof(chunk.payload) - z); //
chunk.crc = swap16(crc16(chunk.payload, sizeof(chunk.payload)));
chunk.blk_neg = 0xff - chunk.blk;
int i;
for(i = 0; i < 3; i++){
ret = safewrite(serial_fd, &chunk, sizeof(chunk));
if(ret != sizeof(chunk)){
return -errno;
}
}
if(next){
chunk.blk++;
len -= z;
buf += z;
}
}
ret = safewrite(serial_fd, &eof, sizeof(eof));
if(ret != sizeof(eof))
return -errno;
ret = safewrite(serial_fd, &eof, sizeof(eof));
if (ret != sizeof(eof))
return -errno;
ret = safewrite(serial_fd, &eof, sizeof(eof));
if (ret != sizeof(eof))
return -errno;
printf("done.\n");
return 0;
}
static xmodem_receive(int serial_fd, char* filename){
size_t len;
int ret, fd;
size_t retry;
uint8_t expected_blkno;
size_t nrecv;
uint16_t crch, crcl;
size_t datalen;
struct xmodem_chunk chunk;
struct stat stat;
int file_size=0;
int c;
FILE *fp;
char* exet;
uint8_t send_start;
uint8_t eof = X_EOT;
int eof_count=0;
retry = 3;
fp = fopen(filename, "ab+");
while(1){
int v_stx_count=0;
int inv_stx_count=0;
int garbage_blk_count=0;
int correct_blk_count=0;
int garbage_count=0;
int correct_count=0;
uint8_t chunk_payload[1024];
uint8_t garbage_payload[3][1024];
uint16_t garbage_crc[3];
int val_payload_crc_pr = 0;
int val_payload_crc_cr = 0;
int val_payload_crc_nx = 0;
int pr_val_crc=0;
int nx_val_crc=0;
int val_crc=0;
size_t z = 0;
while(retry){
int next = 0;
ret = saferead(fd, &chunk, 1);
printf("read chunk");
z = sizeof(chunk.payload);
if(chunk.start != X_STX){
printf("error STX\n");
return 0; // error
}
printf("retry part\n");
if(chunk.crc != swap16(crc16(chunk.payload, sizeof(chunk.payload)))){
if(garbage_count > 1){
int i;
for(i=0; i < garbage_count; i++){
if(chunk.crc == swap16(crc16(garbage_payload[i], sizeof(chunk.payload)))){
correct_count++;
memcpy(chunk_payload, garbage_payload[i], len);
}
}
if(correct_count < 1){
memcpy(garbage_payload[garbage_count], &chunk.payload, len);
garbage_count++;
}
printf("garbage#1\n");
}else{
// ごみの登録
memcpy(garbage_payload[0], &chunk.payload, len);
garbage_crc[0] = chunk.crc;
garbage_count++;
printf("garbage#2\n");
}
}else{
printf("correct\n");
correct_count++;
memcpy(chunk_payload , &chunk.payload, len);
}
}
safewrite(fd, &chunk_payload, z);
}
close(fd);
return 0;
}
static int open_serial(const char *path, int baud)
{
int fd;
struct termios tty;
fd = open(path, O_RDWR | O_SYNC);
if(fd < 0) {
perror("open");
return -errno;
}
memset(&tty, 0, sizeof(tty));
if(tcgetattr(fd, &tty) != 0) {
perror("tcgetattr");
return -errno;
}
cfsetospeed(&tty, baud);
cfsetispeed(&tty, baud);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // nosignaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controlsm
tty.c_cflag &= ~(PARENB | PARODD); // ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off party
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
perror("tcsetattr");
return -errno;
}
return fd;
}
static void dump_serial(int serial_fd)
{
char in;
for (;;) {
read(serial_fd, &in, sizeof(in));
printf("%c", in);
fflush(stdout);
}
}
int main(int argc, char **argv){
int a, ret, serial_fd;
serial_fd = open_serial("/dev/ttyUSB0", 115200);
ret = xmodem_receive(serial_fd, "sample.jpg");
// dump_serial(serial_fd);
}
`
最佳答案
您的程序从不接收任何数据,因为它从不尝试使用串行端口执行 I/O。xmodem_receive() 从不使用其参数 serial_fd
。
static xmodem_receive(int serial_fd, char* filename){
相反,此例程使用未初始化的变量 fd
作为调用 saferead() 和 safewrite() 的参数。
int ret, fd;
...
ret = saferead(fd, &chunk, 1);
...
safewrite(fd, &chunk_payload, z);
要复合此错误,您不必费心去检查这些调用的返回值,因此由于使用未初始化的变量(即无效的文件描述符)而导致的任何错误都会被悄无声息地丢弃。
关于c - 无法从串口获取数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41282541/
我目前正在尝试基于哈希表构建字典。逻辑是:有一个名为 HashTable 的结构,其中包含以下内容: HashFunc HashFunc; PrintFunc PrintEntry; CompareF
如果我有一个指向结构/对象的指针,并且该结构/对象包含另外两个指向其他对象的指针,并且我想删除“包含这两个指针的对象而不破坏它所持有的指针”——我该怎么做这样做吗? 指向对象 A 的指针(包含指向对象
像这样的代码 package main import "fmt" type Hello struct { ID int Raw string } type World []*Hell
我有一个采用以下格式的 CSV: Module, Topic, Sub-topic 它需要能够导入到具有以下格式的 MySQL 数据库中: CREATE TABLE `modules` ( `id
通常我使用类似的东西 copy((uint8_t*)&POD, (uint8_t*)(&POD + 1 ), back_inserter(rawData)); copy((uint8_t*)&PODV
错误 : 联合只能在具有兼容列类型的表上执行。 结构(层:字符串,skyward_number:字符串,skyward_points:字符串)<> 结构(skyward_number:字符串,层:字符
我有一个指向结构的指针数组,我正在尝试使用它们进行 while 循环。我对如何准确初始化它并不完全有信心,但我一直这样做: Entry *newEntry = malloc(sizeof(Entry)
我正在学习 C,我的问题可能很愚蠢,但我很困惑。在这样的函数中: int afunction(somevariables) { if (someconditions)
我现在正在做一项编程作业,我并没有真正完全掌握链接,因为我们还没有涉及它。但是我觉得我需要它来做我想做的事情,因为数组还不够 我创建了一个结构,如下 struct node { float coef;
给定以下代码片段: #include #include #define MAX_SIZE 15 typedef struct{ int touchdowns; int intercepti
struct contact list[3]; int checknullarray() { for(int x=0;x<10;x++) { if(strlen(con
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: Empty “for” loop in Facebook ajax what does AJAX call
我刚刚在反射器中浏览了一个文件,并在结构构造函数中看到了这个: this = new Binder.SyntaxNodeOrToken(); 我以前从未见过该术语。有人能解释一下这个赋值在 C# 中的
我经常使用字符串常量,例如: DICT_KEY1 = 'DICT_KEY1' DICT_KEY2 = 'DICT_KEY2' ... 很多时候我不介意实际的文字是什么,只要它们是独一无二的并且对人类读
我是 C 的新手,我不明白为什么下面的代码不起作用: typedef struct{ uint8_t a; uint8_t* b; } test_struct; test_struct
您能否制作一个行为类似于内置类之一的结构,您可以在其中直接分配值而无需调用属性? 前任: RoundedDouble count; count = 5; 而不是使用 RoundedDouble cou
这是我的代码: #include typedef struct { const char *description; float value; int age; } swag
在创建嵌套列表时,我认为 R 具有对列表元素有用的命名结构。我有一个列表列表,并希望应用包含在任何列表中的每个向量的函数。 lapply这样做但随后剥离了列表的命名结构。我该怎么办 lapply嵌套列
我正在做一个用于学习目的的个人组织者,我从来没有使用过 XML,所以我不确定我的解决方案是否是最好的。这是我附带的 XML 文件的基本结构:
我是新来的 nosql概念,所以当我开始学习时 PouchDB ,我找到了这个转换表。我的困惑是,如何PouchDB如果可以说我有多个表,是否意味着我需要创建多个数据库?因为根据我在 pouchdb
我是一名优秀的程序员,十分优秀!