Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added PythonDriver/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions PythonDriver/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.autopep8"
},
"python.formatting.provider": "none"
}
8 changes: 8 additions & 0 deletions PythonDriver/TestBufferAscii.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import sys
import glob

from module.ChannelBuffersSerial import *
if __name__ == '__main__':


pass
6 changes: 6 additions & 0 deletions PythonDriver/TestBufferAsciiTime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import glob

if __name__ == '__main__':

pass
6 changes: 6 additions & 0 deletions PythonDriver/TestBufferBinary16bits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import glob

if __name__ == '__main__':

pass
6 changes: 6 additions & 0 deletions PythonDriver/TestBufferBinary16bitsTime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import glob

if __name__ == '__main__':

pass
6 changes: 6 additions & 0 deletions PythonDriver/TestBufferBinary8bits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import glob

if __name__ == '__main__':

pass
6 changes: 6 additions & 0 deletions PythonDriver/TestBufferBinary8bitsTime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import glob

if __name__ == '__main__':

pass
Binary file added PythonDriver/module/.DS_Store
Binary file not shown.
157 changes: 157 additions & 0 deletions PythonDriver/module/ChannelBuffersSerial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@

import sys
import glob
import serial
import serial.tools.list_ports
from multipledispatch import dispatch


class SerialPort(serial.Serial):

global ser # global variable to store the serial object

def __init__(self):
super(SerialPort, self).__init__()

def listSerialPorts(self):
"""
Lists the serial port names available on the system
"""
# Checking the computer operational system:
if sys.platform.startswith('win'):
ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux'):
# this excludes your current terminal "/dev/tty"
ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('cygwin'): # for windows/cygwin
ports = glob.glob('/dev/tty.*')
elif sys.platform.startswith('darwin'):
ports = glob.glob('/dev/cu.*')
else:
raise EnvironmentError('Unsupported platform')

# Definning variables:
portsFound = []
for port in ports:
try:
s = serial.Serial(port)
s.close()
portsFound.append(port)
except (OSError, serial.SerialException):
pass

return portsFound

def findDevices(self, device_serial_name="Arduino"):
"""
Look through all the available serial connections, the serial objects with "device_serial_name"
inside
"""
comPorts = []

portsFound = self.listSerialPorts()
connectionAvailable = len(portsFound)

if (device_serial_name != "All"):
for curr_device in range(connectionAvailable):
devicePort = str(portsFound[curr_device])

if device_serial_name in devicePort:
comPorts.append((devicePort.split(' '))[0])
else:
for curr_device in range(connectionAvailable):
devicePort = str(portsFound[curr_device])

comPorts.append((devicePort.split(' '))[0])

return comPorts

def connect(self, serialPort, baudrate=9600, timeout=10, xonxoff=False):
if serialPort is not None:
try:
self.ser = serial.Serial(
port=serialPort, baudrate=baudrate, timeout=timeout, xonxoff=xonxoff)
print("Connected to :")
print(serialPort)
except serial.SerialException as var:
print("Connection issued!\n Exception detail:"+var)

def disconnect(self):
self.ser.close()

def isComStablished(self, command, readStr="ACK", encoding='ascii', errors='strict'):
command += str('\n')
serialString = val = SerialPort.ser.write(
command.encode(encoding, errors))

# inCommingData=self.ser.read_until(b'}')
inCommingData = SerialPort.ser.readline()

if (inCommingData == readStr):
return True

else:
return False


class ReadChannel(SerialPort):

def __init__(self, channelNumber):
super(SerialPort, self).__init__()
self.channelNo = channelNumber

# Declaring and implementing all the available functions for the channelBuffer

def Ascii(self, sepCar='\t', decode='ascii'):
readData = self.ser.readline().decode(decode).split(sepCar)
readData.remove("\r\n")

return readData

def AsciiTimeStamp(self, sepCar='\t'):

readData = self.Ascii(sepCar)

return readData

def RawData16(self):

pass

def RawData8(self):

pass

def RawDataTimeStamp16(self):

pass

def RawDataTimeStamp8(self):

pass


if __name__ == '__main__':

# uart = SerialPort()
uart = ReadChannel(4)
ports = uart.listSerialPorts()

devices = uart.findDevices("usbmodem")
# devices = uart.findDevices("Arduino")
# print("Devices available : ", devices)
# print("Comports available : ", ports)
dat = devices[0]
print("dat : "+str(type(dat)))

uart.connect(dat)

uart.connect(devices[0])

for i in range(10):
# dataRead = uart.Ascii()
dataRead = uart.AsciiTimeStamp()

print(dataRead)

uart.disconnect()
29 changes: 29 additions & 0 deletions StreamBufferedChannel.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <StreamChannel.hpp>

