Recent

Author Topic: Lazarus and Arduino stops working on Synaser.  (Read 879 times)

microguy

  • Jr. Member
  • **
  • Posts: 57
Lazarus and Arduino stops working on Synaser.
« on: September 30, 2024, 11:36:14 pm »
I wrote a small program on Lazarus and Ardunio with synaser. It was working fine to turn on and off on those two LEDs. Now it is not working. I have reboot PC and other programs. I could not figure out what went wrong. Hope someone can help me with this.

Here are my codes for Arduino Nano

int led6 = 6; // Pin D6
int led7 = 7; // Pin D7

void setup()
{
  pinMode(led6, OUTPUT); // Set pin D6 as digital out
  pinMode(led7, OUTPUT); // Set pin D7 as digital out

  // Start up serial connection
  Serial.begin(9600); // baud rate
  Serial.flush();
}

void loop()
{
  String input = "";
 
  // Read any serial input
  while (Serial.available() > 0)
  {
    input += (char) Serial.read(); // Read in one char at a time
    delay(20); // Delay for 5 ms so the next char has time to be received

  // led6
  if (input == "on6")
    {
      digitalWrite(led6, HIGH); // on
      Serial.println("Led6 is On");
    }
  else if (input == "off6")
    {
      digitalWrite(led6, LOW); // off
      Serial.println("Led6 is Off");
    }
  // led7
  if (input == "on7")
    {
      digitalWrite(led7, HIGH); // on
      Serial.println("Led7 is On");
    }
  else if (input == "off7")
    {
      digitalWrite(led7, LOW); // off
      Serial.println("Led7 is Off");
    }
  }
}

Code for Lazarus

procedure TForm1.FormCreate(Sender: TObject);
begin
  ser := TBlockSerial.Create;
  Sleep(25); //try 250  notworking
  ser.Connect('COM4'); // write here Arduino COM port number I did make sure right COM4.
  Sleep(25);  // not woring with 250
  ser.Config(9600, 8, 'N', SB1, False, False);
  ser.RTS := false; // comment this if needed
  ser.DTR := false; // comment this if needed
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  ser.free;
end; 

procedure TForm1.Button1Click(Sender: TObject);
begin
  // "OFF6"
  Ser.SendString('off6');
  Shape2.brush.Color:=clRed;
end;             

procedure TForm1.Button2Click(Sender: TObject);
begin
  // "ON6"
  Ser.SendString('on6');
  Shape2.brush.Color:=clGreen;
end;             

procedure TForm1.Button3Click(Sender: TObject);
begin
  // "OFF7"
  Ser.SendString('off7');
  Shape2.brush.Color:=clRed;
end;             

procedure TForm1.Button4Click(Sender: TObject);
begin
  // "ON7"
  Ser.SendString('on7');
  Shape2.brush.Color:=cGreen;
end;             




Curt Carpenter

  • Hero Member
  • *****
  • Posts: 564
Re: Lazarus and Arduino stops working on Synaser.
« Reply #1 on: October 01, 2024, 12:44:24 am »
Can you verify that your serial link is working?

avra

  • Hero Member
  • *****
  • Posts: 2532
    • Additional info
Re: Lazarus and Arduino stops working on Synaser.
« Reply #2 on: October 01, 2024, 01:24:27 am »
If you want your embedded systems to run non-stop without freezing then educate yourself about:
1. Watchdog timer (software hang protection - usually internal)
2. Brown-out detection (hardware valid voltage range protection - internal or external)

AVR based Arduino can handle first by coding, and second by burning proper fuse from your programmer software.

Btw. Check your delay...
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: Lazarus and Arduino stops working on Synaser.
« Reply #3 on: October 01, 2024, 09:51:45 pm »
Here are my codes for Arduino Nano



Code for Lazarus

Please use [code][/code]-tags to avoid the forum software potentially interpreting the code and to make it better readable.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8032
Re: Lazarus and Arduino stops working on Synaser.
« Reply #4 on: October 02, 2024, 09:03:20 am »
OP's been asking variants of this question since 2017, so can hardly claim to be unfamiliar with the forum's conventions.

The one thing that has repeatedly come out of it is that the doesn't understand data communications, and has absolutely no interest in reading chip datasheets etc.

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

robert rozee

  • Full Member
  • ***
  • Posts: 182
Re: Lazarus and Arduino stops working on Synaser.
« Reply #5 on: October 02, 2024, 01:38:37 pm »
OP's been asking variants of this question since 2017, so can hardly claim to be unfamiliar with the forum's conventions.

The one thing that has repeatedly come out of it is that the doesn't understand data communications, and has absolutely no interest in reading chip datasheets etc.

MarkMLl

MarkMLI: your reply is mean-spirited and impolite. if you can not say something constructive then you would be better off saying nothing.

