c-是否可以通过RS-232增加数据接收大小



我正在使用此RS-232实现:https://www.teuniz.net/RS-232/在Windows操作系统上。我在C中编写了一个代码,其中一个读取输入文件并将其转发给另一个参与者。然而,根据这个实现,我最多可以将4096字节的数据加载到操作系统中。有可能增加这个数字吗?因为,例如,我发送了一个大小为5MB的文件,在9600波特下大约花了90分钟,但我不想增加波特

这是发件人的代码:

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif
// RS232 library
#include "rs232.h"
// Maximum size of data in block
#define INTERNALL_BUFFER 20480
// Size of static array
#define STATIC_ARRAY 1024
int main(int argc, char *argv[])
{
// Checking, if was the correct program parameters have been entered
if (argc < 2)
{
fprintf(stderr, "nIt was the incorrect program parameters have been enteredn");
fprintf(stderr, "Usage: test_tx file.suffixn");
return 1;
}
FILE *stream;
int cport_nr = 0,        /* /dev/ttyS0 (COM1 on windows) */
bdrate = 9600,       /* 9600 baud */
k = 1, fileSize = 0, /* k = counting, fileSize is size of data */
true_count = 0, again = 0,
sent_bytes = 0, begin = 0, last_block = 0,
sent_size = INTERNALL_BUFFER; /* Maximum transfer size in block is sent_size */
unsigned char convert[STATIC_ARRAY], *contents;
// 8N1 means eight databits, no parity, one stopbit
// If flowctrl is set to 0, no flow control is used
char mode[] = {'8', 'N', '1', 0};
size_t size;
// Open file from CLI, find the size of it
stream = fopen(argv[1], "rb");
fseek(stream, 0L, SEEK_END);
fileSize = ftell(stream);
fseek(stream, 0L, SEEK_SET);
// Allocate space for the entire file content
contents = (unsigned char *)malloc(fileSize + 1);
// Check if the memory has been successfully
// allocated by malloc or not
if (contents == NULL)
{
printf("Memory not allocated.n");
exit(0);
}
// Stream file into memory
size = fread(contents, 1, fileSize, stream);
contents[size] = 0;
fclose(stream);
// Counting the number of blocks
float count_blocks = fileSize / (float)INTERNALL_BUFFER;
count_blocks = ceil(count_blocks);
fprintf(stdout, "nThe count of blocks sent will be: %.0f", count_blocks);
// Opens the comport
if (RS232_OpenComport(cport_nr, bdrate, mode, 0))
{
printf("Can not open comportn");
return (0);
}
//////Je mozne upravit prijem a odosielanie dat do funkcii/funkcie...
// Convert int to char and send size
sprintf((char *)convert, "%d", fileSize);
printf("nSize of data is: %dn", fileSize);
int annouc = RS232_SendBuf(cport_nr, convert, STATIC_ARRAY);
if (annouc > 0)
printf("nThe size of the transmitted data was sentn");
#ifdef _WIN32
Sleep(5000);
#else
usleep(1000000);     /* sleep for  1 Seconds */
#endif
printf("n");
while (1)
{
int finish = 0;
// If transmission will be repetead
if (again)
{
begin = 0;
again--;
}
// If the size of the data being sent is less than INTERNALL_BUFFER
if (fileSize < sent_size)
sent_size = fileSize;
// Sends multiple bytes via the serial port
sent_bytes = RS232_SendBuf(cport_nr, contents + begin, sent_size);
if (sent_bytes < 0)
{
printf("Failed to send datan");
printf("Closed sending data and comportn");
break;
}
//  printf("%d.Sending: %d bytes.n %.*sn", k++, sent_bytes, sent_bytes,contents + begin);
printf("%d.Sending: %d bytesn", k++, sent_bytes);
true_count++;
begin += sent_bytes;
#ifdef _WIN32
Sleep(3000);
#else
usleep(1000000); /* sleep for  1 Seconds */
#endif
int help_block = true_count;
// If we want to send last less data than size of INTERNAL BUFFER
if (++help_block == count_blocks)
last_block++;
if ((begin % sent_size == 0) && (last_block) && (begin != fileSize))
sent_size = fileSize - begin;
// Checking if sending data was successful or no
while (begin == fileSize && true_count == (int)count_blocks)
{
int n = 0, result = 100;
unsigned char buf[STATIC_ARRAY];
n = RS232_PollComport(cport_nr, buf, STATIC_ARRAY);
if (n > 0)
{
printf("nReceiving: %d bytesn%.*sn", n, n, (char *)buf);
result = memcmp(buf, "Sending of data was successful :)n", n);
}
if (result == 0)
{
finish++;
memset(buf, 0, n);
break;
}
else if (result != 100)
{
memset(buf, 0, n);
again++;
}
}
if (finish)
break;
}
printf("Finish...n");
printf("Closed RS232...n");
// Free memory aloccated
free(contents);
// Closes the serial port
RS232_CloseComport(cport_nr);
return 0;
}

