udp数据如何通过ansi c中的RS232传输?
我想使用 udp 在 RS232 上传输和接收数据,并且我想了解哪些技术可以让我以更快的速率传输和接收数据,并且不会丢失数据?
提前致谢。我已经尝试过,但如果可能的话需要改进
#include <stdio.h>
#include <dos.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#define PORT1 0x3f8
void main()
{
int c,ch,choice,i,a=0;
char filename[30],filename2[30],buf;
FILE *in,*out;
clrscr();
while(1){
outportb(PORT1+0,0x03);
outportb(PORT1+1,0);
outportb(PORT1+3,0x03);
outportb(PORT1+2,0xc7);
outportb(PORT1+4,0x0b);
cout<<"\n===============================================================";
cout<<"\n\t*****Serial Communication By BADR-U-ZAMAN******\nCommunication between two computers By serial port";
cout<<"\nPlease select\n[1]\tFor sending file \n[2]\tFor receiving file \n[3]\tTo exit\n";
cout<<"=================================================================\n";
cin>>choice;
if(choice==1)
{
strcpy(filename,"C:\\TC\\BIN\\badr.cpp");
cout<<filename;
for(i=0;i<=strlen(filename);i++)
outportb(PORT1,filename[i]);
in=fopen(filename,"r");
if (in==NULL)
{ cout<<"cannot open a file";
a=1;
}
if(a!=1)
cout<<"\n\nFile sending.....\n\n";
while(!feof(in))
{
buf=fgetc(in);
cout<<buf;
outportb(PORT1,buf);
delay(5);
}
}
else
{
if(choice==3)
exit(0);
i=0;
buf='a';
while(buf!=NULL)
{
c=inportb(PORT1+5);
if(c&1)
{
buf=inportb(PORT1);
filename2[i]=buf;
i++;
}
}
out=fopen(filename2,"t");
cout<<"\n Filename received:"<<filename[2];
cout<<"\nReading from the port...";
cout<<"writing to file"<<filename2;
do
{
c=inportb(PORT1+5);
if(c&1)
{
buf=inportb(PORT1);
cout<<buf;
fputc(buf,out);
delay(5);
}
if(kbhit())
{
ch=getch();
}
}while(ch!=27);
}
getch();
}
}
i want to transmit and receive data on RS232 using udp and i want to know about techniques which allow me to transmit and receive data on a faster rate and also no lose of data is there?
thanx in advance. i have tried but need improvements if possible
#include <stdio.h>
#include <dos.h>
#include<string.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#define PORT1 0x3f8
void main()
{
int c,ch,choice,i,a=0;
char filename[30],filename2[30],buf;
FILE *in,*out;
clrscr();
while(1){
outportb(PORT1+0,0x03);
outportb(PORT1+1,0);
outportb(PORT1+3,0x03);
outportb(PORT1+2,0xc7);
outportb(PORT1+4,0x0b);
cout<<"\n===============================================================";
cout<<"\n\t*****Serial Communication By BADR-U-ZAMAN******\nCommunication between two computers By serial port";
cout<<"\nPlease select\n[1]\tFor sending file \n[2]\tFor receiving file \n[3]\tTo exit\n";
cout<<"=================================================================\n";
cin>>choice;
if(choice==1)
{
strcpy(filename,"C:\\TC\\BIN\\badr.cpp");
cout<<filename;
for(i=0;i<=strlen(filename);i++)
outportb(PORT1,filename[i]);
in=fopen(filename,"r");
if (in==NULL)
{ cout<<"cannot open a file";
a=1;
}
if(a!=1)
cout<<"\n\nFile sending.....\n\n";
while(!feof(in))
{
buf=fgetc(in);
cout<<buf;
outportb(PORT1,buf);
delay(5);
}
}
else
{
if(choice==3)
exit(0);
i=0;
buf='a';
while(buf!=NULL)
{
c=inportb(PORT1+5);
if(c&1)
{
buf=inportb(PORT1);
filename2[i]=buf;
i++;
}
}
out=fopen(filename2,"t");
cout<<"\n Filename received:"<<filename[2];
cout<<"\nReading from the port...";
cout<<"writing to file"<<filename2;
do
{
c=inportb(PORT1+5);
if(c&1)
{
buf=inportb(PORT1);
cout<<buf;
fputc(buf,out);
delay(5);
}
if(kbhit())
{
ch=getch();
}
}while(ch!=27);
}
getch();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请注意,许多操作系统会阻止对端口的直接访问。您必须编写专门的驱动程序才能访问它们。
如果您可以直接控制 RS232 端口引脚,则可以以编程方式调整速度。在大多数情况下,RS232 由 UART(或 USART)控制。该设备还控制速度(突发速率)。传输速度受到该设备的限制。例如,如果 UART 支持的最高速度是 9600 bps,那么您的程序就无法更快地传输数据。
您将需要优化您的程序,以便在每个 I/O 事务中传输尽可能多的内容。这是对通信渠道最有效的利用。一种常见的传输方法是使用一个线程从缓冲区传输数据。主线程将数据格式化到缓冲区中,然后向传输线程发出启动信号。这与使用 DMA 控制器类似。还要检查您的端口控制器是否具有块数据功能,这会使您的程序更加高效。
希望这有帮助。
Be aware the many operating systems block direct access to ports. You would have to write a specialized driver to access them.
If you can control the RS232 port pins directly, you may be able to adjust the speed programmatically. In most cases, RS232 is controlled by a UART (or USART). This device also controls the speed (burst rate). The transmission speed is limited by this device. For example, if the UART's top supported speed is 9600 bps, then your program cannot transmit data any faster.
You will want to optimize your program to transmit as much content as possible per I/O transaction. This is the most efficient use of the communications channel. A common method for transmitting is to have one thread that transmits data from a buffer. The main thread formats the data into the buffer, then signals the transmission thread to start. This is similar to using a DMA controller. Also check if your port controller has block data capabilities, which make your program more efficient.
Hope this helps.