Arduino Nano serial break

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 works and give a break to the host computer but I noticed two problems.

After the break the Arduino UART is more 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

Remove the Serial.end().

"more or less crazy" is a rather bad description. What exactly happens? What makes you sure that no other code part is responsible for that?

ASCII CHR 120 (decimal) is "x"... is this the character?

Are you seeing the backwards-question-mark ( ؟ )... if so, this could be an indication of baud rate re-sync or baud rate mismatch as you receive data.

For the same ascii code the signal on tx pin is different before and after the break.

I use 1200 baud 7N2 so the serial monitor of arduino is not working. I use Gtkterm and can see also the problem that way. After the break the ascii code received are not the good one.

A reset and then works well again !

I suspect that you can do a simple digitalWrite(),

void sendserialbreak()
{
  // make sure last character was transmitted completely
  Serial.flush();
  // send break
  digitalWrite(1, LOW);
  delay(some delay time);
  digitalWrite(1, HIGH);
}

You probably want to get rid of the magic number.

Not tested !!

Hi,

I try this :

Pin D5 is used 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.

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

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

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

hi,

This is how I had implemented sending a Serial breakfield on one of my projects:

void send_brk(HardwareSerial &refSer, unsigned long baud)
{
  //breakfield mode
  refSer.end();
  refSer.begin(baud);
  refSer.write(0x00);
  refSer.end();

  //normal mode
  refSer.begin(baud);
}

void setup() {
  unsigned long break_baud = 9600;  //baud rate calculated from duration of brk_field;
                                    //break_baud = (duration of brk_field)/9, (9 = 8bits+1 start bit)

  Serial.begin(115200);
  delay(10);
  Serial.println("READY");

  delay(5000);

  send_brk(Serial, break_baud);

  delay(1000);

  Serial.println("Finish");
}

void loop() {
  // put your main code here, to run repeatedly:
  
}

I had to add an external pullup-resistor on the TX line to maintain the line voltage as the line would momentarily drop between .end() and .begin() commands.

Hope that helps...

You can't, the USART overrides any normal I/O.

Thanks @oqibidipo, OP already had a better implementation in post #6.

Hello,

I solve my problem with the hardware solution.

Trying to do that by software need a pull up resistor because TX pin swing at Serial.end or Serial.begin and may be at baud rate change...

Philippe

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.