PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SLIPSerialToUDP.pde 8.9KB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. import processing.serial.*;
  2. //download at http://ubaa.net/shared/processing/udp/
  3. import hypermedia.net.*;
  4. //download at www.sojamo.de/libraries/controlp5
  5. import controlP5.*;
  6. /************************************************************************************
  7. GUI
  8. ************************************************************************************/
  9. ControlP5 cp5;
  10. DropdownList serialddl;
  11. DropdownList baudddl;
  12. Textlabel arduinoLabel;
  13. Textlabel UDPLabel;
  14. Textlabel incomingPacket;
  15. Button startButton;
  16. Button stopButton;
  17. Textfield ipAddressField;
  18. Textfield incomingPortField;
  19. Textfield outgoingPortField;
  20. void setupGUI() {
  21. //the ControlP5 object
  22. cp5 = new ControlP5(this);
  23. //start button
  24. startButton = cp5.addButton("START")
  25. .setPosition(200, 200)
  26. .setSize(200, 19)
  27. ;
  28. //stop button
  29. stopButton = cp5.addButton("STOP")
  30. .setPosition(200, 200)
  31. .setSize(200, 19)
  32. ;
  33. stopButton.hide();
  34. //Serial Port selector
  35. serialddl = cp5.addDropdownList("SerialPort")
  36. .setPosition(50, 100)
  37. .setSize(200, 200)
  38. ;
  39. serialddl.setItemHeight(20);
  40. serialddl.setBarHeight(15);
  41. serialddl.captionLabel().set("SELECT ARDUINO SERIAL PORT");
  42. serialddl.captionLabel().style().marginTop = 3;
  43. serialddl.captionLabel().style().marginLeft = 3;
  44. serialddl.valueLabel().style().marginTop = 3;
  45. //set the serial options
  46. String SerialList[] = Serial.list();
  47. for (int i=0;i<SerialList.length;i++) {
  48. String portName = SerialList[i];
  49. serialddl.addItem(portName, i);
  50. }
  51. serialddl.setIndex(0);
  52. //setup the baud list
  53. baudddl = cp5.addDropdownList("BaudRate")
  54. .setPosition(50, 50)
  55. .setSize(200, 200)
  56. ;
  57. baudddl.setItemHeight(20);
  58. baudddl.setBarHeight(15);
  59. baudddl.captionLabel().set("SELECT THE BAUD RATE");
  60. baudddl.captionLabel().style().marginTop = 3;
  61. baudddl.captionLabel().style().marginLeft = 3;
  62. baudddl.valueLabel().style().marginTop = 3;
  63. //the baud options
  64. for (int i=0;i<serialRateStrings.length;i++) {
  65. String baudString = serialRateStrings[i];
  66. baudddl.addItem(baudString, i);
  67. }
  68. baudddl.setIndex(4);
  69. //udp IP/port
  70. ipAddressField = cp5.addTextfield("IP address")
  71. .setPosition(300, 30)
  72. .setAutoClear(false)
  73. .setText(ipAddress)
  74. ;
  75. incomingPortField = cp5.addTextfield("Incoming Port Number")
  76. .setPosition(300, 80)
  77. .setAutoClear(false)
  78. .setText(str(inPort))
  79. ;
  80. outgoingPortField = cp5.addTextfield("Outgoing Port Number")
  81. .setPosition(300, 130)
  82. .setAutoClear(false)
  83. .setText(str(outPort))
  84. ;
  85. //text labels
  86. arduinoLabel = cp5.addTextlabel("arduinoLabel")
  87. .setText("Serial")
  88. .setPosition(50, 10)
  89. .setColorValue(0xffffff00)
  90. .setFont(createFont("SansSerif", 11))
  91. ;
  92. UDPLabel = cp5.addTextlabel("UDPLabel")
  93. .setText("UDP")
  94. .setPosition(300, 10)
  95. .setColorValue(0xffffff00)
  96. .setFont(createFont("SansSerif", 11))
  97. ;
  98. incomingPacket = cp5.addTextlabel("incomingPacketLabel")
  99. .setText("Incoming Packet")
  100. .setPosition(210, 100)
  101. .setColorValue(0xffffff00)
  102. .setFont(createFont("SansSerif", 10))
  103. ;
  104. incomingPacket.hide();
  105. }
  106. void controlEvent(ControlEvent theEvent) {
  107. String eventName = theEvent.getName();
  108. if (theEvent.isGroup()) {
  109. if (eventName == "SerialPort") {
  110. //set the serial port
  111. serialListNumber = int(theEvent.getValue());
  112. }
  113. else if (eventName == "BaudRate") {
  114. int index = int(theEvent.getValue());
  115. baud = Integer.parseInt(serialRateStrings[index]);
  116. }
  117. else {
  118. }
  119. }
  120. else if (theEvent.isAssignableFrom(Textfield.class)) {
  121. if (eventName == "IP address") {
  122. ipAddressField.setFocus(false);
  123. ipAddress = theEvent.getStringValue();
  124. }
  125. else if (eventName == "Incoming Port Number") {
  126. incomingPortField.setFocus(false);
  127. inPort = Integer.parseInt(theEvent.getStringValue());
  128. }
  129. else if (eventName == "Outgoing Port Number") {
  130. outgoingPortField.setFocus(false);
  131. outPort = Integer.parseInt(theEvent.getStringValue());
  132. }
  133. }
  134. }
  135. boolean applicationRunning = false;
  136. //start everything
  137. public void START(int theValue) {
  138. setupUDP();
  139. setupSerial();
  140. hideControls();
  141. applicationRunning = true;
  142. }
  143. //hide all the controls and show the stop button
  144. void hideControls() {
  145. serialddl.hide();
  146. baudddl.hide();
  147. startButton.hide();
  148. outgoingPortField.hide();
  149. incomingPortField.hide();
  150. ipAddressField.hide();
  151. incomingPacket.show();
  152. //show the stop button
  153. stopButton.show();
  154. }
  155. void showControls() {
  156. serialddl.show();
  157. baudddl.show();
  158. startButton.show();
  159. outgoingPortField.show();
  160. incomingPortField.show();
  161. ipAddressField.show();
  162. incomingPacket.hide();
  163. //hide the stop button
  164. stopButton.hide();
  165. }
  166. public void STOP() {
  167. stopSerial();
  168. stopUDP();
  169. showControls();
  170. applicationRunning = false;
  171. }
  172. /************************************************************************************
  173. SERIAL
  174. ************************************************************************************/
  175. //the Serial communcation to the Arduino
  176. Serial serial;
  177. String[] serialRateStrings = {
  178. "300", "1200", "2400", "4800", "9600", "14400",
  179. "19200", "28800", "38400", "57600", "115200"
  180. };
  181. int baud = 9600;
  182. int serialListNumber = 0;
  183. ArrayList<Byte> serialBuffer = new ArrayList<Byte>();
  184. void setupSerial() {
  185. serial = new Serial(this, Serial.list()[serialListNumber], baud);
  186. }
  187. void stopSerial() {
  188. serial.stop();
  189. }
  190. void serialEvent(Serial serial) {
  191. //decode the message
  192. while (serial.available () > 0) {
  193. slipDecode(byte(serial.read()));
  194. }
  195. }
  196. void SerialSendToUDP() {
  197. byte [] buffer = new byte[serialBuffer.size()];
  198. //copy the buffer over
  199. for (int i = 0; i < serialBuffer.size(); i++) {
  200. buffer[i] = serialBuffer.get(i);
  201. }
  202. //send it off
  203. UDPSendBuffer(buffer);
  204. //clear the buffer
  205. serialBuffer.clear();
  206. //light up the indicator
  207. drawIncomingSerial();
  208. }
  209. void serialSend(byte[] data) {
  210. //encode the message and send it
  211. for (int i = 0; i < data.length; i++){
  212. slipEncode(data[i]);
  213. }
  214. //write the eot
  215. serial.write(eot);
  216. }
  217. /************************************************************************************
  218. SLIP ENCODING
  219. ************************************************************************************/
  220. byte eot = byte(192);
  221. byte slipesc = byte(219);
  222. byte slipescend = byte(220);
  223. byte slipescesc = byte(221);
  224. byte previousByte;
  225. void slipDecode(byte incoming) {
  226. byte previous = previousByte;
  227. previousByte = incoming;
  228. //if the previous was the escape char
  229. if (previous == slipesc) {
  230. //if this one is the esc eot
  231. if (incoming==slipescend) {
  232. serialBuffer.add(eot);
  233. }
  234. else if (incoming==slipescesc) {
  235. serialBuffer.add(slipesc);
  236. }
  237. }
  238. else if (incoming==eot) {
  239. //if it's the eot
  240. //send off the packet
  241. SerialSendToUDP();
  242. }
  243. else if (incoming != slipesc) {
  244. serialBuffer.add(incoming);
  245. }
  246. }
  247. void slipEncode(byte incoming) {
  248. if(incoming == eot){
  249. serial.write(slipesc);
  250. serial.write(slipescend);
  251. } else if(incoming==slipesc) {
  252. serial.write(slipesc);
  253. serial.write(slipescesc);
  254. } else {
  255. serial.write(incoming);
  256. }
  257. }
  258. /************************************************************************************
  259. UDP
  260. ************************************************************************************/
  261. //UDP communication
  262. UDP udp;
  263. int inPort = 9000;
  264. int outPort = 9001;
  265. String ipAddress = "127.0.0.1";
  266. void setupUDP() {
  267. udp = new UDP( this, inPort );
  268. //udp.log( true ); // <-- printout the connection activity
  269. udp.listen( true );
  270. }
  271. void stopUDP() {
  272. udp.close();
  273. }
  274. void UDPSendBuffer(byte[] data) {
  275. udp.send( data, ipAddress, outPort );
  276. }
  277. //called when UDP recieves some data
  278. void receive( byte[] data) {
  279. drawIncomingUDP();
  280. //send it over to serial
  281. serialSend(data);
  282. }
  283. /************************************************************************************
  284. SETUP/DRAW
  285. ************************************************************************************/
  286. void setup() {
  287. // configure the screen size and frame rate
  288. size(550, 250, P3D);
  289. frameRate(30);
  290. setupGUI();
  291. }
  292. void draw() {
  293. background(128);
  294. if (applicationRunning) {
  295. drawIncomingPackets();
  296. }
  297. }
  298. /************************************************************************************
  299. VISUALIZING INCOMING PACKETS
  300. ************************************************************************************/
  301. int lastSerialPacket = 0;
  302. int lastUDPPacket = 0;
  303. void drawIncomingPackets() {
  304. //the serial packet
  305. fill(0);
  306. rect(75, 50, 100, 100);
  307. //the udp packet
  308. rect(325, 50, 100, 100);
  309. int now = millis();
  310. int lightDuration = 75;
  311. if (now - lastSerialPacket < lightDuration) {
  312. fill(255);
  313. rect(85, 60, 80, 80);
  314. }
  315. if (now - lastUDPPacket < lightDuration) {
  316. fill(255);
  317. rect(335, 60, 80, 80);
  318. }
  319. }
  320. void drawIncomingSerial() {
  321. lastSerialPacket = millis();
  322. }
  323. void drawIncomingUDP() {
  324. lastUDPPacket = millis();
  325. }