1. [Eu 3.1] Serial problem: not reading whole return?
- Posted by DanM_anew Mar 27, 2014
- 1249 views
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)
2. Re: [Eu 3.1] Serial problem: not reading whole return?
- Posted by BRyan Mar 27, 2014
- 1309 views
Dan usb is not the same as rs232 serial.
It has its own protocol.
Go to to this url maybe this will help.
http://www.beyondlogic.org/usbnutshell/usb3.shtml
You may also need some usb driver.
If I don't understand what you are doing then you can ignore this post.
3. Re: [Eu 3.1] Serial problem: not reading whole return?
- Posted by DanM_anew Mar 27, 2014
- 1295 views
Dan usb is not the same as rs232 serial.
It has its own protocol.
Go to to this url maybe this will help.
http://www.beyondlogic.org/usbnutshell/usb3.shtml
You may also need some usb driver.
If I don't understand what you are doing then you can ignore this post.
Heh, BRyan, I'm not in a position to ignore ANY potential help! But so far it seems that treating the USB as simply another recognized port is quasi-working, at least one way, to the Arduino micro-controller, and probably either my procedure to receive its echo back, or the nature of the sending of the echo from the Arduino, or maybe a combination of the two, is what my problem is. But I'll take a look at your link, thanks.
Dan
4. Re: [Eu 3.1] Serial problem: not reading whole return?
- Posted by Fernando Mar 27, 2014
- 1257 views
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)
Hi Dan,
Maybe your problem is related to the serial timeouts. Use the function SetCommTimeouts() to adjust the timeouts for your case. For example, in some projects, I used:
SetCommTimeouts(hCom1,{30,5,100,5,100})
--SetCommTimeouts Sets the time-out parameters for all read and write operations on a specified communications device. --paramters: hCom communction device handle returned by serial_open() -- timeouts sequence {ReadIntervalTimout, (maximum allowed timeout between two character receive -- ReadTotalTimeoutMultiplier, ('requested number of char'*ReadTotalTimeoutMultiplier -- ReadTotalTimeoutConstant, + ReadTotalTimeoutConstant = ReceptionTimeout) -- WriteTotalTimeoutMultiplier, ('number of char written'*WriteTotalTimeoutMultiplier -- WriteTotalTimeoutConstant} + WriteTotalTimeoutConstant = TransmissionTimeout
Link: http://msdn.microsoft.com/en-us/library/aa363190(v=vs.85).aspx
After sending data to serial, I have used the following function to wait for serial response:
function WaitResponse(atom timeout) atom t0 integer val sequence lin lin = {} t0 = time() while time() - t0 < timeout do val = serial_getc(hCom1) if val != -1 then if val = CR then return lin else lin &= val end if end if end while return TIMEOUT_ERROR end function
Regards,
Fernando