SerialEthernet()
Description
Sends strings of text to a TCP server via Ethernet using Arduino Leonardo with the Arduino Ethernet Shield as a serial slave device controlled by Bpod.
Arduino Leonardo must be connected by USB to the same computer as Bpod.
The Bpod Arduino shield must be plugged into the Arduino Ethernet shield.
A CAT6 Ethernet cable (see Bpod Arduino shield BOM) must connect the Bpod Arduino shield to one of the two Bpod serial ports.
Load the SerialEthernet firmware to the Arduino Leonardo board: /Bpod/Firmware/SerialEthernetModule/SerialEthernetModule.ino
The SerialEthernet module acquires its own IP address (DHCP), and connects to a remote TCP server when given its IP and port.
The user can load up to 32 text strings before each trial, if they total under 2kB.
Bytes received on the hardware serial port (not USB) trigger the corresponding text string (1-32) to be sent to the remote server with <1ms latency.
Transmission mode can be configured - "0" = normal, "1" = TCP connection is closed and re-opened after each transmission.
If initial connection to a remote TCP server fails, the plugin throws an error. Otherwise, it reports a successful connection to the MATLAB command window.
Syntax
To initialize:
SerialEthernet('Init', SerialPort)
To connect to a remote TCP server:
SerialEthernet('Connect', RemoteIP, RemotePort)
To set the transmission mode:
SerialEthernet('MessageMode', Mode)
To load a text string to the SerialEthernet module:
SerialEthernet('LoadString', StringID, String)
To test-send a string:
SerialEthernet('TriggerString', StringID)
To close the SerialEthernet module:
SerialEthernet('close')
Parameters
SerialPort: A string specifying the Arduino Leonardo serial port (e.g. 'COM3' or '/dev/ttyUSBS101')
RemoteIP: The IP address of the remote TCP server, formatted as a vector of 4 integers (e.g. [192 168 1 101]).
RemotePort: The port of the remote TCP server (a positive integer in the range 1-65535)
Mode: Some TCP connections on Ethernet enabled devices automatically close if held open for too long. If Mode=0, strings are sent and the connection is left open. If Mode=1, the TCP connection is closed and re-opened after each string is sent. Note that this may affect how frequently you can send strings.
StringID: A byte specifying which string to load or test-send (1-32).
String: A character array with the text string to load.
The total of all strings loaded must be under 2KB. Strings can be re-loaded between trials.
Returns
-None
Example
% 1. This code connects to a remote TCP server, loads a string and test-sends it.
RemoteIP = [192 168 1 101]; RemotePort = 3336; % Set parameters
Message = 'Hello TCP server. I am Bpod.';
SerialEthernet('Init', 'COM3'); % Initialize SerialEthernet on port COM3
SerialEthernet('Connect', RemoteIP, RemotePort); % Connect to the remote TCP server
SerialEthernet('LoadString', 3, Message); % Load a string into slot 3 of 32
SerialEthernet('TriggerString', 3); % Test-send string 3 to the TCP server
SerialEthernet('close'); % Close connection to SerialEthernet
% 2. This code connects to a remote TCP server, loads a string and creates a state matrix to trigger it.
% the code assumes that the SerialEthernet module is connected to Bpod serial port 1 (of 2).
RemoteIP = [192 168 1 101]; RemotePort = 3336; % Set parameters
Message = 'Hello TCP server. I am Bpod.';
SerialEthernet('Init', 'COM3'); % Initialize SerialEthernet on port COM3
SerialEthernet('Connect', RemoteIP, RemotePort); % Connect to the remote TCP server
SerialEthernet('LoadString', 3, Message); % Load a string into slot 3 of 32
sma = NewStateMatrix();
sma = AddState(sma, 'Name', 'State1', ...
'Timer', 1,...
'StateChangeConditions', {'Tup', 'exit'},...
'OutputActions', {'Serial1Code', 3}); % Send string 3 to the remote TCP server