Recent

Author Topic: Serial port  (Read 2159 times)

Lauriet

  • New Member
  • *
  • Posts: 28
Serial port
« on: May 22, 2025, 04:23:55 am »
Everything is a file.......right ?

I want to use the serial port to simply write and read strings thru it.

Can I just:


assign(f, 'ttyxxx');
rewrite(f);
write(f, 'whatever');


Of course I would have to of setup the baudrate etc.


MarkMLl

  • Hero Member
  • *****
  • Posts: 8431
Re: Serial port
« Reply #1 on: May 22, 2025, 09:15:22 am »
No, you can't. WriteLn() etc. take a parameter of type text, not of type file.

You can do it with a lot of futzing around by messing with the low-level implementation of the buffers etc. as shown in the telnet server project in my Git repo, but (a) that's at the whim of the developers who might decide to break it and (b) it is likely to depend on the OS (and you've not told us what you're using).

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

Lauriet

  • New Member
  • *
  • Posts: 28
Re: Serial port
« Reply #2 on: May 22, 2025, 09:56:24 am »
Well, what if:

var
  f : text;

assign(f, '/dev/ttyUsb');  { for a usb to serial adaptor}
rewrite(f);
write(f, 'some stuff');

But doesn't /dev/ttyUsb exist as a file ???




CM630

  • Hero Member
  • *****
  • Posts: 1404
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: Serial port
« Reply #3 on: May 22, 2025, 12:26:53 pm »
I use https://github.com/JurassicPork/TLazSerial . I found no wiki for it, but it has some examples.
Лазар 4,0 32 bit (sometimes 64 bit); FPC3,2,2

Nimbus

  • Jr. Member
  • **
  • Posts: 56
Re: Serial port
« Reply #4 on: May 22, 2025, 12:33:52 pm »

Lauriet

  • New Member
  • *
  • Posts: 28
Re: Serial port
« Reply #5 on: May 23, 2025, 03:11:55 am »
Hmmmmmm  :-\

Maybe I need to google unix/linux sites cause I seem to remember in the dark past being able to use the port as a file. The other options will be OK........but, you know, enquiring minds must have answer.


Here is what "deep thought" says:
In Linux, serial ports are treated as special files within the /dev directory. This allows you to interact with them using standard file I/O operations and utilities, making it easy to read, write, and redirect data to and from serial devices.
« Last Edit: May 23, 2025, 03:14:19 am by Lauriet »

cdbc

  • Hero Member
  • *****
  • Posts: 2208
    • http://www.cdbc.dk
Re: Serial port
« Reply #6 on: May 23, 2025, 08:19:40 am »
Hi
Hmmm, if I were you, I would have a 'LookSee' at @MarkMLl's github thingy and see how he does it...  ;D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Re: Serial port
« Reply #7 on: May 23, 2025, 08:34:26 am »
it give the libusb library.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

MarkMLl

  • Hero Member
  • *****
  • Posts: 8431
Re: Serial port
« Reply #8 on: May 23, 2025, 09:02:07 am »
Hi
Hmmm, if I were you, I would have a 'LookSee' at @MarkMLl's github thingy and see how he does it...  ;D
Regards Benny

Basically, I'm pretty sure that people have looked at direct file access to (typically) /dev/ttyUSBn (on unix, YMMV) in the past and found it wanting. It might be worth trying again though, but there's lots of things like getting the control lines set that you have to use ioctl() for.

The serial.pp unit which is a standard part of the RTL is OK for Windows and Linux, although I've not done any maintentance on it recently. It's tested on SunOS/Solaris but not with anything else in the BSD family of OSes, i.e. including Macs. There's also another support file in one of my Github repos that does things like checking how much data is available: the comments go into some detail about a nasty kernel-level problem that affects Linux.

The telnet stuff includes setup of the low-level buffers that support WriteLn() etc. That probably isn't necessary /if/ going directly to the named port works... basically, I do a lot of instrumentation access and normally serial.pp plus possibly Str() etc. is entirely adequate.

MarkMLl
« Last Edit: June 17, 2025, 05:03:25 pm by 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: 1025
Re: Serial port
« Reply #9 on: May 23, 2025, 01:35:10 pm »
Basically, I'm pretty sure that people have looked at direct file access to (typically) /dev/ttyUSBn (on unix, YMMV) in the past and found it wanting. It might be worth trying again though, but there's lots of things like getting the control lines set that you have to use ioctl() for.

As said above, you can.  Below an example using virtual serial ports complements of socat (tested on Linux only):
Code: Bash  [Select][+][-]
  1. socat -d -d pty,rawer,echo=0 pty,rawer,echo=0
  2. 2025/05/23 12:38:35 socat[307365] N PTY is /dev/pts/11
  3. 2025/05/23 12:38:35 socat[307365] N PTY is /dev/pts/12
  4. 2025/05/23 12:38:35 socat[307365] N starting data transfer loop with FDs [5,5] and [7,7]

Use one endpoint as file in Pascal:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. var
  4.   f: text;
  5.  
  6. begin
  7.   Assign(f, '/dev/pts/12');
  8.   rewrite(f);
  9.   write(f, 'Text from FPC');
  10.   CloseFile(f);
  11. end.

Open the other end point in a terminal like app, in this case minicom:
Code: Bash  [Select][+][-]
  1. Welcome to minicom 2.8
  2.  
  3. OPTIONS: I18n
  4. Port /dev/pts/11, 12:42:24
  5.  
  6. Press CTRL-A Z for help on special keys
  7.  
  8. Text from FPC

So it "works" (reading data quickly becomes challenging), but as MarkMLl said, you don't have access to settings such as baud rate, parity, stop bits, blocking / non blocking transmission etc.  There is a good reason for having several serial port libraries and components - quite a bit more than just file I/O is typically needed to interface with a serial port.

Lauriet

  • New Member
  • *
  • Posts: 28
Re: Serial port
« Reply #10 on: May 24, 2025, 06:44:02 am »
Socat looks interesting:

would I have to set baudrates and stuff beforehand ???

Lauriet

  • New Member
  • *
  • Posts: 28
Re: Serial port
« Reply #11 on: May 24, 2025, 06:46:34 am »
I've tried serial.pp but the SerOpen('/dev/ttyACM0') returns with -1.......so i guess it wasn't opened.

Did i give it the right param ???

af0815

  • Hero Member
  • *****
  • Posts: 1392
Re: Serial port
« Reply #12 on: May 24, 2025, 08:17:07 am »
-1 can be returned if the device is used by another process or not correct freed from an older process. And only one process can work with this device and you must have the rights to use the device. By default a normal user have no rights for the devices.
regards
Andreas

ccrause

  • Hero Member
  • *****
  • Posts: 1025
Re: Serial port
« Reply #13 on: May 24, 2025, 08:35:21 am »
Socat looks interesting:

It is useful to perform testing without hardware. Since socat is software emulation it cannot as AFAIK emulate baud rate mismatches.

Quote

would I have to set baudrates and stuff beforehand ???
If you are referring to assigning a serial port as a file, then yes the baud rate etc should be configured separately from Pascal's file handling.

ccrause

  • Hero Member
  • *****
  • Posts: 1025
Re: Serial port
« Reply #14 on: May 24, 2025, 08:41:47 am »
-1 can be returned if the device is used by another process or not correct freed from an older process. And only one process can work with this device and you must have the rights to use the device. By default a normal user have no rights for the devices.
+1

Check permissions of /dev/ttyUSB0, does it include your username or a group which you belong to?

 

TinyPortal © 2005-2018