Arduino Nano serial break

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Hello there !

 

I have build a ps2 keyboard to serial ascii converter with a Nano so I can use these keyboards with my old computers.

 

This is working well but I need a function to send a 'break' to the serial line. I choose the Pause/Attn key to initiate this function.

 

At this moment I have do that :

 

void sendserialbreak()

{

Serial.end();

Serial.begin(300, SERIAL_7N2);  // Set slow baud rate

Serial.print(0b00000000);

Serial.end();

Serial.begin(1200, SERIAL_7N2): // Restore normal baud rate

}

 

With the lower speed null char is interpreted as a break.

 

This give a break to the host computer but I noticed two problems.

 

After the break the Arduino UART is more or or less crazy and I have to reboot the Nano to get it working well again...

Also the successive Serial.end. Serial begin etc... seems to cause swing on the TX line and this is badly interpreted as a character (ascii code 120 decimal) by the host computer.

 

I search a clean solution to do that.

 

Many thanks for help and suggestions.

 

Regards.
Philippe

This topic has a solution.

Last Edited: Wed. Jun 1, 2022 - 05:53 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


The problem you've found is that Arduino functions do an awful lot of hidden stuff. I see two options...

 

1) Write your USART code in native C. You can do this in the Arduino IDE and there are plenty of examples out there.

 

2) Generate your break in hardware (it's how I do it)...

 

 

...with the IO pin set as an input the TX pin will work as usual. Set the IO pin low and then make it an output and the line will go low and generate your break.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome!

WawaVoum wrote:

After the break the Arduino UART is more or or less crazy and I have to reboot the Nano to get it working well again...

Also the successive Serial.end. Serial begin etc... seems to cause swing on the TX line ...

Consider disabling the USART then a reinitialization.

USART Initialization | ATmega328PB

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Set the TX pin as an output set high as the serial port idles high. This setting will be overridden/restored when you do your serial.begin/end leaving the pin high in between. No more odd characters.

 

jim

 

 

 

FF = PI > S.E.T

 

Last Edited: Mon. May 30, 2022 - 05:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suspect that what's happening is that Serial.begin() and Serial.end() enable and disable the USART which will mean that the TX and RX pins will revert to IO lines, and if left in the default state as inputs will float.

 

You could try setting TX to an output set high (?) somewhere near rthe top of your setup() code so that when released it'll be in a definite state.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Re,

 

I give a try to the hardware solution proposed (with 470 ohms and 1 Kohms).

 

I use pin D5 as I/O pin.

 

The sendserialbreak() routine is now as follow

 

void sendserialbreak()
{
   Serial.flush();
  digitalWrite(BreakPin,LOW);
  pinMode(BreakPin,OUTPUT);
  delay(30);
  pinMode(BreakPin,INPUT);
}

 

BreakPin is set to 5 and as input in setup part of the code.

 

This produce the expected signal on serial line whithout extra character (one problem solved !).

 

But again the uart is not working after the break. I can see on scope that for the same keystroke the signal is different on the serial line before and after the break.

Also this is also visible on the usb socket (GTKTerm read char well before the break and read nothing good after). So the Max232 part of the circuit is not the problem.

A simple reset of the Nano and works well again...

At this moment I dont understand how this is possible ???surprise

 

I dont understand except if some kind of "readings" are do by serial functions on the tx pin ???

 

Last Edited: Mon. May 30, 2022 - 06:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

WawaVoum wrote:
Also this is also visible on the usb socket (GTKTerm read char well before the break and read nothing good after). So the Max232 part of the circuit is not the problem.

 

usb socket, Max232 ???

Obviously not a standard Nano. A diagram may help here.

 

 

BTW: I've used this baudrate switching technique with success on Windows and PIC to bring a bluetooth module back into Command Mode.

(I know, I know; both those platforms have break support built in, let's just ignore that for now)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

 

The Max232 is outside and connected to the Nano TX pin and the host just for level translation.

By usb socket I mean the usb connector of the Nano.

 

What I want say is that the problem can be see after the Max232 but also before so for sure it come from the Nano itself.

 

Regards.
Philippe

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm still really confused. I don''t see how you can compare and contrast signals on USB D+ / D- versus TX & RX into your MAX232. They are wildly different protocols.

 

Edit:

I looked over the AVR source for HardwareSerial.flush(), HardwareSerial.begin() and HardwareSerial.write() and believe you could get away with this.

 

void sendserialbreak()
{
    Serial.flush();                 // Ensure all TX activity has stopped.
    Serial.begin(300, SERIAL_7N2);  // Set slow baud rate
    Serial.print(0);                // The byte will be written to UDR0 immediately
    Serial.flush();                 // Ensure all TX activity has stopped.
    Serial.begin(1200, SERIAL_7N2): // Restore normal baud rate
}

 

Last Edited: Tue. May 31, 2022 - 09:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes and no...

 

The usb socket is connected to rx and tx pins via a serial to usb converter included in the Nano (could be a CH340 or similar).

As soon as you connect the Nano to a computer you get a device ttyUSBxx (under linux) which act as a serial port (and the kernel load the appropriate driver).

This CH340 driver in the computer do the job for converting from usb D+/D- to serial communication protocol.

 

And here you can read the same datas that on tx pins.

 

Of course using both as the same time can be confusing but I only use TX in my project so here it is not a problem...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

To get a quicker answer, post a small complete sketch that demos the problem, as posting small snippets of where you think the problem is, is not helpful.  
jim

 

 

FF = PI > S.E.T

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

 

I solve the problem with the hardware solution proposed here.

 

Trying to do the break only by software give TX pin swing (at serial.end, serial.begin and I suspect also at baud rate change) and possible 'false character' sending...

All these solutions need a pull up resistor to TX pin to get a clean break.

 

Thanks for all the suggestions.

Regards.
Philippe