Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: eri0o on Wed 09/12/2020 03:43:06

Title: ENGINE PLUGIN: agsserialport 0.1.0
Post by: eri0o on Wed 09/12/2020 03:43:06
agsserialport  version 0.1.0

(https://dev.azure.com/ericoporto/agsserialport/_apis/build/status/ericoporto.agsserialport?branchName=main)

Get Latest Release agsserialport.dll (https://github.com/ericoporto/agsserialport/releases/download/0.1.0/agsserialport.dll) | libagsserialport.so (https://github.com/ericoporto/agsserialport/releases/download/0.1.0/libagsserialport.so) | libagsserialport.dylib (https://github.com/ericoporto/agsserialport/releases/download/0.1.0/libagsserialport.dylib) | GitHub Repo (https://github.com/ericoporto/agsserialport) | Demo Windows (https://github.com/ericoporto/agsserialport/releases/download/0.1.0/agsserialport_demo_windows.zip) | Demo Linux (https://github.com/ericoporto/agsserialport/releases/download/0.1.0/agsserialport_demo_linux.tar.gz)

(https://user-images.githubusercontent.com/2244442/101576025-e9d85780-39b7-11eb-84d0-fbc14596778e.png)

A very untested Serial Port plug-in for Adventure Game Studio. I made this for myself to play with Arduino at home, and I will eventually document this better and fix it's bugs.

For now, here is an example that continuously reads incoming data on the serial port. Reading and Writing operations are non-blocking.

Code (ags) Select
// room script file
SP_Port* _port;

function room_RepExec()
{
  if(_port == null) return;
  String read = "";

 
  for(int byte_count = _port.WaitingBytesRead;byte_count>0;byte_count = _port.WaitingBytesRead)
  {
    if(byte_count > 0)
    {
      String piece_read = _port.Read(0);
      if(piece_read!=null) read = read.Append(piece_read);
    }
  }
 
  if(!String.IsNullOrEmpty(read)) Display(read);
}

function room_AfterFadeIn()
{
  // Port HAS to be open before configuring
  if(_port.Open(eAGSP_Mode_Read) == eAGSP_OK)
  {
    if(_port.SetBaudrate(9600) != eAGSP_OK) Display("Error setting baudrate");
    if(_port.SetBits(8) != eAGSP_OK) Display("Error setting bits");
    if(_port.SetParity(eAGSP_Parity_None) != eAGSP_OK) Display("Error setting parity");
    if(_port.SetStopBits(1) != eAGSP_OK) Display("Error setting stop bits");
    if(_port.SetFlowControl(eAGSP_FlowControl_None) != eAGSP_OK) Display("Error setting flow control");
  }
  else
  {
    _port = null;
  }
}

function room_Load()
{
  String portname;
  for(int i=0; i<AGSP.PortNamesCount; i++)
  {
    portname = AGSP.PortNames[i];
  }

  if(portname == null || portname.Length < 1)
  {
    return;
  }
   
  _port = SP_Port.Create(portname);
 
}

function hGlowingOrb_AnyClick()
{
  if(_port == null) return;
 
  Display(String.Format("Serial Port Name: %s\nDescription: %s\n", _port.Name, _port.Description));
}


The library has three main structs:

A SP_Port has no default configuration, you always have to set a configuration after opening a port before reading or writing to it. In any operation that returns a value, a negative value is an error.

Here is a very simple Arduino example that can be used to verify you are reading correctly.

Code (c) Select
void setup()                    // run once, when the sketch starts
{
  Serial.begin(9600);           // set up Serial library at 9600 bps, Arduino defaults to 8 bits, 1 Stop Bit, No Parity and No Flow Control
}

void loop()                       // run over and over again
{
  Serial.println("Hello serial world!");  // prints hello with ending line break
  delay(5000);
}


This should be mostly useless but may be fun to play with hardware at home. This is a port of libserialport for AGS.