studio 6 incomplete

mail@pastecode.io avatar
unknown
java
a year ago
1.8 kB
3
Indexable
Never
package communication;

import java.util.Scanner;

import jssc.*;

public class SerialComm {

	SerialPort port;

	private boolean debug;  // Indicator of "debugging mode"

	// This function can be called to enable or disable "debugging mode"
	void setDebug(boolean mode) {
		debug = mode;
	}	


	// Constructor for the SerialComm class
	public SerialComm(String name) throws SerialPortException {
		port = new SerialPort(name);		
		port.openPort();
		port.setParams(SerialPort.BAUDRATE_9600,
				SerialPort.DATABITS_8,
				SerialPort.STOPBITS_1,
				SerialPort.PARITY_NONE);

		debug = false; // Default is to NOT be in debug mode
	}

	void writeByte(byte value) {
		try {
			port.writeByte(value);
		} catch (SerialPortException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if (this.debug == true) {	
			System.out.print("<0x"+String.format("%02x", value)+">");
		}

	}

	byte readByte() throws SerialPortException {
		byte value = port.readBytes(1)[0];
		if (this.debug == true) {	
			System.out.print("[0x"+String.format("%02x", value)+"]");
		}
		return value;
	}

	boolean available() throws SerialPortException {
		if (port.getInputBufferBytesCount() > 0) {
			return true;
		}
		return false;
	}
	// TODO: Add a main() method
	public static void main(String[] args) throws SerialPortException {
		SerialComm port = new SerialComm("/dev/cu.usbserial-210");
		port.setDebug(false);
		// TODO: Complete section 6 of the Studio (gather sanitized user input
		//       and send it to the Arduino))

		while (true) {
			if (port.available()) {
				// read each character to the Arduino via the serial port.
				byte firstByte = port.readByte();
				byte secondByte = port.readByte();
				int j = (firstByte << 8) & 0xff00;
				j += (secondByte & 0xff);
				System.out.println(j);
			
			}
		}

	}
}