Howto:Port I/O from Nasal: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
Line 103: Line 103:
_setlistener("/sim/cockpit/eufd-input", processMessage);
_setlistener("/sim/cockpit/eufd-input", processMessage);
</syntaxhighlight>
</syntaxhighlight>
Obviously, you could now add some header to each message to identify the message type. For example, by checking a substring of the message:
<syntaxhighlight lang="php">
var processMessage = func() {
  var msg = getprop('/sim/cockpit/eufd-input');
  var msg_type = substr(msg,0,1);
  print("Info: Message received! TYPE:", msg_type);
}
_setlistener("/sim/cockpit/eufd-input", processMessage);
</syntaxhighlight>
Next, you could come up with a list of supported message types:
<syntaxhighlight lang="php">
var processMessage = func() {
  var msg_types = {'A':'SET FONT','B':'CLEAR SCREEN','C':'RESET','D':'SHUTDOWN',E':'NOP'};
  var msg = getprop('/sim/cockpit/eufd-input');
  var msg_type = substr(msg,0,1);
  print("Info: Message received! TYPE:", msg_type);
  if ( contains(msg_types,msg_type) ) print(msg_types[msg_type]);
}
_setlistener("/sim/cockpit/eufd-input", processMessage);
</syntaxhighlight>




Line 110: Line 138:
We suggest you just try it, create a simple protocol with a couple properties, explore ways to send and receive those values, and so on. If your serial device is a microcontroller that your programming then you may be able to get away with sending/receiving the values directly to/from flightgear without need of any secondary 'middleware' programs at all. If not you may need a small program to handle marshalling the data between flightgear and your hardware.
We suggest you just try it, create a simple protocol with a couple properties, explore ways to send and receive those values, and so on. If your serial device is a microcontroller that your programming then you may be able to get away with sending/receiving the values directly to/from flightgear without need of any secondary 'middleware' programs at all. If not you may need a small program to handle marshalling the data between flightgear and your hardware.


On the pc side of things I would strongly suggest forgetting about C/C++ unless you really, really prefer it and/or have no choice. It is going to be much more complex doing serial and/or network programming in straight C/C++.  
On the PC side of things I would strongly suggest forgetting about C/C++ unless you really, really prefer it and/or have no choice. It is going to be much more complex doing serial and/or network programming in straight C/C++.  


Using a serial device properly requires more than just 'fopen'ing it, except in the most simple of cases, and can be quite tedious and complex using straight C system calls...networking as well. Personally I recommend using a higher level language (I use Python) that will make your life so much easier and allow you to get things done quickly and easily without hassles of low level details.
Using a serial device properly requires more than just 'fopen'ing it, except in the most simple of cases, and can be quite tedious and complex using straight C system calls...networking as well. Personally I recommend using a higher level language (I use Python) that will make your life so much easier and allow you to get things done quickly and easily without hassles of low level details.

Navigation menu