LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-09-2004, 12:28 AM   #1
ODSunal
LQ Newbie
 
Registered: Dec 2004
Distribution: Suse 9.1
Posts: 1

Rep: Reputation: 0
Send an .hex file by serial port


Hello everybody:

I need to drive the serial port. I have been reading about some functions to do this in C and i found this:

write(fd, "a", 1);

This function returns -1 on error or the number of bytes transmitted in success.

My problem is that what I need to send by serial port an .hex file. So I have read the .hex file with:

int car;
char nombre[13]=PWM.hex"; // File which will bi open
FILE *buffer;
buffer = fopen( nombre, "r" );
printf( "Fichero: %s -> ", nombre );
if( buffer )
printf( "File open\n" );
else
{
printf( "File is not open \n" );
return 1;
}
car=fgetc(buffer);

In car I have the first character of the .hex file. When I show this character with:

printf("%c", car);

every thing is good but I don´t know how to send this character by the serial port, the function:

write(fd, car, 1);

doesn´t work. This function returns -1 meaning that an error occurs. I think that this is because the data type doesn´t match. But who can I make this work well?

Can anybody tell me what’s wrong or which function should I use instead of write????.

before the code that i have show below i have open the serial port file so this is not the problem.

Tanks a lot.
 
Old 12-09-2004, 03:37 AM   #2
rjlee
Senior Member
 
Registered: Jul 2004
Distribution: Ubuntu 7.04
Posts: 1,994

Rep: Reputation: 76
First of all, the easiest way to output a file through a serial device is to use
Code:
cat file > /dev/ttyS0
(This assumes that you're not bothered about the speed of the serial device).

You can find out what the error is by #include-ing the files errno.h and string.h. Then replace your call to write(fd,car,1) with:
Code:
errno=0;
write(fd,car,1);
if (errno != 0) printf("%s\n",strerror());
This will output the error message caused by the write command.
 
Old 12-09-2004, 08:12 PM   #3
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
Hi, you're sending a intel hex image file to a board?
The way I do it is through kermit.
# kermit
set port ttyS0
set speed 9600
set flow none
set carrier off
transmit filename.hex
close
quit

I also have a serial library I wrote if you still want to do it in C.
I don't know how to get it to you, you can try emailing me.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using serial port card(PCMCIA) with IPAQ running Linux, can't find ttyS0 port d2army Linux - Laptop and Netbook 0 11-12-2005 08:07 PM
Search file for Hex value paddyjoy Programming 6 08-11-2005 01:11 AM
Send HEX info to parallel port thebover Slackware 14 10-12-2004 03:53 PM
Attaching an intel Hex file. krushna Linux From Scratch 0 06-07-2004 03:47 PM
How to Redirect serial port input to a file Legaviu Linux - General 1 07-31-2001 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration