Re: [Eu 3.1]How to send many files to Arduino via USB?

new topic     » goto parent     » topic index » view thread      » older message » newer message
ryanj said...
DanM_anew said...

I see that SOH is Start Of Heading, STX is Start Of Text, and ETX is end of text. I presume EOT is just end of block? And do you use any special characters sent to define the different parts of the format, or is that just taken care of by the defined position in the block? The BlockLength should work for me, the only variation in block length will be the last block from a file, or a file that's shorter than the defined block size I'll send.

I looked at an ASCII chart and picked those particular ASCII codes, because i thought they made the most sense. Here is the Euphoria version for reading a packet, to get you started. It gets an unknown number of bytes for the serial port, and attempts to process it until enough bytes come in to make the complete packet. If it takes too long, it times out.

constant 
SOH = 1,  
STX = 2, 
ETX = 3, 
EOT = 4, 
MAXMSGLEN = 96 
 
procedure device_read_task() 
    atom Timeout, MsgLen, MsgCRC 
    sequence InBuffer = {}, MsgData 
    object inb 
     
    while 1 do 
        Timeout = time() 
        inb = usb:Read() --read bytes in buffer 
        if sequence(inb) and length(inb) > 0 then 
            Timeout = time() 
            InBuffer &= inb 
        end if 
     
        MsgData = {} 
        for b = 1 to length(InBuffer) do 
            if (length(InBuffer) - b + 1) > 7 then 
                if InBuffer[b] = SOH then 
                    MsgLen = InBuffer[b+1] 
                    if MsgLen <= MAXMSGLEN and InBuffer[b+2] = STX then 
                        if length(InBuffer) >= b + 7 + MsgLen - 1 then 
                            if InBuffer[b+3+MsgLen] = ETX and InBuffer[b+3+MsgLen+3] = EOT then 
                                MsgData = InBuffer[b+3..b+3+MsgLen-1] 
                                 
                                process_message(MsgData[1], MsgData[2..$]) --process packet data (msgtype, msgdata) 
                                 
                                InBuffer = {} 
                                exit 
                            end if 
                        end if 
                    end if 
                end if 
            end if 
        end for 
         
        if time() - Timeout > 0.5 then 
            InBuffer = {} 
        end if 
         
        task_yield() 
    end while 
end procedure 

Oh, ok, there ARE code characters defining the starts of the different fields in the block sent, good to know! I'll study your code, I'm going in the other direction, sending from an Eu coded 'terminal' pgm, received on Uno with C or C plus plus subset or something like that, but the general idea has to be about the same in order that the handshaking work, so I'll try to transpose that into the Uno code! smile .

Here's what I'm doing here in Eu terminal just after a send of a block to the Uno, I can't really tell if it's doing what I want, it's supposed to be waiting for a response FROM the Uno, indicating eventually if it's ok to send another block (or resend same one again):

     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 = {} 
     		val = serial_getc(hCom1)  -- AND THIS RECEIVES AND DISPLAYS ANY RESPONSE 
	        if val != -1 then 
	          val &= serial_get_bytes(hCom1,length(chunk)) -- SHOULD get whole echo 
              puts(1, "\nIT GOT AN ECHO!  " & val & "\n") 
              puts(1, "length of echo= " & sprint(length(val)) & "\n\n") 
	          exit 
            end if 
     end while 

Does it seem like it's trying to do what I want? All I'm wanting it to do is wait to receive the handshake from the Uno, one or two responses, "all ok, send more", or "resend last block" eventually, but I'm not sure I'm doing it right in this initial version.

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

Search



Quick Links

User menu

Not signed in.

Misc Menu