User Tools

Site Tools


using_serial_ports_in_linux

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
using_serial_ports_in_linux [2022/03/03 11:20]
richardrussell created
using_serial_ports_in_linux [2022/03/06 22:15] (current)
richardrussell Added notes on permissions
Line 11: Line 11:
 So to avoid your BASIC program 'blocking' in a statement like INPUT# whilst it waits for data to arrive, there needs to be some way to test how much data is 'waiting' in an input buffer.  Then the program can continue to perform useful tasks whilst it waits. So to avoid your BASIC program 'blocking' in a statement like INPUT# whilst it waits for data to arrive, there needs to be some way to test how much data is 'waiting' in an input buffer.  Then the program can continue to perform useful tasks whilst it waits.
  
-//BBC BASIC for Windows// provides built-in features to configure a serial port (as an extension of the **OPENUP** function) and to test for how much data is waiting in an input buffer (**EXT#** function), but //BBC BASIC for SDL 2.0// does not (because SDL 2.0 itself, which BBCSDL relies on to provide the cross-platform abstraction layer, doesn't). +To configure and open a serial port in Linux you should use code similar to the following (in this case the port **/dev/ttyUSB0** is being configured for 1 stop bit, 8 data bits, no parity and a speed of 9600 baud).  Note the addition of a trailing dot in the OPENUP function to suppress appending a **.bbc** extension:
- +
-To configure and open a serial port in Linux you should use code similar to the following (in this case the port **/dev/ttyUSB0** is being configured for 1 stop bit, 8 data bits, no parity and a speed of 9600 baud):+
  
 <code bb4w> <code bb4w>
-      OSCLI "ldattach -1 -8 -n -s 9600 0 /dev/ttyUSB0" +      OSCLI "ldattach -1 -8 -n -s 9600 0 /dev/ttyUSB0;
-      OSCLI "stty -F /dev/ttyUSB0 9600 raw"+      OSCLI "stty -F /dev/ttyUSB0 9600 raw;"
       serial% = OPENUP("/dev/ttyUSB0.")       serial% = OPENUP("/dev/ttyUSB0.")
 </code> </code>
  
-To test how much data is 'waiting' in an input buffer, and can therefore be read safely without blocking, use function similar to this:+To test how much data is 'waiting' in an input buffer, and can therefore be read safely without blocking, use the EXT# functionsimilar to this:
  
 <code bb4w> <code bb4w>
-      DEF FNavail(N%) +      WHILE EXT#serial
-      LOCAL F%  +        byte& = BGET#serial% 
-      SYS "fileno", @hfile%(N%)!28 TO F% +        REM Do something with the data here 
-      SYS "ioctl", F%, &541B, ^N% : REM FIONREAD +      ENDWHILE 
-      = N%+      REM Continue with any background tasks
 </code> </code>
 +
 +====Notes on permissions====
 +
 +Typically to access a serial port in Linux you will either need root privileges (e.g. by using **sudo**) or
 +be a member of the **dialout** group.  You can add the current user to the dialout group as follows:
 +
 +<code bash>
 +sudo usermod -a -G dialout $USER
 +</code>
 +
 +For this change to become active it is likely that you will need to restart your PC.
  
using_serial_ports_in_linux.1646306427.txt.gz · Last modified: 2022/03/03 11:20 by richardrussell