Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PC to uC]Wave File Through RS-232

Status
Not open for further replies.

theone_in_themoon

Junior Member level 3
Junior Member level 3
Joined
Oct 20, 2003
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Lahore Pakistan
Activity points
323
Serial Code "HangUp"

Hello Everybody...

Ive seen quite a handsom no. of posts regarding serial communication through RS-232 port .... But my problem is a bit strange.

Im using a simple code to read a file character by character and transmitt it serially. Wat i want to do actually is to send a WAV file serially to a Micro controller which stores it in a CF Card.

Currently im testing it to send the file correctly. But even when i join together the traans and receive of the Serial Port(COM1) the program hangs up after sending approx. 250 bytes.

Ive tried sending a text file and that is sent correcly.... but when sending a WAV file .... the program just gets stuck after 251 bytes.

Ive played with a lot ... and ive noticed that if we put a delay after bioscom send character command line ...... then there is a change in the no. of byets after which it gets stuck. But the Max it has gone to is 340 byte approx.

Here is the code :
Code:
/*
THIS IS A PROGRAME TO READ A FILE BYTE BY BYTE AND SEND IT TO 
SERIAL PORT.

HOWEVER IT ALSO RECEIVES THE SAME CHAR BACK AND WRITES IT TO A
SPECIFIED LOCATION.

***IT DOES NOT WAIT FOR THE CHAR SENT ... TO BE RECEIVED

*/

#include <conio.h>
#include <iostream.h>
#include <dos.h>
#include <process.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <io.h>
#include <bios.h>



#define COM1 0
#define DATA_READY 0X100
#define TRUE 1
#define FALSE 0
#define SETTINGS ( 0x80 | 0x03 | 0x00 | 0x00 )


void main(void)
{
	clrscr();

	FILE *file_r ;

	FILE *file_w ;


	char path_r[50];
	char path_w[50];

	long ctr_r = 0;
	long filesize = 0;

	int handle = 0;
	int status = 0;

	unsigned char ch_rx = 0;
	unsigned char ch_tx = 0;

	bioscom( 0 , SETTINGS , COM1 );

	cout << "\n\nEnter the file path for Read : ";
	gets ( path_r );

	cout << "\n\nEnter the file path for Write : ";
	gets ( path_w );

	handle = open(path_r, O_RDONLY);
	filesize = filelength( handle );
	close( handle );
	cout << "File Size : " << filesize << endl;


	file_r = fopen ( path_r , "rb" ) ;//"rb" for read only in BINARY

	if ( !file_r )
	{
		perror ( "Read FILE ERROR" ) ;
		getch();
		exit( 0 );
	}

	file_w = fopen ( path_w , "wb" );//"wb" for write only in BINARY

	if ( !file_w )
	{
		perror ( "Write FILE ERROR" ) ;
		getch();
		exit( 0 );
	}


	//loop till end of file for read
	while ( ctr_r < filesize )
	{
		ch_tx = fgetc(file_r);
		bioscom ( 1 , ch_tx , COM1 );
		cout << endl << "Ch_Tx: " << ch_tx;

			status = bioscom( 3 , 0 , COM1 );
			if(status & DATA_READY)
			{
				ch_rx = bioscom( 2 , 0 , COM1 );
				fputc( ch_rx , file_w);
				cout << endl << "Ch_Rx: " << ch_rx << "    " << ctr_r;
			       //	wait = 0;
			       //	getch();
			}

		ctr_r++;
	}

	fclose ( file_r ) ;
	fclose ( file_w ) ;

	cout << "\n\n\WAV file Created ! " << endl;
	getch();
}//end of main

I have also uploaded the cpp file

Please see and help :(

Thanks a lot in advance
 

I guess this is caused by serial port buffer over run. set the baud rate to a lower value then try again.

mike

-----------------------------------------
www.ezpcb.com
 

PLEASE DO NOT CROSSPOST!!!
I removed your other post and moved this one to the place where it should be.

And one remark/question:
Are you programming for DOS?
When you're programming for windows you should handle the serial port in an other way (as a file). Don't do direct HW IO in windows unless you're writing device drivers and know what you're doing!!!

More information can be found here:
https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnfiles/html/msdn_serial.asp

Some things you can try:
Try reading the wav file and see if it is read completely (without sending anything over the serial port). When it fail's there's something wrong with the routines that read your file.
Try sending all Binary characters over the uart and see whether they are received fine.
Code:
for(int i = 0; i<256; i++)
  SendByte((unsigned char)i); // With SendByte the wrapper routine for sending a byte
Try using a debugger and set a breakpoint on the place where it fails

Narrow down the possibilities and try to find the error

Antharax
 

Im sorry abt the cross post but no on replied it to me here so i thought better to post it there as well. But im still sorry :$

Now for the problem ..... well .... as i didnt mention it earlier... ill tell u that im writing this programe to interface PC with a uC. The uC with receive a file byte by byte and write it to a Compact Flash Card. Its working fine excpet that this programe stucks in SOME of the files. It gets stuck after different no. of byte for different files. For some files it runs completely :S

And yes ... it gets stuck even when im just reading an writing within a PC without serial transmission. Ive tried looking at the Character sent in the last before getting stuck.... but its different in different files. Which means it not abt the character.

As far as the buffer size is concerned.... if that was the case .... then it shud ALWAYS get stuck after same no. of byte ... which is not the case. :S

Im helpless right now ... cant think of anything more...

Please Help

Thanks a lot
 

I guess you're programming for DOS
I don't have the header's you're using so i cannot compile your file.

I compiled a file which read's and writes a complete file (actually it's just copy ;-))
I've marked the place where you should try to add the code for the uart

