can anyone guide me how can I send and receive (transfer) files between 2 computers using RS232...pls also give some link about the basics because I have no background of this concept nor any good material
Thanks,
Edesign
Printable View
can anyone guide me how can I send and receive (transfer) files between 2 computers using RS232...pls also give some link about the basics because I have no background of this concept nor any good material
Thanks,
Edesign
Also, one more question..where can I learn VB? Is there any forum of this group related to VB??
Thanks,
Edesign
The principle of serial communication is pretty simple, but there is no definite portable standard, so it would depend on several things....
What hardware/OS are you using?
Do you have a particular protocol in mind?
As to VB, I'm sure there are people on the board that knows VB, but there is no board on this site for VB discussions in particular.
--
Mats
basically, i want to transfer a file between 2 pcs using an RF module(or an optical fiber)..I wrote a program using outportb which sent characters but now need to do it with entire file...i will be using windows xp and at college I have IBM machine...the hardware part is already available(for some other purpose we made it actually)...but have no idea about the software...where to start with..
So, you have a PC running XP and an IBM <what architecture> running <what OS>?
On the PC side, you could open the serial port with CreateFile().
--
Mats
Hello,
I actually wrote a small program, but now the trouble is...I am using inport and outport in my program and I have included <dos.h>...still compiling it in Visual studio gives an error that these functions are not defined...
Edesign
If you're actually using RS232...
http://msdn2.microsoft.com/en-us/library/ms810467.aspx
gg
Oh my God...It seems so tough to me...Isn't there any other way...
Or..pls if someone can give me a brief idea in simpler language...Or where can I get examples and overview that sort of explanation??
Edesign
http://electrosofts.com/serial/ should help.
Try using a DOS based compiler like Turbo C.
Here's some code that I've used in the past to communicate with a security controller which uses a Motorola 68000 processor.
Code:#include <windows.h>
#include <stdio.h>
#include <string.h>
HANDLE hPort;
BOOL WriteByte(BYTE bybyte)
{
DWORD iBytesWritten=0;
DWORD iBytesToRead = 1;
if(WriteFile(hPort,(LPCVOID)
&bybyte,iBytesToRead,&iBytesWritten,NULL)==0)
return FALSE;
else return TRUE;
}
BOOL WriteString(const void *instring, int length)
{
int index;
BYTE *inbyte = (BYTE *) instring;
for(index = 0; index< length; ++index)
{
if (WriteByte(inbyte[index]) == FALSE)
return FALSE;
}
return TRUE;
}
BOOL ReadByte(BYTE &resp)
{
BOOL bReturn = TRUE;
BYTE rx;
DWORD dwBytesTransferred=0;
if (ReadFile (hPort, &rx, 1, &dwBytesTransferred, 0)> 0)
{
if (dwBytesTransferred == 1)
{
resp=rx;
bReturn = TRUE;
}
else bReturn = FALSE;
}
else bReturn = FALSE;
return bReturn;
}
BOOL ReadString(void *outstring, int *length)
{
BYTE data;
BYTE dataout[4096]={0};
int index = 0;
while(ReadByte(data)== TRUE)
{
dataout[index++] = data;
}
memcpy(outstring, dataout, index);
*length = index;
return TRUE;
}
void ClosePort()
{
CloseHandle(hPort);
return;
}
HANDLE ConfigureSerialPort(LPCSTR lpszPortName)
{
HANDLE hComm = NULL;
DWORD dwError;
DCB PortDCB;
COMMTIMEOUTS CommTimeouts;
// Open the serial port.
hComm = CreateFile (lpszPortName, // Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING, // How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy
// Initialize the DCBlength member.
PortDCB.DCBlength = sizeof (DCB);
// Get the default port setting information.
GetCommState (hComm, &PortDCB);
// Change the DCB structure settings.
PortDCB.BaudRate = 9600; // Current baud
PortDCB.fBinary = TRUE; // Binary mode; no EOF check
PortDCB.fParity = TRUE; // Enable parity checking
PortDCB.fOutxCtsFlow = FALSE; // No CTS output flow control
PortDCB.fOutxDsrFlow = FALSE; // No DSR output flow control
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
// DTR flow control type
PortDCB.fDsrSensitivity = FALSE; // DSR sensitivity
PortDCB.fTXContinueOnXoff = TRUE; // XOFF continues Tx
PortDCB.fOutX = FALSE; // No XON/XOFF out flow control
PortDCB.fInX = FALSE; // No XON/XOFF in flow control
PortDCB.fErrorChar = FALSE; // Disable error replacement
PortDCB.fNull = FALSE; // Disable null stripping
PortDCB.fRtsControl = RTS_CONTROL_ENABLE;
// RTS flow control
PortDCB.fAbortOnError = FALSE; // Do not abort reads/writes on
// error
PortDCB.ByteSize = 8; // Number of bits/byte, 4-8
PortDCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
PortDCB.StopBits = ONESTOPBIT; // 0,1,2 = 1, 1.5, 2
// Configure the port according to the specifications of the DCB
// structure.
if (!SetCommState (hComm, &PortDCB))
{
printf("Could not configure serial port\n");
return NULL;
}
// Retrieve the time-out parameters for all read and write operations
// on the port.
GetCommTimeouts (hComm, &CommTimeouts);
// Change the COMMTIMEOUTS structure settings.
CommTimeouts.ReadIntervalTimeout = MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
if (!SetCommTimeouts (hComm, &CommTimeouts))
{
printf("Could not set timeouts\n");
return NULL;
}
return hComm;
}
int main(void)
{
// Can also use COM2, COM3 or COM4 here
hPort = ConfigureSerialPort("COM1");
if(hPort == NULL)
{
printf("Com port configuration failed\n");
return -1;
}
// Call your ReadString and WriteString functions here
ClosePort();
return 0;
}
i wrote a small code using inportb & outportb....just checked with a serial cable connected between 2 pcs...
I am able to transfer files but facing 2 problems...
1) even though the baud rate set is very high 38400bps, it takes too much of time..I mean
by this baud rate ideally it should have a datarate of 3.75KBps, but to transfer a 15KB file it takes more than 1.5-2 minutes..I don't know why..
2) this is the code used for reception
this does the work, but every time i need to break the execution..guessing it might have comlete reception..Is there some sort of acknowledge mechanism..so that i can know tht file transfer is complete...Code:void receivefile(FILE *out)
{
do
{
c=inportb(PORT1+5);
if(c&1)
{
cha=inportb(PORT1);
fputc(cha,out);
delay(10);
}
}while(1);
}
Would you think that waiting 0.01 seconds every character you output could have some effect on the transfer time? At 38400, this is equivalent of waiting for 384 bit times - so 38 characters, making it 39 two out of five waits.Code:delay(10);
Also calling fputc on every character will be noticably less efficient than buffering up some amount (such as 256 to 1024 bytes) and storing that using fwrite().
--
Mats
hey edesign
I have book calledit's mostly about PARALLEL PORT but it might help you. If you need it just send me a PM .Code:interfacing-with-c-programming-real-world-applications
thanks.
Guess you can use a sentinel character in the file you are sending so that you can check it in the received file.Quote:
this does the work, but every time i need to break the execution..guessing it might have comlete reception..Is there some sort of acknowledge mechanism..so that i can know tht file transfer is complete...
Suppose if it is '#'
Like so...
instead ofCode:do
{
// code to receive
}while(cha!='#');
Code:do
{
// code to receive
}while(1);
Alternatively, send the size of the file in a "header packet" somehow.
--
Mats
yes, sir i was just wondering why its taking so much time and realized that delay is the reason..also sir, I have one question,
If transfer can be done so easily using these commands then why use bioscom or createfile etc. commands? I mean what is the main difference?(just curious)..
Thanks
Edesign
Perhaps you'd want to use it within a 32-bit application (e.g. receive a LARGE file and store it in memory before writing anything to disk - just as an example), use higher than 38400 bps with some reliability, and not consume 100% of the CPU whilst doing it?
Also, your serial port may not be where you expect it to be - e.g. it may be a USB port in between, and it MAY be that the DOS emulation inside Windows handles that too, but it may also NOT do that. Even in the case of DOS emulation actually handling the IO, it will most likely take MUCH longer to perform this emulation process with a USB device, and the overhead is noticeable.
--
Mats
--
Mats
Ok..thanks....
I got it...Now I will try using fiber optic kit that I have at my college...I hope that also work..
Thanks..all
Hello all,
I made a complete program & now suddenly I am facing a strange problem...for text files it works perfectly but for other type files it transfers 1KB & then stops..what can be the reason..
I can't find out what can go wrong...I changed everything I can think of causing the problem...Code:#include <stdio.h>
#include <dos.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
#include<limits.h>
#define PORT1 0x3f8
#define PATH_MAX 512
long int length;
char ch,cha;
char a,c;
int choice,i;
void main()
{
void transmitfile(FILE *,long int);
void receivefile(FILE *);
long int sizeoffile(FILE *);
char filename[PATH_MAX],filename2[PATH_MAX],buf;
FILE *in,*out;
clrscr();
outportb(PORT1+3,0x80);
outportb(PORT1,0x03);
outportb(PORT1+3,0x03);
printf("\nthis computer is going to act as a:");
printf("\n 1.sender \n2.receiver\n\nEnter the choice:");
scanf("%d",&choice);
char x;
switch(choice)
{
case 1:
x=getchar();
printf("enter the filename to be created:");
fgets(filename,PATH_MAX,stdin);
in=fopen(filename,"rb");
length=sizeoffile(in);
if(length!=NULL)
printf("\n\nsending.....\n\n");
else
printf("\n\nFile does not exist\n\n");
transmitfile(in,length);
fclose(in);
printf("Reading from the port...");
printf("writing to file %s",filename2);
break;
case 2:
x=getchar();
printf("enter the filename to be created:");
fgets(filename2,PATH_MAX,stdin);
out=fopen(filename2,"wb");
printf("Reading from the port...");
printf("writing to file %s",filename2);
receivefile(out);
break;
default:
exit(1);
}
getch();
}
long sizeoffile(FILE *fp){
long curpos, length;
curpos = ftell(fp);
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
fseek(fp, curpos, SEEK_SET);
return(length);
}
void transmitfile(FILE *in,long int len)
{
long int i=0;
do{
{
ch=fgetc(in);
do{
a=inportb(0x3fd)&0x40;
}while(a!=0x40);
outportb(PORT1,ch);
//printf("%c",ch);
i++;
}while(i<len);
}
void receivefile(FILE *out)
{
do
{
do
{
c=inportb(PORT1+5)&0x01;
}while(c!=0x01);
cha=inportb(PORT1);
//printf("%c",buf);
fputc(cha,out);
//delay(10);
if(kbhit())
{
ch=getch();
}
}while(ch!=27);
}
Thanks
Edesign
I don't see any reason why your code should stop after 1KB - are you sure it's exactly 1KB, or some other amount, and perhaps it makes sense to add a log-file that prints each byte sent as a hex number (using " %02x" format), for the transmit and receive sides, and see where it stops.
I'm surprised that works at all - fgets() is supposed to leave a newline in the string, and fopen() should complain about that.Code:fgets(filename,PATH_MAX,stdin);
in=fopen(filename,"rb");
Ehm, NULL is meant to be used with pointers. The variable "length" is a pointer.Code:length=sizeoffile(in);
if(length!=NULL)
Perhaps this belongs just after scanf(), rather than in each of the two switch statements?Code:x=getchar();
--
Mats
Hello Sir,
yes..I should move it before switch..also..I will change the fgets to readline that you suggested me ...then I will check again...If I keep the printf statements uncommented it prints same garbage on both screens...I'll check once again...
Thanks,
Edesign
also note that main returns int - read FAQ
Sir there is also one more question - general
When i pass the file path as command line arguments and if the path is having space " ", then it is not able to open the file...that is c:\documents & settings\hi.txt..can't be opened..how to overcome this?
Edesign
use quotes around the path
You should pass the path in ""
QuantumPete
And one drawback of using an old DOS-based compiler is that it doesn't "understand" long filenames. Long filenames is anything that:
Contains a mix of lower and upper case
Contains one or more spaces.
Has more than 8 characters before the dot or more than 3 after, or more than one dot in the name.
[There are PROBABLY other things that classify as "long name" too, but those are the most common "long name" rules].
You then need to specify the filename using the "old style names". If you do dir /X it will show the names as 8.3 compliant names. Names will look like this XXXXXX~1.YYY. So your file to copy would be perhaps "c:\Docume~1\hi.txt"
--
Mats