jakebird451
jakebird451

Reputation: 2348

Can Arduino transmit null characters via Serial?

I'm going to set up a binary driven serial communications between two Arduinos via the built-in hardware serial library. Since my packets are structured in a binary format, it is very likely that several characters in the packet are null characters for instances of integers with a 0 value. I'm not sure how the Arduinos will handle null characters or if at all. I would certainly like to know before I go any further on my project.

Upvotes: 4

Views: 3652

Answers (4)

Gengis
Gengis

Reputation: 63

Binary data can be transmitted using the write() method in the following form:

Serial.write(buf, len)

where...

buf: an array to send as a series of bytes.
len: the number of bytes to be sent from the array.

In this way all the specified characters are transmitted, included the null ones. Otherwise, if just the buffer parameter is passed to the method, it treats the parameter as a string and stops transmitting at the first null character it encounters.

Upvotes: 6

Dhiraj Patil
Dhiraj Patil

Reputation: 21

Same Issue I found sending following uart packet via Software Serial: a[10]={0x12,0x32,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x14} mySerial.write(a) will send only 0x12,0x32,0x30 and wont send the null character. To solve this I had to send null characters using mySerial.write((byte)0). So Arduino Serial Library sends null character only if it is sent alone :P

Upvotes: 0

vz0
vz0

Reputation: 32923

Yes. The Arduino documentation for write() talks about "binary data" and bytes.

Upvotes: 2

O. Jones
O. Jones

Reputation: 108766

Back to the future!

Yes, standard UART serial will handle binary just fine. Make sure you have a stop bit configured on the UART (universal asynchronous receive/transmit) device at each end of the serial line and you should be all set.

This kind of thing takes a certain amount of goofing around to get right, in my experience. If you set both UARTs to 8 bits, one stop bit, no parity, and the same bits/sec rate, you should be good.

You could try hooking one end of the serial line to a terminal emulator on your PC if you're really puzzled. SecureCRT by Van Dyke Software has a 30-day free trial and will handle ordinary serial.

Upvotes: 1

Related Questions