RPICSIO
The SerialPortFS Class
The SerialPortFS Class
Provides the Serial Port functionality for a Raspberry Pi. This is the SYSFS version
Note that by default the default Raspian operating system (at the time of writing this documentation) uses serial port AMA0 for the Console serial port. You will need to disable this functionality before you can use the serial port. See the notes on the Serial Port Example page for more information.
IMPORTANT NOTE: Before creating any RPICSIO Port class you will need to ensure
that the RPICSIO Library knows which type of Raspberry Pi you are dealing with. This is done by executing a call (only once is necessary) like the one below somewhere
in your code before you create any port.
RPICSIOConfig.Instance.RPIType = RPITypeEnum.RPITYPE_RPI2;
Be aware that you need to ensure the Serial port is configured (using raspi-config
before this port will work.
VERY IMPORTANT NOTE:
The pins (including the serial port pins) on the Raspberry Pi are 3.3Volt. You CANNOT connect them up to a 5V serial device without level shifting the voltage or you will burn out the RPI. You DEFINITELY CANNOT connect them to the +/- 18V of a true RS232 serial port (on the back of a PC) because your RPI will instantly die.
Source Code
The source code for this class is available online for
download
and also in
browseable form.
Example Code
Example code which demonstrates the use of this class can be found on the Serial Port Example page.
Syntax
public class SerialPortFS : PortFS
Provides the Serial Port functionality for a Raspberry Pi. (SYSFS version)
Public Constructors
public SerialPortFS(SerialPortEnum serialPortIn, SerialPortOpenModeEnum openMode) : base(GpioEnum.GPIO_NONE)
Constructor.
- Parameters
- serialPortIn - The serial port to use.
- openMode - the read mode (BLOCK or NONBLOCK) in which we open the serial port.
The blocking mode refers to to the way the serial port code deals with a request to read more data than is present in the input queue. If the port is in OPEN_BLOCK mode and more data is requested than is present then the call will wait until the required amount of data arrives. In OPEN_NONBLOCK mode the port will just return what it has.
Public Methods
public void Break()
Removes all data from the Rx and Tx queues. The equivalent of the termios.c tcflush call
public override void ClosePort()
Closes the port.
public void DumpTermiosStruct(TermiosStruct xfer)
Dumps out the Termios structure to the console. For diagnostic purposes only.
public void Dispose()
Implements IDisposable. Should be called after the class is closed to release resources.
public void Flush()
Removes all data from the Rx and Tx queues. The equivalent of the termios.c tcflush call
public TermiosStruct GetTermiosState()
Gets the Termios structure. The equivalent of the termios.c tcgetattr call
public override PortDirectionEnum PortDirection()
Gets the PortDirection
- Returns
- A PortDirectionEnum value. This value is always PortDirectionEnum.PORTDIR_OUTPUT.
public int ReadByteArray(byte[] rxByteBuf, int numBytes)
Reads a buffer in from an Serial Device. We choose to structure this so that the caller passes in the byte array. This allows it to be re-used. If we just create and return one here then C# will have to deal with a lot of garbage collection (potentially) on very busy calls which would slow things down.
- Parameters
- rxByteBuf, The buffer in which we store the bytes read.
- numBytes, The max number of bytes to read.
- Returns
- The number of bytes read.
public string ReadString(int maxBytes)
Reads a Serial Device and returns the contents as a string. This is
not especially efficient in terms of memory use as both the return
string and an intermediate byte[] array need to be created and
subsequently garbage collected.
- Parameters
- numBytes, The max number of bytes to read.
- Returns
- a string with the contents of the serial port receive
buffer up to the specified maxBytes. If nothing is ready to be
read on the serial port an empty string will be returned.
public void SetTermiosState(ref TermiosStruct xfer)
Sets the Termios structure. The equivalent of the termios.c
tcsetattr call.
- Parameters
- xfer, The termios structure to set.
public void Write(byte[] txByteBuf, int numBytes)
Writes a byte array out to an Serial Device.
- Parameters
- txByteBuf, The byte array buffer with bytes to write.
- numBytes, The number of bytes to send
public void Write(string outStr)
Writes a string out to an Serial Device as a series of ASCII bytes.
- Parameters
- outStr, The string to write.
Public Properties
public SerialPortBaudRateEnum BaudRate { get; set; }
Gets/Sets the baud rate
- value
- A SerialPortBaudRateEnum value.
public SerialPortBitLengthEnum BitLength { get; set; }
Gets/Sets the bit length of the bytes transmitted via the RPI Serial port
- value
- A SerialPortBitLengthEnum value.
public int BytesInRxBuffer { get; }
Gets the number of pending (unread) bytes in the read buffer of the
UART.
- value
- The number of pending (unread) bytes.
public SerialPortParityEnum Parity { get; set; }
Gets/Sets the parity bits of the bytes transmitted via the RPI Serial port
- value
- A SerialPortParityEnum value.
public SerialPortEnum SerialPort { get; }
Gets the Serial Port. There is no Set accessor this is set in the constructor.
- value
- A SerialPortEnum value.
public SerialPortStopBitsEnum StopBits { get; set; }
Gets/Sets the stop bit setting of the bytes transmitted via the RPI Serial port
- value
- A SerialPortStopBitsEnum value.