Recent

Author Topic: One-way serial communication example using Synaser & Arduino Nano  (Read 2365 times)

Aruna

  • Hero Member
  • *****
  • Posts: 568
One-way serial communication example using Synaser & Arduino Nano
« on: September 21, 2024, 10:09:17 pm »
I am following this example on the wiki and the arduino sketch I modified slightly as shown below:

Code: C++  [Select][+][-]
  1. void setup()
  2. {
  3.     pinMode(LED_BUILTIN, OUTPUT); // Set the default led as digital out, this way will work on *any* board uno, nano, mega
  4.  
  5.     // Start up serial connection
  6.     Serial.begin(9600); // baud rate
  7.     Serial.flush();
  8.     delay(1000);  // give the port time to work
  9.  
  10. }
  11.  
  12. void loop() {
  13.     String input = "";
  14.  
  15.     // Read any serial input
  16.     while (Serial.available() > 0)
  17.     {
  18.         input += (char) Serial.read(); // Read in one char at a time
  19.         delay(5); // Delay for 5 ms so the next char has time to be received
  20.  
  21.         if (input == "on")
  22.         {
  23.             digitalWrite(LED_BUILTIN, HIGH); // on
  24.         }
  25.         else if (input == "off")
  26.         {
  27.             digitalWrite(LED_BUILTIN, LOW); // off
  28.         }
  29.         else if (input == "blink")
  30.         {
  31.           for (int i = 0; i <= 5; i++) {
  32.             digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  33.             delay(500);                                      // wait for 0.5 seconds
  34.             digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  35.             delay(500);          
  36.            
  37.             }
  38.         }
  39.     }
  40. }

When I test the sketch using the arduino IDE's serial monitor everything works as expected.

When testing from Lazarus I see the 'RX' and 'L' led's on the nano flicker but the led does not come on and stay on. I have tried changing baud rates, no luck. Can use some help in how best to move forward with this please.

From past experience when the leds flicker that is usually a sign of baud rate mismatch but I could not get things to work :-(


« Last Edit: September 21, 2024, 10:14:36 pm by Aruna »

MarkMLl

  • Hero Member
  • *****
  • Posts: 8094
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #1 on: September 21, 2024, 11:30:16 pm »
When testing from Lazarus I see the 'RX' and 'L' led's on the nano flicker but the led does not come on and stay on. I have tried changing baud rates, no luck. Can use some help in how best to move forward with this please.

From past experience when the leds flicker that is usually a sign of baud rate mismatch but I could not get things to work :-(

Check what you're doing to the control lines, in particular DTR which is usually connected to the Arduino's reset signal.

https://github.com/MarkMLl/ping-arduino-loader

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ccrause

  • Hero Member
  • *****
  • Posts: 988
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #2 on: September 22, 2024, 08:12:34 am »
Check what you're doing to the control lines, in particular DTR which is usually connected to the Arduino's reset signal.
Yes, this is a likely cause of the symptoms described.

Try disabling the DTR signal by inserting ser.DTR := false; before calling the Connect method(untested).

Aruna

  • Hero Member
  • *****
  • Posts: 568
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #3 on: September 23, 2024, 12:34:38 pm »
Check what you're doing to the control lines, in particular DTR which is usually connected to the Arduino's reset signal.
Tried that already. No luck.

Aruna

  • Hero Member
  • *****
  • Posts: 568
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #4 on: September 23, 2024, 12:35:16 pm »
Check what you're doing to the control lines, in particular DTR which is usually connected to the Arduino's reset signal.
Yes, this is a likely cause of the symptoms described.

Try disabling the DTR signal by inserting ser.DTR := false; before calling the Connect method(untested).
Tried it, same error.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8094
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #5 on: September 23, 2024, 01:11:12 pm »
Tried it, same error.

Right. Are you still at the position that the "sketch" works properly if driven from the Arduino IDE, but not with the code you posted at  https://forum.lazarus.freepascal.org/index.php/topic,68617.msg530689.html#msg530689 (I want to check that before fiddling with your download... although how the Hell you get a simple project to get to 500K is beyond me).

When I specified DTR and explicitly said that it was connected to the Arduino's reset signal, I did of course mean DTR coming from the computer.

I think that the problem is that you're trying to use the normal text/character operations provided by FPC (hence unix etc.) when in actual fact you need to be using either the serial unit (which I use heavily) or one of the component-level wrappers available (which I've never touched). You /have/ looked at the examples I gave you, /haven't/ /you/?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Aruna

  • Hero Member
  • *****
  • Posts: 568
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #6 on: September 23, 2024, 01:27:36 pm »
Tried it, same error.

Right. Are you still at the position that the "sketch" works properly if driven from the Arduino IDE, but not with the code you posted at  https://forum.lazarus.freepascal.org/index.php/topic,68617.msg530689.html#msg530689 (I want to check that before fiddling with your download... although how the Hell you get a simple project to get to 500K is beyond me).
If you open the unit1.lfm in a text editor and browse you will see it has a
Code: Pascal  [Select][+][-]
  1. Picture.Data = {
that is the reason :-)

I think that the problem is that you're trying to use the normal text/character operations provided by FPC (hence unix etc.) when in actual fact you need to be using either the serial unit (which I use heavily) or one of the component-level wrappers available (which I've never touched). You /have/ looked at the examples I gave you, /haven't/ /you/?
Yes I had a look at ping-arduino-loader/pingar.lpr.

