[Eu 3.1] Serial problem: not reading whole return?

new topic     » topic index » view thread      » older message » newer message

In my quest to send multiple files from host laptop to an Arduino Uno microcontroller, I have a Uno 'sketch' (program) which I think is both receiving and returning simple text files from an Euphoria serial program over the USB port/line, and the Euphoria program seems to be sending ok, but not quite receiving echo back ok.

I think part of my problem (aside from my programming 'skills'!) is that the echo from the Uno may be one character at a time as the line sent to it is received, and I'll try to address that, but it also seems that I'm not handling the return properly here on this end.

It does look like I'm somehow not clearing the returned echo properly between successive files sent, because what prints for the second file sent looks like just the rest of the text from the first file sent.

Can anyone help?

Here's the send (and receive echo) function, and after that follows some debug prints:

procedure SendToUno_PB_onClick (integer self, integer event, sequence params)--params is () 
   sequence filename 
    integer fn, x 
    object dirinfo 
    sequence data 
  sequence chunk 
  sequence whole_file 
  object val  -- for a return from Uno, currently echoed data sent? 
  whole_file = {} 
 
 
 
 
  -- first part must get list of files, and directory name 
  -- directory is: MP3directory, 
  -- files are in:  MP3files 
 
  eraseItems(List5) 
 
  for n = 1 to length(MP3files) do 
  puts(1, "\n number of files= " & sprint(length(MP3files)) & "\n") 
  	filename = MP3directory & "\\" & MP3files[n] 
 
       -- now you have to READ each file! 
    fn = open(filename, "rb") 
    if fn <= 0 then 
      --      setText(SB, sprintf("Unable to open file '%s'.", {filename})) 
        return 
    end if 
    chunk = {} 
    whole_file = {} 
    while 1 do  -- have to move parts of each file: 
      chunk = get_bytes(fn, 100) -- read 100 bytes at a time 
puts(1, "\nthe chunk is: " & chunk) 
-- this is probably where it SHOULD send a chunk to USB serial, 
-- and WAIT until given permission to continue? 
     serial_puts(hCom1,chunk)  -- try this (which seems to work) 
     -- now have to make it wait until Uno sends something back, 
     -- at this point ECHO sent data 
     -- so I can see if it's working 
     puts(1, "\nfile: " &sprint(n) & " this is just after read chunk; " ) 
     puts(1, "  length=   " & sprint(length(chunk)) & "\n") 
     while 1 do 
--     puts(1, "this is in the while to receive echo" & "\n") 
 
--read count bytes from serial port 
--global function serial_get_bytes(atom hCom, integer count) 
 
 
     		val = serial_getc(hCom1)  -- AND THIS RECEIVES AND DISPLAYS ANY RESPONSE 
	        if val != -1 then 
	          val &= serial_get_bytes(hCom1,100) -- SHOULD get whole echo 
              puts(1, "\nit got echo!  " & val & "\n") 
              puts(1, "length of echo= " & sprint(length(val)) & "\n") 
	          exit 
            end if 
     end while 
 
      whole_file &= chunk        -- chunk might be empty, that's ok 
      if length(chunk) < 100 then 
          exit 
      end if 
    end while 
 
    close(fn) 
   addItem(List5, whole_file) 
    chunk = {} 
 
 
  end for 
 
 
end procedure 
setHandler( SendToUno_PB, w32HClick, routine_id("SendToUno_PB_onClick")) 

DEBUG PRINTS: (I prettied the responses a little to make echos clearer)

number of files= 2

the chunk is: This is a short test file.
file: 1 this is just after read chunk; length= 26

it got echo! echo is: This
length of echo= 4

number of files= 2

the chunk is: This is a second short text file.
file: 2 this is just after read chunk; length= 33

it got echo! Echo is: is
length of echo= 4 (note: I suspect the '4' is space-is-space)

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu