1

I am a newbie in Arduino and serial communication. I am working on a project that use Arduino to send data bits to an actuator using RS232 communication protocol. In order to control the actuator, an instruction consists of 6 bytes must be sent with 9600 baud, no hand shaking, 8N1 format.

Here are the instructions: Byte 1 - Device, Byte 2 - Command, Byte 3 - Data(LSB), Byte 4 - Data, Byte 5 - Data, Byte 6 - Data(MSB)

For example to send 1, 20, 1, 1, 0, 0 (Move the actuator to absolute position of 257 microsteps) code This is my draft code. How can I send start and stop bit? I am confused on sending start and stop bit. The Serial.write() is sending binary data as a byte. Does it mean that I write Serial.write(20), the compiler will send the data in 00010100 binary format? I am quite frustrated with serial communication.

  • The Serial.write is working on a level of bytes and byte arrays, not bits. You don't need to implement the lower level stuff when using it. Anyway, looks like a major misunderstanding of things. – Eugene Sh. Mar 23 '17 at 18:42
  • The start and stop bits are rolled into the protocol automatically. You do not have to worry about them. Try eapbg.com/single-post/2016/05/26/eapbg-15-Introduction-to-UART maybe it will help. – vini_i Mar 23 '17 at 18:42
  • 4
    Please don't post code as screenshots. Please post code as text instead. Text is indexable and searchable, screenshot's aren't. – Nick Alexeev Mar 23 '17 at 19:00
3

The start and stop bit are already included as part of the UART protocol. When you send a byte it first sends the start bit, then the byte, then the stop bit. You don't need to do anything to make those bits send.

20 is actually sent as 0001010001

So your code will just be a series of Serial.write() calls for each byte you want to send.

Serial.write(1);
Serial.write(20);
Serial.write(1);
Serial.write(1);
Serial.write(0);
Serial.write(0);

Or as an array:

uint8_t data[6] = {1, 20, 1, 1, 0, 0};
Serial.write(data, 6);
  • Thanks for the comment, I will try using your suggestion :) – kamchai Mar 24 '17 at 8:20
  • 20 is sent as 0001010001: least significant bit first. – Edgar Bonet Mar 24 '17 at 8:49
  • Yeah, I just blindly copied his example without checking. – Majenko Mar 24 '17 at 10:01
2

Read the language reference: https://www.arduino.cc/en/Reference/Serial
..and for write(): https://www.arduino.cc/en/Serial/Write
....notice that it "writes" the bytes or buffers you define. You have already defined the baud rate and started the serial channel, then all you need to do is pass the characters or strings you want to send to the write function.

In your application it may be best to build a 6 character command string and send it all at once as a string. You must then remember that With "write" you are responsible for "writing" the cr and lf which is probably needed as a command terminator in your receiving device.

For your kind of application it may also be possible to use "println" which appends a cr/lf automatically for you. It depends on your protocol.

  • You certainly don't want to use println() with a binary protocol. – Edgar Bonet Mar 24 '17 at 8:50
  • @EdgarBonet. If it' is a truly binary level serial protocol that's true. However I did qualify it, it's unusual to see simple protocols like this not be easily keyboard creatable strings. – Jack Creasey Mar 24 '17 at 11:06
  • You failed to read (or understand) the second paragraph of the original question. – Edgar Bonet Mar 24 '17 at 11:56
  • Not at all, I assumed given the grade of question that the OP may not have described the problem completely. I completely covered his question accurately in my first paragraph. I simply gave an option if that was suitable. I have altered the answer....hope it meets your niggle. – Jack Creasey Mar 24 '17 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.