'Operating with Sockets: debt to Rob Faludi
'jleblanc 3.2.06
'Runs on Xport board with the 18F252
DEFINE OSC 20
'Setup ADC pins
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 5
TRISA = %11111111 'set all "A" ports to inputs
adcon1 = %10000010 'right justify results(?)
'declare variables
adcVar VAR WORD 'store adc input value
pin VAR BYTE 'for looping thru pin numbers
numOfPins CON 1 'of max number of pins to scan
'variable for averaging
adcAddVar VAR WORD
adcAvgVar VAR WORD
i VAR BYTE
' We communicate with the XPort using 9600 8N1 serial
true9600 CON 84
timeOut CON 50 ' the serial incoming timeout in milliseconds
' Our serial communication pins
tx VAR PORTC.6
rx VAR PORTC.7
' Set Debug definitions
DEFINE DEBUG_REG PORTB ' the debug port
DEFINE DEBUG_BIT 0 ' the debug pin
DEFINE DEBUG_BAUD 9600 ' the debug baud rate
DEFINE DEBUG_MODE 1 ' Set Debug mode: 0 = true, 1 = inverted
' Our blinking pin for status messages
statusPin VAR PORTC.4
LED VAR BYTE
OUTPUT statusPin
' Used to read response from the XPort
inByte VAR BYTE
' Used to send a byte from the XPort
outByte VAR BYTE
' Track whether or not we are connected to the remote server
connected VAR BIT
connected = 0
' Turn on our LED so we know that the startup sequence is going
HIGH statusPin
' Wait for the XPort to boot up
PAUSE 5000
' Turn off the status LED while operating
LOW statusPin
' Just so we know that we're ready, we'll quickly blink the status LED
counter VAR BYTE
blinkCount VAR BYTE ' number of times to blink
'blinkLight Var Byte ' which portb pin to blink
'LED = "g"
'blinkCount = 4
'gosub blinkTheLight
main:
GOSUB readabyte
GOSUB checkInput
GOSUB sendAnalogValues
IF inByte <> 0 THEN
DEBUG inByte
ENDIF
PAUSE 500
GOTO main
checkInput:
IF inbyte == "1" THEN
'LED = "y"
blinkcount = 1
GOSUB blinkTheLight
ENDIF
' if inbyte == "H" then
' LED = "y"
' blinkcount = 1
' gosub blinkTheLight
' endif
' if inbyte == "M" then
' LED = "r"
' blinkCount = 3
' gosub blinkTheLight
' endif
' if inbyte == "W" then
' LED = "g"
' blinkCount = 8
' gosub blinkTheLight
' endif
' if inbyte == "L" then
' LED = "r"
' blinkCount = 8
' gosub blinkTheLight
' endif
RETURN
readAByte:
SERIN2 rx, true9600, timeOut, noData, [inByte]
RETURN
noData:
inByte = 0
RETURN
sendAnalogValues:
'loop thru pins 0 to numOfPins
'FOR pin = 0 TO numOfPins-1 STEP 1
' adcAddVar = 0
' adcAvgVar = 0
' for i = 0 to 9 step 1
' ADCIN pin, AdcVAr 'set adcVar to pin value
' adcAddVar = adcAddVar + ADCVar
' NEXT
' adcAvgVar = adcAddVar/10
' outByte = adcAvgVar/4'convert to byte
' serout2 tx, true9600, [outByte]
' NEXT
ADCIN 0, AdcVAr
outByte = AdcVAr/4'convert to byte
SEROUT2 tx, true9600, [outByte]
RETURN
blinkTheLight:
counter = 0
WHILE counter < blinkCount
HIGH portc.4
PAUSE 100
LOW portc.4
PAUSE 100
' select case LED
' case "g"
' high portb.1
' pause 100
' low portb.1
' pause 100
' case "y"
' high portb.2
' pause 100
' low portb.2
' pause 100
' case "r"
' high portb.3
' pause 100
' low portb.3
' pause 100
' end select
counter = counter + 1
WEND
RETURN
'THE PROCESSING CODE
'// rob@faludi.com
'// A full example for using Sockets in Processing to communicate with a Lantronix XPort.
'// This program could also be used to communicate primitive variables (bytes) with any other TCP/IP enabled device.
'//
'// We open a Socket, check to see if data is available, get a byte of data
'// from the remote device, and then send a byte of data to the remote device.
'// At the end of the program, we close the streams of data and close the Socket.
'// (Once you are done debugging your code, you should remove all the println statements for better speed.)
'import java.io.*; // this is the input/output library needed for data streams
'import java.net.*; // this is the network library needed for sockets
'String host;
'int port;
'Socket mySocket; // declare Socket
'DataInputStream myInputStream; // declare data input stream. This will run within a socket, bringing data into Java
'DataOutputStream myOutputStream; // declare data output stream. This will run within a socket, sending data out from Java
'byte myDataIn, myDataOut; // declare some variables to store the data we're sending and receiving
' //NOTE that in processing a byte is from 127 to -128 where:
' //for a byte from 0-255 0->0, 127->127, 128->-128, 255->-1
'int DATA;
'void setup()
'{
' size(400,255);
' background(0);
' framerate(30);
' host = "128.122.151.199"; // define a host to communicate with. This can be a name or IP address
' port = 10001; // define a port to contact on that host. Must be a number, typically 10001 for an XPort
'}
'void draw()
'{
' checkConnection(host, port); // subroutine to create a connection, via a socket, to the XPort
' if (dataIsWaiting() == true) // check to see if there's new data waiting to come in
' {
' myDataIn = getSomeData(); // ... and if there's new data, get it
' }
' myDataOut = 1; // create some placeholder data, in this case ASCII letter A
' sendSomeData(myDataOut); // send the data out
' //NOTE that in processing a byte is from 127 to -128 where:
' //for a byte from 0-255 0->0, 127->127, 128->-128, 255->-1
' DATA=(int)myDataIn;
' if(DATA<0)
' { DATA=DATA+256;}
' background(DATA);
'}
'////////CHECK CONNECTION\\\\\\\\
'void checkConnection(String host, int port)
'{
' if(mySocket == null || mySocket.isConnected() == false)
' {
' println("trying to connect to: " + host + " at port: " + port);
' try // make an attempt to run the following code
' {
' mySocket = new Socket(host,port); // initialize socket, connecting it to a host computer's port
' println("connected!");
' }
' catch(Exception e) // if the "try" attempt gave an error, run the following code
' {
' e.printStackTrace(); // print the error to the log
' println("unable to connect to: " + host + " at port: " + port);
' }
' }
'}
'////////CHECK TO SEE IF DATA IS WAITING TO COME IN\\\\\\\\\\\
'boolean dataIsWaiting()
'{
' boolean bytesAvailable = false;
' if ( myInputStream == null) // if there's no active input stream
' {
' try // create an new input stream from a particular socket
' {
' myInputStream = new DataInputStream(mySocket.getInputStream());
' println("opening input stream");
' }
' catch (Exception e)
' {
' e.printStackTrace();
' println("error while opening input stream");
' }
' }
' try
' {
' if (myInputStream.available()>0) // check to see if any bytes are available
' {
' bytesAvailable = true; // ...and if they are set the variable to true
' println(myInputStream.available() + " bytes available...");
' }
' }
' catch(Exception e)
' {
' e.printStackTrace();
' println("error while checking for bytes available");
' }
' return bytesAvailable;
'}
'////////GET SOME DATA \\\\\\\\\\
'byte getSomeData()
'{
' byte inData = 0; // declare and initialize the data variable
' try
' {
' if (myInputStream.available()>0) // only read the byte if there's a byte to read [this is a redundant check]
' {
' inData = myInputStream.readByte(); // read a byte from the input stream
' println("data received: " + inData);
' }
' }
' catch(Exception e)
' {
' e.printStackTrace();
' println("no data");
' }
' return inData;
'}
'////////SEND SOME DATA\\\\\\\\\\
'void sendSomeData(byte outData)
'{
' if (myOutputStream == null) // if there's no active output stream
' {
' try
' {
' myOutputStream = new DataOutputStream(mySocket.getOutputStream()); // create an new output stream from a particular socket
' }
' catch (Exception e)
' {
' e.printStackTrace();
' // println("no output stream");
' }
' }
' try
' {
' myOutputStream.writeByte(outData); // write a byte to the output stream
' //println("data sent: " + outData);
' }
' catch(Exception e)
' {
' e.printStackTrace();
' //println("event send failed");
' }
'}
'public void stop() // when the program quits
'{
' try
' {
' myInputStream.close(); // close the input stream
' myOutputStream.close(); // close the output stream
' mySocket.close(); // close the socket
' }
' catch (Exception e)
' {
' e.printStackTrace();
' println("couldn't close connection");
' }
'}