microguy: the main problem with your Arduino program is that it assumes that the whole command ("on6", "off6", etc) arrives at the Arduino without any delays between characters, and that there are no extra characters (such as carriage-return or line-feed) directly after a command. you may be better off starting off with just using single-letter commands. how about:
'A' = turn on LED6,
'a' = turn offf LED6,
'B' = turn on LED7,
'b' = turn offf LED7.

your loop routine then becomes:

Code: C  [Select][+][-]
  1. void loop()
  2. {
  3.     while (Serial.available() > 0)             // Loop while characters are available
  4.     {
  5.         char input = Serial.read();            // Read in next available character
  6.  
  7.         if (input == 'A')                      // led6 ON
  8.         {
  9.             digitalWrite(led6, HIGH); // on
  10.             Serial.println("Led6 is On");
  11.         }
  12.         if (input == 'a')                      // led6 OFF
  13.         {
  14.             digitalWrite(led6, LOW); // off
  15.             Serial.println("Led6 is Off");
  16.         }
  17.  
  18.         if (input == 'B')                      // led7 ON
  19.         {
  20.             digitalWrite(led7, HIGH); // on
  21.             Serial.println("Led7 is On");
  22.         }
  23.         if (input == 'b')                      // led7 OFF
  24.         {
  25.             digitalWrite(led7, LOW); // off
  26.             Serial.println("Led7 is Off");
  27.         }
  28.     }
  29. }

you now don't need to add in any delays waiting for the next serial character to arrive. i would suggest testing this out with a terminal emulator running at the PC end first, and once you are happy it is working then move on to using a program written using Lazarus/FPC.

to handle complex commands like "on6" you need to accumulate characters received into a buffer, and each time a character is added to that buffer check at the end of the buffer for a matching command. whenever a matching command is found empty the buffer, and whenever the buffer gets full delete the first character to make space for the next one received to be stored at the end.


cheers,
rob   :-)   
« Last Edit: October 02, 2024, 01:50:27 pm by robert rozee »

microguy

  • Jr. Member
  • **
  • Posts: 57
Re: Lazarus and Arduino stops working on Synaser.
« Reply #6 on: October 03, 2024, 06:04:48 am »
Can you verify that your serial link is working?

I am using Arduino IDE  Serial monitor and it is working when I send code to Nano. But it is not working from Lazarus to Nano.

microguy

  • Jr. Member
  • **
  • Posts: 57
Re: Lazarus and Arduino stops working on Synaser.
« Reply #7 on: October 03, 2024, 06:09:14 am »
If you want your embedded systems to run non-stop without freezing then educate yourself about:
1. Watchdog timer (software hang protection - usually internal)
2. Brown-out detection (hardware valid voltage range protection - internal or external)

AVR based Arduino can handle first by coding, and second by burning proper fuse from your programmer software.

Btw. Check your delay...

Avra,

Sorry for delay. I have some trouble to login this Forum and it gives me an error.  Anyway. I am kind of new on WatchDog timer.  I will study and how to use it.  I notice it is for Arduino Uno. But not sure if it will work with Arduino Nano. I will do more google on it.

microguy

  • Jr. Member
  • **
  • Posts: 57
Re: Lazarus and Arduino stops working on Synaser.
« Reply #8 on: October 03, 2024, 06:54:03 am »

microguy: the main problem with your Arduino program is that it assumes that the whole command ("on6", "off6", etc) arrives at the Arduino without any delays between characters, and that there are no extra characters (such as carriage-return or line-feed) directly after a command. you may be better off starting off with just using single-letter commands. how about:
'A' = turn on LED6,
'a' = turn offf LED6,
'B' = turn on LED7,
'b' = turn offf LED7.

your loop routine then becomes:

Code: C  [Select][+][-]
  1. void loop()
  2. {
  3.     while (Serial.available() > 0)             // Loop while characters are available
  4.     {
  5.         char input = Serial.read();            // Read in next available character
  6.  
  7.         if (input == 'A')                      // led6 ON
  8.         {
  9.             digitalWrite(led6, HIGH); // on
  10.             Serial.println("Led6 is On");
  11.         }
  12.         if (input == 'a')                      // led6 OFF
  13.         {
  14.             digitalWrite(led6, LOW); // off
  15.             Serial.println("Led6 is Off");
  16.         }
  17.  
  18.         if (input == 'B')                      // led7 ON
  19.         {
  20.             digitalWrite(led7, HIGH); // on
  21.             Serial.println("Led7 is On");
  22.         }
  23.         if (input == 'b')                      // led7 OFF
  24.         {
  25.             digitalWrite(led7, LOW); // off
  26.             Serial.println("Led7 is Off");
  27.         }
  28.     }
  29. }

you now don't need to add in any delays waiting for the next serial character to arrive. i would suggest testing this out with a terminal emulator running at the PC end first, and once you are happy it is working then move on to using a program written using Lazarus/FPC.