#define SIZE 3
uint16_t DATA[SIZE]={200, 128,150};


StreamBuffer Channel;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Channel.setMaxChannelNumber((uint8_t) SIZE);
}

void loop() {
// put your main code here, to run repeatedly:
Channel.appendChannelArray(DATA,SIZE);



Channel.transmitBufferAscii(Serial, "\t");
//Channel.transmitBufferBinaryRaw8(Serial);
//Channel.transmitBufferBinaryRaw16(Serial);
//Channel.sendBufferAsciiTimeStamp(Serial, 12,"\t");
//Channel.sendBufferBinaryTimeStamp(Serial, 12);

delay(100);

}
Binary file added StreamChannel/.DS_Store
Binary file not shown.
144 changes: 144 additions & 0 deletions StreamChannel/StreamChannel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
#include "StreamChannel.hpp"



bool StreamBuffer::isFull(){


return ((_currentIndex==_definedSize)? true:false);

}


StreamBuffer::StreamBuffer(){
_definedSize=0;
_currentIndex=0;
}


void StreamBuffer::setMaxChannelNumber(uint8_t channelNo){
_definedSize=channelNo;

_buffer=new uint16_t[_definedSize];
_lsbBuffer=new uint8_t[_definedSize];
_msbBuffer=new uint8_t[_definedSize];

for (uint8_t i=0; i<_definedSize; i++){
_buffer[i]=0;
_lsbBuffer[i]=0;
_msbBuffer[i]=0;

}


}


void StreamBuffer::appendChannelData(uint16_t data){
_currentIndex=_currentIndex%_definedSize; //set the _currentIndex to 0 when the buffer if full and this function is called to append a new data

*(_buffer+_currentIndex)=data;
_currentIndex++;


}

void StreamBuffer::Flush(){
_currentIndex=0;
for (uint8_t i=0; i<_definedSize; i++) _buffer[i]=0;

}


void StreamBuffer::appendChannelArray(uint16_t *dataSet, uint8_t arraySize){
//the buffer is reset
Flush();

if((arraySize<=_definedSize)&&(arraySize>0)){
//fill the buffer with this datSet
for (uint8_t i=0; i<arraySize;i++){
this->appendChannelData(dataSet[i]);
}

}else if (arraySize>_definedSize){
//drop other packets
for (uint8_t i=0; i<_definedSize;i++){
this->appendChannelData(dataSet[i]);
}


}else{
//dont do anything if the array size if less thant 0

}


}


void StreamBuffer::transmitBufferAscii(Stream &stream, String sepCar){

for (uint8_t i=0; i<_currentIndex; i++){

stream.print(_buffer[i]);
stream.print(sepCar);
}
stream.println();


}


void StreamBuffer::transmitBufferBinaryRaw8(Stream &stream){
for(uint8_t i=0; i<_currentIndex; i++){
_lsbBuffer[i]=(_buffer[i] & 0xFF);
_msbBuffer[i]=(_buffer[i] >> 8) & 0xFF;
}

String header="#"+ String(String(_currentIndex).length())+String(_currentIndex*sizeof(uint8_t));
//Send MSB AS FIRST PACKET
stream.print(header);
stream.write((uint8_t *) _msbBuffer,_currentIndex*sizeof(uint8_t)); // Send the upper byte first
stream.println();

//Send LSB AS SECOND PACKET
stream.print(header);
stream.write((uint8_t *) _lsbBuffer ,_currentIndex*sizeof(uint8_t)); // Send the lower byte
stream.println();

}


void StreamBuffer::transmitBufferBinaryRaw16(Stream &stream){

String header="#"+ String(String(_currentIndex).length())+String(_currentIndex*sizeof(uint8_t));
stream.print(header);
stream.write((char *) _buffer, (_currentIndex)*sizeof(uint16_t));


}


void StreamBuffer::sendBufferAsciiTimeStamp(Stream &stream, unsigned long timeStamp, String sepCar){
stream.print(timeStamp);
stream.print(sepCar);
this->transmitBufferAscii(stream,sepCar);

}


void StreamBuffer::sendBufferBinaryTimeStamp(Stream &stream, unsigned long timeStamp){
String header="#"+ String(String(_currentIndex).length())+String(_currentIndex*sizeof(uint8_t)+sizeof(unsigned long));

stream.print(header);
stream.write((char *) timeStamp, sizeof(unsigned long));
stream.write((char *) _buffer, (_currentIndex)*sizeof(uint16_t));

}


StreamBuffer::~StreamBuffer(){
delete[] _buffer;
delete[] _lsbBuffer;
delete[] _msbBuffer;
}
Loading