ccrause

  • Hero Member
  • *****
  • Posts: 988
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #7 on: September 23, 2024, 06:41:57 pm »
We have to dig a bit deeper:
 1) Check if Arduino is actually resetting by adding a couple of LED flashes in Setup().  This way you can visually see when the controller resets.  This will help in deciding whether the DTR signal is messing around.  Although the bootloader should also flash the onboard LED when the board is reset.

2) In the wiki Lazarus example TBlockSerial.Create is called every time a button is pressed.  Rather move TBlockSerial.Create to TForm1.OnShow and free it in TForm1.OnClose.  This way the DTR signal should only be asserted once when the Lazarus program starts.

3) If you have a spare USB-serial converter (or another Arduino board with an onboard USB-serial converter): connect the converter RX pin to the RX pin on the Arduino board, this way you can snoop the serial traffic in the Arduino serial monitor (or any other terminal) using the USB-serial converter.

4) If you have a logic analyser or oscilloscope (should be first step, but I assume OP is just getting started), confirm that the signal on the RX pin of the Arduino has the expected minimum pulse width (9600 baud => 104 microseconds).

MarkMLl

  • Hero Member
  • *****
  • Posts: 8094
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #8 on: September 23, 2024, 06:57:29 pm »
Also newer Arduino examples change the serial setup code slightly so that it reads

Code: C  [Select][+][-]
  1. ...
  2.   SERIAL.begin(9600); // Note: some boards do not support 9600 Baud etc.
  3.   while (!SERIAL) ; // Wait for serial port to connect. Needed for Native USB only
  4. ...
  5.  

I believe that (!SERIAL) only applies to chips on the Arduino-family board with internal USB support, i.e. they don't have an FTDI chip etc. converting signals on the USB port to electrical signals ("TTL-level RS232" as some people insist on calling it).

However even ignoring the above there are two subtleties: (a) there are obviously /many/ chip families by now that are supported by the Arduino IDE and libraries (I've got at least AVR, ARM and RISC-V based boards, each of various types). (b) Even confining oneself to AVR chips, and even considering boards that are "traditional Arduino", and even- $DEITY help us- limiting consideration to "Arduino Uno", some specimens use a (knock-off) FTDI chip as the serial converter while others use another small AVR... and by now I've lost track of which of those appears as /dev/USB0 and which as /dev/ACM0.

And so on...

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 8094
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #9 on: September 23, 2024, 06:59:27 pm »
Yes I had a look at ping-arduino-loader/pingar.lpr.

Also see https://github.com/MarkMLl/serialcomms/blob/main/Term.pas etc.

I /think/ I gave you that link before, but otherwise I'm sure you spotted it when you looked at other stuff in the overall repo.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Aruna

  • Hero Member
  • *****
  • Posts: 568
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #10 on: September 23, 2024, 08:11:37 pm »
We have to dig a bit deeper:
 1) Check if Arduino is actually resetting by adding a couple of LED flashes in Setup().  This way you can visually see when the controller resets.  This will help in deciding whether the DTR signal is messing around.  Although the bootloader should also flash the onboard LED when the board is reset.

