Recent

Author Topic: Use ttyS0, exsample code? For Linux  (Read 1607 times)

coradi

  • Full Member
  • ***
  • Posts: 167
Use ttyS0, exsample code? For Linux
« on: September 20, 2024, 08:01:58 pm »
Hallo,
is tehre any exsample code for using Serial ports with USb Converter (ttyS0)?
A simple open Port, and read port, or send and read back from same RX/TX?
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8101
Re: Use ttyS0, exsample code? For Linux
« Reply #1 on: September 20, 2024, 09:18:23 pm »
https://github.com/MarkMLl/serialcomms plus several others in the same 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

coradi

  • Full Member
  • ***
  • Posts: 167
Re: Use ttyS0, exsample code? For Linux
« Reply #2 on: September 20, 2024, 10:47:30 pm »
Great, I will try
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8101
Re: Use ttyS0, exsample code? For Linux
« Reply #3 on: September 21, 2024, 09:20:46 am »
That's obviously based on the standard serial.pp, which hasn't had any attention for many years /but/ was tested on Linux, Windows (2Kish?) and SunOS/Solaris (which should be fairly similar to MacOS or whatever it's called).

It extended work that somebody else had done, but I was specifically focusing on low-level access and the calls will typically be in their own thread. At the time I was working on a serial comms analyzer (to capture the protocol between two commercial comms analyzers) and I very much wanted each captured byte to have a timestamp and a snapshot of the control line states.

On Windows it gets sole-access to the port, on unix it doesn't (which is something I'd be inclined to change). Also it doesn't have a SerAvailable(), but in Linux that has a gotcha: the result is truncated at 4K. More details in that repo, which also has stuff to detect various USB devices (which in practice are often built into e.g. a multimeter, Arduino 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

coradi

  • Full Member
  • ***
  • Posts: 167
Re: Use ttyS0, exsample code? For Linux
« Reply #4 on: November 18, 2024, 11:00:08 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: [Select]
program uart19200;

{$mode objfpc}{$H+}

uses
  cthreads, SysUtils, synaser;

var
  Serial: TBlockSerial;
  ReceivedData: string;
begin
 // WriteLn('Empfangene Daten: ');
  // Serielle Schnittstelle initialisieren
  Serial := TBlockSerial.Create;
  try
    // Serielle Schnittstelle öffnen
    Serial.Connect('/dev/ttyUSB0'); // Passe den Pfad hier an
    Serial.Config(19200, 8, 'N', SB1, False, False);

    // Empfangen von Daten
    WriteLn('Warte auf Daten...');

    while True do
    begin


      if Serial.WaitingData > 0 then
      begin
        ReceivedData := Serial.Recvstring(1000); // 1 Sekunde Timeout
        if ReceivedData <> '' then
        begin
          WriteLn('Empfangene Daten: ', ReceivedData);
        end;
      end
      else
        Sleep(100); // Warte kurz, um CPU-Last zu reduzieren
    end;

  except
    on E: Exception do
      WriteLn('Fehler: ', E.Message);
  end;

  Serial.Free;
end.             
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

MarkMLl

  • Hero Member
  • *****
  • Posts: 8101
Re: Use ttyS0, exsample code? For Linux
« Reply #5 on: November 19, 2024, 09:01:37 am »
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

Don't post ChatGPT code here in case a subsequent search engine takes it as authoritative.

I pointed you at tested code which you said you'd try: what happened?

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

Thaddy

  • Hero Member
  • *****
  • Posts: 16363
  • Censorship about opinions does not belong here.
Re: Use ttyS0, exsample code? For Linux
« Reply #6 on: November 19, 2024, 09:11:51 am »
Furthermore, please resolve any problems with chatGPT presented or generated code yourself, before posting.
There is nothing wrong with being blunt. At a minimum it is also honest.

ccrause

  • Hero Member
  • *****
  • Posts: 988
Re: Use ttyS0, exsample code? For Linux
« Reply #7 on: November 19, 2024, 11:13:47 am »
With hterm the LED from CH UART is working, but with this code, nothing happends
...
Which LED?
What is CH UART?
What actions did you perform in hterm?
The code example you refer to only receives serial data, thus there will be no noticeable activity on the USB converter unless something else is transmitting data on the converter's RX pin.  What do you expect should happen when you only receive data in the test code?

coradi

  • Full Member
  • ***
  • Posts: 167
Re: Use ttyS0, exsample code? For Linux
« Reply #8 on: November 21, 2024, 12:44:16 pm »
nobody?
Amstrad Schneider CPC 6128
AVR8/ARM(STM32)

Thaddy

  • Hero Member
  • *****
  • Posts: 16363
  • Censorship about opinions does not belong here.
Re: Use ttyS0, exsample code? For Linux
« Reply #9 on: November 21, 2024, 12:50:45 pm »
nobody?
Which LED?
What is CH UART?
What actions did you perform in hterm?
The code example you refer to only receives serial data, thus there will be no noticeable activity on the USB converter unless something else is transmitting data on the converter's RX pin.  What do you expect should happen when you only receive data in the test code?
Why do you not answer those questions? ccrause is an expert. He needs that information, understand?
There is nothing wrong with being blunt. At a minimum it is also honest.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8101
Re: Use ttyS0, exsample code? For Linux
« Reply #10 on: November 21, 2024, 12:56:23 pm »
nobody?

Have you actually read the messages posted in this thread?

What more do you expect anybody to say?

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