473,729 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 11142
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**********@p andora.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.progr ammer (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***********@p hysik.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.l earn.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**********@p andora.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
11030
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 creates a "normal" comm port (like COM3). There is a C++ program that has no problem talking to the equipment, but two Python programs have trouble. One is a big, scary, controller program, and the other, since that is the one I'm having trouble...
21
3113
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 bit by bit to another byte. They have some weird way they set this up that i have to compare these things with AND. in other words, if bit 1 is 1 AND bit 1 is 1 then the real value is 1... long story short. is there a good way to compare...
8
14769
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
5054
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', '\0', '\1', 'Z', '0', '0',
5
4405
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 was quite insestual for while wasn't it? I need to write putchar so that printf can function properly. Anyway, the compiler comes with just a shell of a putchar routine. It literally returns the character you passed to it and nothing else. That is...
2
5766
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 Developersdex http://www.developersdex.com *** Don't just participate in USENET...get rewarded for it!
1
1717
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 binary file through com port, read and record the com port data into a disk file. After file comparison, no error occurs. When the GPS receiver is connected, my program drops or adds one or two bytes in each data block which is about 200 bytes. The...
0
2059
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 / Bluetooth / Infrared. May I ask does serial port equal to com port? The program needs to read and write data of the smart card which places on top of the card reader. The card reader has serveral models, they support
0
2005
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 incoming string looking for the vbCrLf indicator but it too gave me errors as the length of the stream was not always 24 bits for some reason. I have to slow the output down to reduce the amount of garbled data being sent to the serial port.
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9142
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6722
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.