开发者

how can udp data can passed through RS232 in ansi c?

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<<file开发者_Python百科name;
    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();
 }
 }


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