473,394 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

write binary data to serial port

Hi,

I need to write binary data to a serial port. I am using this function:

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

I am able to write a alpha-numeric character to the port using this:

write (filedescriptor,"a",1);

But I want to write a byte of 1's and 0's to this port. For examples, I want
to write 00000011 to this file descriptor.

How do I do this?

Many Thanks!!

Regards,
Tom
Nov 14 '05 #1
4 11089
Tom Van Ginneken vient de nous annoncer :
I need to write binary data to a serial port. I am using this function:

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

I am able to write a alpha-numeric character to the port using this:

write (filedescriptor,"a",1);

But I want to write a byte of 1's and 0's to this port. For examples, I want
to write 00000011 to this file descriptor.

How do I do this?

Many Thanks!!

Regards,
Tom


The C-language doesn't deal with the serial ports. You should ask to a
newsgroup dedicated to your platform, probably one with 'unix' in its
name.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #2
Tom Van Ginneken <tv**********@pandora.be> wrote:
I need to write binary data to a serial port. I am using this function: #include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count); I am able to write a alpha-numeric character to the port using this: write (filedescriptor,"a",1); But I want to write a byte of 1's and 0's to this port. For examples, I want
to write 00000011 to this file descriptor.


Please understand that questions about serial ports and functions like
write() are off-topic here - if you have problems with these you will
get a friendlier reception in e.g. comp.unix.programmer (at least
that's what seems to be appropriate from your use of the non-standard
include file <unistd.h>).

But what you seem to be missing is that 'a' is already a bit pattern,
on a machine with an ASCII character set it's 01100001. All you have
to do is to stick the bit pattern you want to send into a char and
then send that. If you want to send e.g. the bit patterns

10111001
00000011
10000001

you would create an array of 3 chars, set its elements to these values
and then send them, e.g.

unsigned char data[ 3 ] = { 0xB9, 0x03, 0x81 };
write( filedescriptor, data, 3 );

Since in C you can't specify binary numbers you have to convert
your bit patterns into the corresponding hexadecimal or octal or
decimal values, so

unsigned char data[ 3 ] = { 0xB9, 0x03, 0x81 }; /* hex */
unsigned char data[ 3 ] = { 0271, 03, 0201 }; /* oct */
unsigned char data[ 3 ] = { 185, 3, 129 }; /* dec */

would all do the trick for the above set of binary values.

Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
Tom Van Ginneken wrote:
Hi,

I need to write binary data to a serial port. I am using this function:

#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);

I am able to write a alpha-numeric character to the port using this:

write (filedescriptor,"a",1);

But I want to write a byte of 1's and 0's to this port. For examples, I want
to write 00000011 to this file descriptor.

How do I do this?

Many Thanks!!

Regards,
Tom


Change the run-time library or operating system so that
a file descriptor of a serial port works correctly. Or
you could consult your operating system documents to
find out what descriptor, if any, are used for the
serial port(s).

I altered the code on one embedded system to use
additional file descriptors for serial ports. Worked
out nice!

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Nov 14 '05 #4
kal
"Tom Van Ginneken" <tv**********@pandora.be> wrote in message news:<h%**********************@phobos.telenet-ops.be>...
size_t write(int fd, const void *buf, size_t count);
This is the function declaration.
write (filedescriptor,"a",1);


The write function is called here to write 1 character from
the buffer "a" (which is a string literal.) This can also
be called as follows (hopefully).

write (filedescriptor,"\141",1);
write (filedescriptor,"\141b",1);
write (filedescriptor,"\141\142",1);

The following may write the characters 'a' and then 'b'.

write (filedescriptor,"\141\142",2);
write (filedescriptor,"\141b",2);
write (filedescriptor,"ab",2);

The following may write the bit pattern 00000011.

write (filedescriptor,"\003",1);
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Bob Greschke | last post by:
We have some equipment that communicates at 57600 baud RS232. The path from the PC is USB to a Phillips USB hub, then off of that a TUSB3410 USB/Serial converter. The driver for the 3410 chip...
21
by: nephish | last post by:
i have an interesting project at work going on. here is the challenge. i am using the serial module to read data from a serial input. it comes in as a hex. i need to make it a binary and compare it...
8
by: collinm | last post by:
hi we use linux and c language under bash i do echo -e \\000\\000\\000\\000\000\\001Z00\\002AA LINUX \\004 >/dev/ttyS2 that send command to a led display (alpha sign communication)
3
by: collinm | last post by:
hi i send a command to a led display, the led display is suppose to return me some character i write a string on a serial port void ledDisplayExist() { char msg={'\0', '\0', '\0', '\0',...
5
by: Confused User | last post by:
I am working on device that utilizes a Motorola 68HC16 microcontroller. I am using an old unsupported piece of crap Whitesmith's / Intermetrics / Tasking compiler. The embedded compiler business...
2
by: Alvin Lau | last post by:
Can I write pocket PC bluetooth program by using C# ? It seems so difficult to find the library? If it is possible , where can i find the reference of these kind of program ? *** Sent via...
1
by: arrowlike | last post by:
In a VC6 program, a mscomm32.ocx is added to the project and read the binary incoming RS232 data stream (from a GPS receiver). I use another commercial serial port debug program to send out a...
0
by: Tom | last post by:
I am new to hardware programming. I need to write a program for reading data from Card Reader which connects to the PC windows 2000/XP OS through Interfacing The Serial / RS-232 Port / USB /...
0
by: RG | last post by:
I am trying to read from my serial port a 24 bit binary number. I was able to read this number as a HEX but I was getting errors as at times using the vBCrLf indicator. I also can read it as an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.