
import time
import os
import serial
import glob


ER_Address = [[0x0A,0x01,0x08,0x99,0xBB,0xBB,0xBB,0xBB,0xBB,0xBB,0xBB]]
"""
----------------------------------------------------------------------------------------------------------------------------
Function Name: RunSequence
----------------------------------------------------------------------------------------------------------------------------
Function Description:
---------------------
A RunSequence function has Multiple calls to the RunSuite function, each call is a single testcase consisting of all the parameters
required by the RunTesSuite.
-----------------------------------------------------------------------------------------------------------------------------
"""
def WriteSerialData(command):    
    ser.write(port,command)
                             
def SendPacket(Packet):    
    str = chr(len(Packet)) + Packet #Concatenation of Packet with the PacketLength
    print str
    WriteSerialData(str)


def CreateFrame(Edata):
        evt = chr(0x12)
        evt = evt + chr(Edata[0])
        for i in range (1, len(Edata)):            
            evt = evt + chr(Edata[i])
        return evt
    
def SetRequest(data):
    print data
    new = []
    new = sum(data, [])
    Addr = CreateFrame(new)    
    SendPacket(Addr)
    print "SendPacket Done"
    ReadPacket()

    
def OpenPort(COMPort,BAUDRATE):
    """
    This function reads the serial port and writes it.
    """
    comport=COMPort
    BaudRate=BAUDRATE
    try:
        ser = serial.Serial(
            port=comport,
            baudrate=BaudRate,
            bytesize=serial.EIGHTBITS,
            parity=serial.PARITY_NONE,
            stopbits=serial.STOPBITS_ONE,
            timeout=10,
            xonxoff=0,
            rtscts=0,
            dsrdtr=0
            )
            
        if ser.isOpen():            
            print "Port Opened"
            ser.write("Chandan")
            string1 = ser.read(8)
            print string1                       
            return ser
        else:
            print "Port CLosed"
            ser.close()
            return 3
    except serial.serialutil.SerialException:
        print "Exception"
        ser.close()
   
     




if __name__ == "__main__":
    
    CurrDir=os.getcwd()
    files = glob.glob('./*pyc')
    for f in files:
        os.remove(f)        
    OpenPort(26,9600)
    SetRequest(ER_Address)
    #SysAPI.SetRequest('ER',ER_Address)    

    print "Test Scripts Execution complete"    
    