2) In the wiki Lazarus example TBlockSerial.Create is called every time a button is pressed.  Rather move TBlockSerial.Create to TForm1.OnShow and free it in TForm1.OnClose.  This way the DTR signal should only be asserted once when the Lazarus program starts.

3) If you have a spare USB-serial converter (or another Arduino board with an onboard USB-serial converter): connect the converter RX pin to the RX pin on the Arduino board, this way you can snoop the serial traffic in the Arduino serial monitor (or any other terminal) using the USB-serial converter.

4) If you have a logic analyser or oscilloscope (should be first step, but I assume OP is just getting started), confirm that the signal on the RX pin of the Arduino has the expected minimum pulse width (9600 baud => 104 microseconds).
Hello @ccrause thank you so much for all the pointers. I do have a pocket scope and am not new to electronics (digital or analog) I just figured out what the problem is. I will share it after double checking and grabbing some lunch. Talk to you soon and thank you for all the suggestions.

rvk

  • Hero Member
  • *****
  • Posts: 6641
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #11 on: September 25, 2024, 02:22:07 pm »
When testing from Lazarus I see the 'RX' and 'L' led's on the nano flicker but the led does not come on and stay on. I have tried changing baud rates, no luck. Can use some help in how best to move forward with this please.
Maybe I'm daft when it comes to Arduino programming... but aren't the RX and TX just for indicating data transmission??

They don't stay on !! Those are not the leds you are controlling with that code.

You control pin 13 (or whatever value you have in LED_BUILTIN) and you need to connect a led to that pin to see if it keeps on or blinks. So the leds for RX and TX are of no interest for you (other than knowing something is transmitted at that point).


MarkMLl

  • Hero Member
  • *****
  • Posts: 8094
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #12 on: September 25, 2024, 03:35:00 pm »
Maybe I'm daft when it comes to Arduino programming... but aren't the RX and TX just for indicating data transmission??

Specifically, they're on the AVR microcontroller pins being used as the serial port, which in the case of older Arduino models is followed by a chip (either a smaller AVR or an FTDI knockoff) providing a USB port for the PC. So as you observe, they just blink.

LED_BUILTIN is predefined for most if not all AVR-based Arduinos. It might not be predefined for other board types- and there are many- which use the Arduino IDE.

And finding out what is actually on a board can be a right pain, since (a) there are things that the IDE knows about the configuration which aren't passed to the compiler and (b) things like loader version, reset reason and so on might or might not be available.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Aruna

  • Hero Member
  • *****
  • Posts: 568
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #13 on: September 25, 2024, 03:55:50 pm »
When testing from Lazarus I see the 'RX' and 'L' led's on the nano flicker but the led does not come on and stay on. I have tried changing baud rates, no luck. Can use some help in how best to move forward with this please.
Maybe I'm daft when it comes to Arduino programming... but aren't the RX and TX just for indicating data transmission??
HI @rvk to clarify things:

On an Arduino board, the RX and TX LEDs indicate activity on the serial communication lines:
    RX (Receive): This LED blinks when the Arduino is receiving data from an external device (such as your computer) via the serial port. It monitors incoming data on the RX pin of the microcontroller.
    TX (Transmit): This LED blinks when the Arduino is sending data to an external device. It indicates activity on the TX pin, which is used for transmitting data from the Arduino.

They don't stay on !! Those are not the leds you are controlling with that code.
I am aware of this and thank you for clarifying. These LEDs are especially useful for debugging, as they give a visual indication of serial communication happening between your Arduino and other devices (like your computer or sensors).

You control pin 13 (or whatever value you have in LED_BUILTIN) and you need to connect a led to that pin to see if it keeps on or blinks. So the leds for RX and TX are of no interest for you (other than knowing something is transmitted at that point).
The RX and TX leds tell me that when I click a button in the ide my data has come across the serial port to the arduino and vice versa. So actually they are of great interest to me :-) and no your definitely not daft either.

rvk

  • Hero Member
  • *****
  • Posts: 6641
Re: One-way serial communication example using Synaser & Arduino Nano
« Reply #14 on: September 25, 2024, 03:58:25 pm »
O, it's about the led L.
That should be LED_BUILTIN and be on the board.
https://flaviocopes.com/arduino-built-in-led/
« Last Edit: September 25, 2024, 04:00:09 pm by rvk »

 

TinyPortal © 2005-2018