AI2 Component  (Version nb184)
Serial.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2016-2019 MIT, All rights reserved
3 // Copyright 2017-2019 Kodular, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
9 import android.content.Context;
10 import android.util.Log;
11 
12 import com.physicaloid.lib.Physicaloid;
13 
25 
26 import java.io.UnsupportedEncodingException;
27 
28 @DesignerComponent(version = YaVersion.SERIAL_COMPONENT_VERSION,
29  description = "Serial component which can be used to connect to devices like Arduino",
30  category = ComponentCategory.CONNECTIVITY,
31  nonVisible = true,
32  iconName = "images/arduino.png",
33  androidMinSdk = 12)
34 
35 @SimpleObject
36 @UsesLibraries(libraries = "physicaloid.jar")
37 public class Serial extends AndroidNonvisibleComponent implements Component {
38  private static final String LOG_TAG = "Serial Component";
39 
40  private Context context;
41 
42  private Physicaloid mPhysicaloid;
43 
44  private int baudRate = 9600;
45  private int bytes = 256;
46 
47  public Serial(ComponentContainer container) {
48  super(container.$form());
49  context = container.$context();
50  Log.d(LOG_TAG, "Created");
51  }
52 
53  @SimpleFunction(description = "Initializes serial connection.")
54  public void InitializeSerial() {
55  mPhysicaloid = new Physicaloid(context);
56  BaudRate(this.baudRate);
57  Log.d(LOG_TAG, "Initialized");
58  }
59 
60  @SimpleFunction(description = "Opens serial connection. Returns true when opened.")
61  public boolean OpenSerial() {
62  Log.d(LOG_TAG, "Opening connection");
63  if (mPhysicaloid == null) {
64  form.dispatchErrorOccurredEvent(Serial.this, "OpenSerial", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
65  return false;
66  }
67  return mPhysicaloid.open();
68  }
69 
70  @SimpleFunction(description = "Closes serial connection. Returns true when closed.")
71  public boolean CloseSerial() {
72  Log.d(LOG_TAG, "Closing connection");
73  if (mPhysicaloid == null) {
74  form.dispatchErrorOccurredEvent(Serial.this, "CloseSerial", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
75  return false;
76  }
77  return mPhysicaloid.close();
78  }
79 
80  @SimpleFunction(description = "Reads data from serial.")
81  public String ReadSerial() {
82  String data = "";
83  if (mPhysicaloid == null) {
84  form.dispatchErrorOccurredEvent(Serial.this, "ReadSerial", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
85  } else {
86  byte[] buf = new byte[this.bytes];
87  if (mPhysicaloid.read(buf) > 0) {
88  try {
89  data = new String(buf, "UTF-8");
90  } catch (UnsupportedEncodingException mEr) {
91  Log.e(LOG_TAG, mEr.getMessage());
92  }
93  }
94  }
95  return data;
96  }
97 
98  @SimpleFunction(description = "Writes given data to serial.")
99  public void WriteSerial(String data) {
100  if (!data.isEmpty() && mPhysicaloid != null) {
101  byte[] buf = data.getBytes();
102  int result = mPhysicaloid.write(buf);
103  if (result == -1)
104  form.dispatchErrorOccurredEvent(Serial.this, "WriteSerial", ErrorMessages.ERROR_SERIAL_WRITING);
105  } else if (mPhysicaloid == null) {
106  form.dispatchErrorOccurredEvent(Serial.this, "WriteSerial", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
107  }
108  }
109 
110  @SimpleFunction(description = "Writes given data to serial, and appends a new line at the end.")
111  public void PrintSerial(String data) {
112  if (!data.isEmpty())
113  WriteSerial(data + "\n");
114  }
115 
116  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns true when the Serial connection is open.")
117  public boolean IsOpen() {
118  if (mPhysicaloid == null) {
119  form.dispatchErrorOccurredEvent(Serial.this, "IsOpen", ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED);
120  return false;
121  }
122  return mPhysicaloid.isOpened();
123  }
124 
125  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns true when the Serial has been initialized.")
126  public boolean IsInitialized() {
127  return mPhysicaloid != null;
128  }
129 
130  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns the current baud rate")
131  public int BaudRate() {
132  return this.baudRate;
133  }
134 
135  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_INTEGER, defaultValue = "9600")
137  public void BaudRate(int baudRate) {
138  this.baudRate = baudRate;
139  Log.d(LOG_TAG, "Baud Rate: " + baudRate);
140  if (mPhysicaloid != null)
141  mPhysicaloid.setBaudrate(baudRate);
142  else
143  Log.w(LOG_TAG, "Could not set Serial Baud Rate to " + baudRate + ". Just saved, not applied to serial! Maybe you forgot to initialize it?");
144  }
145 
146  @SimpleProperty(category = PropertyCategory.BEHAVIOR, description = "Returns the buffer size in bytes")
147  public int BufferSize() {
148  return this.bytes;
149  }
150 
151  @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_INTEGER, defaultValue = "256")
153  public void BufferSize(int bytes) {
154  this.bytes = bytes;
155  Log.d(LOG_TAG, "Buffer Size: " + bytes);
156  }
157 }
com.google.appinventor.components.runtime.Serial.Serial
Serial(ComponentContainer container)
Definition: Serial.java:47
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.annotations.UsesLibraries
Definition: UsesLibraries.java:21
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.annotations.DesignerProperty
Definition: DesignerProperty.java:25
com.google.appinventor.components
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.annotations.PropertyCategory.BEHAVIOR
BEHAVIOR
Definition: PropertyCategory.java:15
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_SERIAL_WRITING
static final int ERROR_SERIAL_WRITING
Definition: ErrorMessages.java:274
com.google.appinventor.components.common.PropertyTypeConstants.PROPERTY_TYPE_INTEGER
static final String PROPERTY_TYPE_INTEGER
Definition: PropertyTypeConstants.java:88
com.google.appinventor.components.runtime.Serial
Definition: Serial.java:37
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.annotations.SimpleProperty
Definition: SimpleProperty.java:23
com.google.appinventor.components.annotations.PropertyCategory
Definition: PropertyCategory.java:13
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.runtime.Component
Definition: Component.java:17
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google.appinventor.components.runtime.Serial.BufferSize
void BufferSize(int bytes)
Definition: Serial.java:153
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.runtime.ComponentContainer.$context
Activity $context()
com.google.appinventor.components.runtime.Serial.BaudRate
void BaudRate(int baudRate)
Definition: Serial.java:137
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_SERIAL_NOT_INITIALIZED
static final int ERROR_SERIAL_NOT_INITIALIZED
Definition: ErrorMessages.java:273
com.google.appinventor.components.common.PropertyTypeConstants
Definition: PropertyTypeConstants.java:14
com.google.appinventor.components.annotations
com.google.appinventor