发送者读取输入文件,找到它的大小,将预期的大小发送给另一方,然后将文件发送给对方,知道文件有多大,并相应地跟进。问题是数据传输需要很长时间,第二件让我困扰的事情是,我想发送数据,但收件人必须能够以块的形式处理所有接收到的数据,我不希望数据丢失。如果我增加睡眠时间,发送数据不会丢失,但在我看来,发送时间非常长。

这是我在Windows上的RS-232应用程序,阅读:github.com/jv813yh/example_rs232_blocks/blob/master/demo_rx.c–

我将Baud提高到115200,但这是我第一次使用RS232,因此我需要帮助或建议。例如,我想传输一个几MB大小的文件,我应该设置什么波特率?哪些传输块可以避免缓冲区溢出?很抱歉,我问了一些愚蠢的问题,我也解决了TCP/IP,我不能完全致力于RS-232

正如其他答案和许多评论中所指出的,这无助于减少通信时间,这可能是您的真正目的,但您可以更改接收(和发送(缓冲区大小。

可以通过使用Win32API的这个功能来实现这一点
SetupComm函数(winbase.h(

[in] dwInQueue
设备内部输入缓冲区的建议大小,以字节为单位。

[in] dwOutQueue
设备内部输出缓冲区的建议大小,以字节为单位。

这个函数和它通知缓冲区实际大小的结构中会有信息
GetCommProperties函数(winbase.h(
COMMPROP结构(winbase.sh(

dwMaxRxQueue
驱动程序内部输入缓冲区的最大大小,以字节为单位。值为零表示串行提供程序没有施加最大值。

但是,如上所述,一些设备驱动程序可能没有缓冲区大小设置功能。

这可能不是一个好例子,但.NET SerialPort类允许您设置4096到2G字节的缓冲区(可能在.NET类实例中使用缓冲区,而不是设备驱动程序?(
SerialPort.ReadBufferSize属性

缓冲区大小,以字节为单位。默认值为4096;最大值是正整数,即2147483647。


顺便说一下,尽管文章的标题上写着接收大小,但文章的描述和提供的源代码只是发送它。
你到底需要设置哪个函数或缓冲区大小?


针对评论:

您尝试使用的RS232C库不适合您的用途
您可能选择了一个支持跨平台、易于使用、易于理解的平台,但您无法使用它来实现更多目标。

如果你想要真正准确、快速的沟通,你应该寻找一个更复杂、更灵活的库,或者从头开始开发自己的库
它不能通过简单的配置更改(如切换波特率(来实现。

它应该具有缓冲区大小设置、硬件流控制、异步完成通知等基本功能
除此之外,如果您希望在不丢失数据或属性的情况下快速传输文件,则需要实现类似ZMODEM的协议。

RS-232是一种面向比特的传输,因此9600波特定义了您每秒可以发送的比特数。更改缓冲(在您的示例中为4k(不会影响传输速度;只有缓冲液需要补充或冲洗的频率。

最新更新