I removed all functions i don't know and only used standard library functions.

I hope this helps.
But DON't do serial communication with the bios calls when you're programming for windows!!!

Code:
#include <stdio.h>
#include <iostream>

int main(void)
{
  FILE * file_r;
  FILE * file_w;

  char path_r[50];
  char path_w[50];

  unsigned char ch_rx = 0;
  unsigned char ch_tx = 0;


  std::cout << "\nEnter the file path for Read :" << std::endl;
  gets (path_r);
  file_r = fopen ( path_r , "rb" ) ;//"rb" for read only in BINARY
  if ( !file_r )
    {
      std::cerr << "Could Not Open Read File!!!" << std::endl;
      return -1;
    }

  std::cout << "\nEnter the file path for Write :" << std::endl;
  gets (path_w);
  file_w = fopen ( path_w , "wb" );//"wb" for write only in BINARY
  if ( !file_w )
    {
      std::cerr << "Could Not Open Write File!!!" << std::endl;
      fclose (file_r);
      return -1;
    }

  std::cout << std::endl;
  while (!feof(file_r))
    {
      ch_tx = fgetc(file_r);
//      std::cout << "Ch_Tx: 0x" << std::hex << (int)ch_tx << std::endl;

      // Send over UART
      // Receive from UART
      //!! WARNING receiving might be blocking!!!
      // This means that the routine waits untill a byte is received!

      // Quick patch:
      ch_rx = ch_tx;

      fputc( ch_tx , file_w);
//      std::cout << "Ch_Rx: 0x" << std::hex << (int)ch_rx << std::endl;
    }

  fclose ( file_r ) ;
  fclose ( file_w ) ;

  std::cout << "WAV file Created ! " << std::endl;
  return 0;
}
 

Probably Turbo C++ has it's own syntax specifics ;-)
(I guess it fails over the namespaces (std::cout ...) )


I suggest you copy the entire file without using the serial port.
(remove all bioscom calls)
perhaps you can try to remove the filesize thing and walk through the entire file like this:
Code:
  while (!feof(file_r))
    {
      ch_tx = fgetc(file_r); 
      ...

Then I suggest you send bytes to the uC using the bios commands (but leave out the receiving part.)

when there are no problems here, you can try to check if everything is received again on the PC. But beware, printing out your info on a terminal window (using cout) is time consuming, so it might cause buffer overflows!
(perhaps you should only print the received byte?)
And try to print the byte in hex:
Code:
cout << hex << (int)ch_rx

And try to use a debugger when possible.

Perhaps you can use this information too:
https://www.ontrak.net/c.htm

Again, i had no confirmation that you are programming for DOS. When you're programming for windows you should not use Turbo C.
There are better (and free) compilers when you're programming for Windows!
I cannot help you with your problem since i don't know your compiler and the routines you're using.

Antharax
 
Thanks Antharax........... Ive already clicked for +3 points for u.
However i need a lil more help. And i think u can help me...

Ive come to know that TC cant support higher baud rates than 9600. But i need to communicate higher than that. Any rate below 10,000 will do.

Now i want to program in Visual C++ or Visual Basic ... a program which does the same thing. i.e. reads the file char by char and sends it thru the serial port.

OR

If that doesnt do either then ill have to resort to Parallel port.

Can you 'translate' the code that i pasted for Visual C++ ???

Ill be really grateful...
Thanks a lot once again
 

Here is a visual C++ example
It ain't the best solution (multithreaded but it does not include correct thread communication)
It also consumes all available cpu power!!!
I didn't have time to write more but it should do what you want (tested it quickly with a second PC, data is read from the file and send over the uart
the data that is received from the uart is written in the other file)

You can study it and adjust it as you wish.

But perhaps you should read a book about windows programming
( things interesting to learn:
- mutexes: HANDLE myMutex = CreateMutex(NULL, FALSE, NULL);
WaitForSingleObject(myMutex , INFINITE);
ReleaseMutex(myMutex );
- events: HANDLE myEvent = CreateEvent(NULL, FALSE, FALSE, NULL); // AUTO Reset
SetEvent(myEvent);
WaitForSingleObject(myEvent, INFINITE);
)


source for the uart code used:
https://www.tetraedre.com/advanced/serial2.php
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top