Lazarus

Programming => Operating Systems => Linux => Topic started by: coradi on December 03, 2024, 09:50:21 pm

Title: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: coradi on December 03, 2024, 09:50:21 pm
This example is from chatgpt,but why it doesn't work?
With hterm the LED from CH UART is working, but with this code, nothing happends

Code: Pascal  [Select][+][-]
  1. program uart19200;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   cthreads, SysUtils, synaser;
  7.  
  8. var
  9.   Serial: TBlockSerial;
  10.   ReceivedData: string;
  11. begin
  12.  // WriteLn('Empfangene Daten: ');
  13.   // Serielle Schnittstelle initialisieren
  14.   Serial := TBlockSerial.Create;
  15.   try
  16.     // Serielle Schnittstelle öffnen
  17. Modify message
  18.  
Title: Re: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: Jurassic Pork on December 04, 2024, 02:18:37 am
hello,
i don't see in your code init of the serial port.
Friendly, J.P
Title: Re: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: Thaddy on December 04, 2024, 06:28:12 am
You need to ask the question more precise:
Code: Pascal  [Select][+][-]
  1. program SerialPortListener;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   cthreads, SysUtils, synaser;
  7.  
  8. var
  9.   Serial: TBlockSerial;
  10.   ReceivedData: string;
  11.  
  12. begin
  13.   // Initialize the serial port
  14.   Serial := TBlockSerial.Create;
  15.   try
  16.     // Set the device to the appropriate serial port (e.g., /dev/ttyS0 or /dev/ttyUSB0)
  17.     Serial.Connect('/dev/ttyS0');
  18.     // Set the baud rate to 19200, 8 data bits, no parity, 1 stop bit
  19.     Serial.Config(19200, 8, 'N', SB1, False, False);
  20.  
  21.     // Loop to read data from the serial port
  22.     while True do
  23.     begin
  24.       // Check if data is available
  25.       if Serial.CanRead(1000) then
  26.       begin
  27.         // Read the data
  28.         ReceivedData := Serial.RecvPacket(1000);
  29.         // Print the received data
  30.         WriteLn('Received: ', ReceivedData);
  31.       end;
  32.     end;
  33.   finally
  34.     // Clean up
  35.     Serial.Free;
  36.   end;
  37. end.
Not my code, chatGPT  :D :D This one works if the synapse library is in the path or the package is installed. (just tested, minimally on /dev/ttyUSB0, the above code is untouched)

Both synaser examples from the wiki also work when you set up the config for your conection:
https://wiki.freepascal.org/Hardware_Access#Serial_Communication
Title: Re: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: coradi on December 04, 2024, 04:38:31 pm
same..nothing happens
No blinking RX LED or something.
With Hterm it works well
I use /dev/ttyUSB0
Title: Re: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: cdbc on December 04, 2024, 05:54:35 pm
Hi
Just checking...
You are trying to fit Thaddy's example to use your local values, right?!?
...Not just copy-pasting and hoping...  ;D
Regards Benny
Title: Re: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: af0815 on December 04, 2024, 05:59:52 pm
The samples are for reading data on the computer side, not for sending. If you use HTerm i think, you are sending data to the serial device.
Title: Re: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: Thaddy on December 04, 2024, 07:04:36 pm
That works almost identical. will add that example tomorrow.
Title: Re: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: coradi on December 04, 2024, 09:37:12 pm
At this moment I only need reading. I don'T use hterm.
I used it only to check if I can get Data here, and this work, in Hterm I receive data.
But in PAscal, nothing happens, when I end hterm for free the com Port.

Yes, I only copy paste this and take a look into the Console output Window
Title: Re: Use tts0, This example is from chatgpt,but why it doesn't work?
Post by: coradi on December 15, 2024, 08:07:57 pm
Maybe the problem is, that the sending string has no /0 at the end?
But ow can I receive it then, if  can't mody the sending string?
TinyPortal © 2005-2018