Skip to content

Commit

Permalink
Merge pull request #1 from Zipcar/sockets
Browse files Browse the repository at this point in the history
Socket implementation
  • Loading branch information
avatarneil authored Jul 4, 2018
2 parents 6c26a10 + 58c6a1a commit fd3ae78
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# MPLabX Simulator UART I/O Plug-in
This is a plug-in for MPLabX v4.20, which support basic UART reading and injection.
The plug-in reads and writes to pre-configured request and response paths that are
external to the simulator, and allow interaction with the developer's platform of choice
The plug-in reads and writes to socket 5556, and allow interaction with the developer's platform of choice
for simulating peripherals (Modem, GPS, etc.) The possibilities are endless!

The majority of the code is under src/Uart.java, but if you wish to implement your own
Expand All @@ -20,6 +19,7 @@ SFRObservers, then you'll want to look under src/UartObserver.java
3. Copy `config.yml` from the `src` directory of `uart_mplabx_plugin` to your MPLab X bin folder.
* MacOS: `/Applications/microchip/mplabx/vX.XX/mplab_platform/bin`
* Windows: `C:\Program Files (x86)\Microchip\MPLABX\vX.XX\mplab_ide\bin`
* Ubuntu 16.04: `/opt/microchip/mplabx/vX.XX/mplab_platform/bin`
4. Open Netbeans
5. `File -> Open Project` Open the `uart_mplabx_plugin` project folder
6. Open `uart_mplabx_plugin -> Source Packages -> zipcar.emulator.uart -> Uart.java` from the projects tab
Expand Down
36 changes: 26 additions & 10 deletions src/zipcar/emulator/uart/Uart.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
import com.microchip.mplab.mdbcore.simulator.MessageHandler;
import com.microchip.mplab.mdbcore.simulator.SimulatorDataStore.SimulatorDataStore;
import com.microchip.mplab.mdbcore.simulator.PeripheralSet;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.util.LinkedList;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import org.openide.util.lookup.ServiceProvider;
import java.util.Map;
import org.yaml.snakeyaml.Yaml;
Expand All @@ -39,9 +43,9 @@ public class Uart implements Peripheral {
int cycleCount = 0;
SCL scl;
boolean notInitialized = true;
LinkedList<Character> chars = new LinkedList<Character>();
FileOutputStream request;
FileInputStream response;
LinkedList<Byte> chars = new LinkedList<Byte>();
BufferedWriter request;
BufferedInputStream response;
Yaml yaml = new Yaml();

@Override
Expand Down Expand Up @@ -80,14 +84,23 @@ public boolean init(SimulatorDataStore DS) {
}

// Setup pipes
try {
/* try {
request = new FileOutputStream(REQUEST_FILE);
response = new FileInputStream(RESPONSE_FILE);
} catch (FileNotFoundException e) {
messageHandler.outputMessage("Exception in init: " + e);
return false;
}
} */


try {
Socket reqSocket = new Socket("localhost", 5556);
request = new BufferedWriter(new OutputStreamWriter(reqSocket.getOutputStream()));
response = new BufferedInputStream(reqSocket.getInputStream());
} catch (Exception e) {
messageHandler.outputError(e);
}

// Add observers
UartObserver obs = new UartObserver();
sfrTX.addObserver(obs);
Expand Down Expand Up @@ -132,18 +145,19 @@ public void reset() {

@Override
public void update() {
if (cycleCount % (4000) == 0) {
if (cycleCount % (267) == 0) {
try {
if (response.available() != 0) { // If there are unread bytes, read them and add the chars
messageHandler.outputMessage("Available bytes: " + response.available());
chars.add((char) response.read());
if (response.available() > 0) {
messageHandler.outputMessage(response.available() + "");
chars.add((byte) response.read());
}
} catch (IOException e) {
messageHandler.outputMessage("Exception reading character from res " + e);
return;
}
if (!chars.isEmpty()) { // Inject anything in chars
if (sfrSTA.getFieldValue("UTXEN") == 1) { // If STA is ready to receive !! IMPORTANT
messageHandler.outputMessage("Injecting: " + chars.peek());
// messageHandler.outputMessage(chars.peek() + ""); // Returns the next char which will be injected
sfrRX.privilegedWrite(chars.pop()); // Inject the next char
sfrInterrupt.privilegedSetFieldValue("U2RXIF", 1); // Trigger the interrupt
Expand All @@ -156,14 +170,16 @@ public void update() {
// Debugging function for manually adding a string to chars
public void setString(String str) {
for (int i = 0; i < str.length(); i++) {
chars.add(str.charAt(i));
chars.add((byte) str.charAt(i));
}
}

// Try to write bytes to the request file
public void output() {
try {
messageHandler.outputMessage("writing to lmpp");
request.write((byte) sfrTX.read());
request.flush();
} catch (Exception e) {
messageHandler.outputMessage("failed to write request byte " + e);
}
Expand Down

0 comments on commit fd3ae78

Please sign in to comment.