to handle complex commands like "on6" you need to accumulate characters received into a buffer, and each time a character is added to that buffer check at the end of the buffer for a matching command. whenever a matching command is found empty the buffer, and whenever the buffer gets full delete the first character to make space for the next one received to be stored at the end.


cheers,
rob   :-)   

Rob,

I tried to use your code with one letter and it did not work with Lazarus but I did tried Arduino Ide Serial Monitor and got it working with single letter.  So I guess something wrong with my Lazarus. I am using synaser. Can someone tell me where I can get Terminal Emulator. Do I need a second computer to test Terminal Emulator with Lazarus?

Reid

robert rozee

  • Full Member
  • ***
  • Posts: 182
Re: Lazarus and Arduino stops working on Synaser.
« Reply #9 on: October 03, 2024, 11:19:07 am »
Rob,
I tried to use your code with one letter and it did not work with Lazarus but I did tried Arduino Ide Serial Monitor and got it working with single letter.  So I guess something wrong with my Lazarus. I am using synaser. Can someone tell me where I can get Terminal Emulator. Do I need a second computer to test Terminal Emulator with Lazarus?
Reid

1. i take it you are using Window 10 or similar? in which case it is not possible to have both the Arduino Ide Serial Monitor and your Lazarus program accessing the serial port at the same time - this is a limitation of Windows. so you would need to close the Arduino Ide Serial Monitor before running your Lazarus program. this limitation applies to any two Windows programs that may be trying to access the same serial port at once.

2. i would change the line ser.Config(9600, 8, 'N', SB1, False, False); to instead use 2 stop bits, ie, ser.Config(9600, 8, 'N', SB2, False, False);, although i'm not too sure if this will make much difference. unfortunately my knowledge of Synaser is rather limited as i've never used it. i prefer to use lower-level routines that are OS specific myself, and do most of my work with Linux.

3. after calling ser.Connect and ser.Config you may wish to add lines to check LastError and LastErrorDesc to ensure that you are successfully opening and configuring the serial port - my guess is that Synaser does not quit your program upon encountering any errors. something like this will likely work:
Code: Pascal  [Select][+][-]
  1. ser.Connect('COM4');
  2. if ser.LastError<>0 then ShowMessage('ser.Connect Error: '+ ser.LastErrorDesc);
  3. ser.Config(9600, 8, 'N', SB2, False, False);
  4. if ser.LastError<>0 then ShowMessage('ser.Config Error: '+ ser.LastErrorDesc);

let us know how you get along. i've been looking at the documentation here:
http://synapse.ararat.cz/doc/help/synaser.TBlockSerial.html
although there may be far better documentation elsewhere. can anyone else reading this provide a pointer to some example code that shows how to use Synaser?


cheers,
rob   :-)
« Last Edit: October 03, 2024, 11:23:30 am by robert rozee »

microguy

  • Jr. Member
  • **
  • Posts: 57
Re: Lazarus and Arduino stops working on Synaser.
« Reply #10 on: October 03, 2024, 11:59:30 pm »
I got it working now. I am using the new Arduino IDE and I did not realized that I had Serial monitor stay online. That is why Lazarus is not working. I had to turn it off the serial monitor first before run  Lazarus program and got it working now. Thank you for all of you who help me with this. Now I am working to add some switches on Arduino Nano. Hope I can figure out how to gate data from Ardunino to Lazarus.

Reid

ccrause

  • Hero Member
  • *****
  • Posts: 970
Re: Lazarus and Arduino stops working on Synaser.
« Reply #11 on: October 04, 2024, 07:26:21 am »
I got it working now. I am using the new Arduino IDE and I did not realized that I had Serial monitor stay online. That is why Lazarus is not working. I had to turn it off the serial monitor first before run  Lazarus program and got it working now. Thank you for all of you who help me with this. Now I am working to add some switches on Arduino Nano. Hope I can figure out how to gate data from Ardunino to Lazarus.

Reid
I hope the practice of error checking now makes sense. Robert Rozee's latest post is a good illustration of error checking.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8032
Re: Lazarus and Arduino stops working on Synaser.
« Reply #12 on: October 04, 2024, 08:41:12 am »
I got it working now. I am using the new Arduino IDE and I did not realized that I had Serial monitor stay online. That is why Lazarus is not working. I had to turn it off the serial monitor first before run  Lazarus program and got it working now. Thank you for all of you who help me with this. Now I am working to add some switches on Arduino Nano. Hope I can figure out how to gate data from Ardunino to Lazarus.

Well done, I'd forgotten that detail. Now for goodness sake keep at this, rather than dropping it for a year or so and then starting off cold: which I can assure you is frustrating for /everybody/.

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

 

TinyPortal © 2005-2018