' Demo: Einfache, nicht blockierende Kommunikation ' mit SerialComInstruments ' www.serialcominstruments.com ' Ulrich Maassen , Version 2a mit TimeOut Check vom 31.07.2014 ' ' Es wurde bewust teilweise mit rechenintensiveren ' String-Operationen gearbeitet um das Programm zu ' vereinfachen und auch dem Ungeübteren die Möglichkeit ' zu geben leicht Änderungen durchzuführen zu können. avr.device = atmega328p avr.clock = 16000000 avr.stack = 50 uart0.baud = 115200 uart0.Rxd.Fifo = 32 uart0.Rxd.enable uart0.txd.enable const startChar = asc("#") const endChar = asc("<") const TaskRepeatTime = 50 ' ms const TimeOutMultiplier = 20 ' TimeOutTime = TaskRepeatTime * TimeOutMultiplier #library "Library/TaskTimer.interface" TaskTimer.Init(Timer1,1,TaskRepeatTime) TaskTimer.Task(0) = TaskReady().addr dim read_ch, read_byte_array(32), array_counter, i_array as byte dim taskFlag, dataValid, timeOutCheck, timeOutEnable as boolean dim result_str, tempStr as string dim timeOutTime, timeOutCount as word Avr.Interrupts.Enable read_byte_array(32) = 0 array_counter = 0 tempStr = "" dataValid = false TaskFlag = false timeOutCount = 0 timeOutTime = TaskRepeatTime * TimeOutMultiplier ' in ms timeOutCheck = false timeOutEnable = true ' legt fest ob auf TimeOut geprüft wird ' Main Loop do when taskFlag = true do readCommandString() if timeOutEnable then ' TimeOut Überprüfen when (TimeOutCount > timeOutTime) do TimeOutProc() endif // do Something loop procedure TaskReady() TaskFlag = true endproc procedure TimeOutProc() TimeOutCount = 0 timeOutCheck = false dataValid = false tempStr = "" array_counter = 0 uart0.flush ' do Something, z.B. Meldung machen print "#38M---TimeOut---<" ' endproc procedure readCommandString() taskFlag = false when (TimeOutCheck = true) do TimeOutCount = TimeOutCount + TaskRepeatTime ' Read Command String while ( uart0.RxReady() ) read_ch = uart0.ReadByte select case read_ch case startChar dataValid = true if (timeOutEnable = true) then timeOutCount = 0 timeOutCheck = true endif case endChar read_byte_array(array_counter) = read_ch for i_array = 0 to array_counter tempStr = tempStr + mkb(read_byte_array(i_array)) next result_str = tempStr timeOutCount = 0 timeOutCheck = false parse_string() dataValid = false tempStr = "" array_counter = 0 endselect if dataValid = true then read_byte_array(array_counter) = read_ch incr array_counter endif wend endproc procedure parse_string() Dim InstrNr, posM, x as byte InstrNr = val(mid(result_str,2,3)) posM = Instr(result_str,"M") select Case InstrNr case 70 ' Taster 70 when (result_str = "#70M1<") do print "#38MTaster 70 down<" when (result_str = "#70M0<") do print "#38MTaster 70 up<" case 71 ' Taster 71 when (result_str = "#71M1<") do print "#38MTaster 71 down<" when (result_str = "#71M0<") do print "#38MTaster 71 up<" case 75 ' DIP-Schalter x = val(mid(result_str,posM + 1,3)) print "#38MDIP = " + Str(x) + "<" ' usw. endselect endproc