Serial.write problem

Post here first, or if you can't find a relevant section!
vitor_boss
Posts: 61
Joined: Wed Apr 19, 2017 9:50 am

Re: Serial.write problem

Post by vitor_boss » Wed Apr 19, 2017 7:16 pm

edogaldo wrote:
vitor_boss wrote:This is the problem, before that call the code use the same loop for a 288 bytes Serial.write and a lot of Serial.print and all work fine, only that lines aren't giving outputs

code is at https://github.com/noisymime/speeduino/ ... /comms.ino
Hi vitor, I can't see any uint8_t/uint16_t in the code you linked, it would be better if you post the real code you are using..
I changed the code trying to make it work but doesn't. I have changed int to uint16_t to keep data alignment across platforms.
victor_pv wrote:
stevestrong wrote:If it is about this part of your code, then it will not work at least because sizeof(response) is actually not explicitly defined, but only indirectly through a variable value. So I assume you have a bug in your code.
Didn't you have some compiler warnings?
I think I agree with Steve on that. If I understand it right, sizeof is calculated at compile time, but you seem to use a variable to set the size.
Wouldn't be better to change the serial.write line to this?
Serial.write((byte *)&response, npage_size[currentPage]);

As to why would it work on a different mcu, you probably should check if the Serial.write implementation is the same or not.
Thanks, I will try that

vitor_boss
Posts: 61
Joined: Wed Apr 19, 2017 9:50 am

Re: Serial.write problem

Post by vitor_boss » Wed Apr 19, 2017 7:18 pm

stevestrong wrote:vitor, could you please explain what exactly means "it didn't work"?
Are any bytes sent over serial? Are they wrong? Any serial monitor log available to show the problem?

I would try this way:

Code: Select all

Serial.write(response, sizeof(response));
It seems that an 16 bit value (x) is added and stored as an 8 bit value (response[n]), so that an overflow can occur. Is this wanted?
I have no bytes output

Edit: Changed code to yours and don't work too.

vitor_boss
Posts: 61
Joined: Wed Apr 19, 2017 9:50 am

Re: Serial.write problem

Post by vitor_boss » Wed Apr 19, 2017 7:25 pm

That is my software log:

Code: Select all

19/04/17 16:16:27 :Info: Activating page: 1
19/04/17 16:16:27 :Time: 0:10.602: SENT 2 bytes iwd=10, 2 bytes
x50 x01                                                               P.

19/04/17 16:16:27 :Time: 0:10.613: SENT 1 bytes iwd=10, 1 bytes
x56                                                                   V

19/04/17 16:16:28 :Debug: Read page time: 174ms.
19/04/17 16:16:28 :Time: 0:10.763: READ 288 bytes, 288 bytes
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................
x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00 x00       ................

19/04/17 16:16:28 :Info: Activating page: 2
19/04/17 16:16:28 :Time: 0:10.785: SENT 2 bytes iwd=10, 2 bytes
x50 x02                                                               P.

19/04/17 16:16:28 :Time: 0:10.795: SENT 1 bytes iwd=10, 1 bytes
x56                                                                   V

Time: 0:13.797: READ Timout after 0 bytes, Expected:64, actual read:0 Raw buffer
19/04/17 16:16:31 :Debug: Time:13.796s. Instruction: 

vitor_boss
Posts: 61
Joined: Wed Apr 19, 2017 9:50 am

Re: Serial.write problem

Post by vitor_boss » Wed Apr 19, 2017 7:37 pm

BennehBoy wrote:Can you not just write out a single byte at a time in the loop?

I'm using Serial.write in some ecu code, but only 1 byte at a time.
That worked for 64 bytes, thank you, but on Line 773 with a 192(3x64) bytes if fails too.

Edit: worked for first reading, next readings only output 50 bytes

stevestrong
Posts: 1520
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Serial.write problem

Post by stevestrong » Thu Apr 20, 2017 9:40 am

If it works once, it should work always.
Your software is buggy.

I would suggest to get rid (remove) of sizeof(response), and use either previously defined constants or appropriate variables, where needed.
Also, define only once the "response[]" buffer of a maximum size (192?), and control the length of data as said.

victor_pv
Posts: 1607
Joined: Mon Apr 27, 2015 12:12 pm

Re: Serial.write problem

Post by victor_pv » Thu Apr 20, 2017 8:25 pm

vitor_boss wrote: Thanks, I will try that
Did you try changing the line to this as I suggested?

Code: Select all

Serial.write((byte *)&response, npage_size[currentPage]);

vitor_boss
Posts: 61
Joined: Wed Apr 19, 2017 9:50 am

Re: Serial.write problem

Post by vitor_boss » Fri Apr 21, 2017 10:14 am

victor_pv wrote:
vitor_boss wrote: Thanks, I will try that
Did you try changing the line to this as I suggested?

Code: Select all

Serial.write((byte *)&response, npage_size[currentPage]);
Yes, no output bytes either.

ag123
Posts: 719
Joined: Thu Jul 21, 2016 4:24 pm

Re: Serial.write problem

Post by ag123 » Fri Apr 21, 2017 10:24 am

try with

Code: Select all

Serial.write("0123456789", 10); 
at the code location, commenting the original codes

you could make a longer string which is similar to the size that you need.

this may help to troubleshoot it. if necessary you may like to trouble shoot with a 'hard coded' byte pattern resembling your output and calling it there
and in addition to test if the particular byte pattern is anticipated there e.g. check the byte sequence for correctness and turn on the led if it is correct

note that with usb-serial you are also dealing with a virtual com port on the os side e.g. windows. i'm not too sure if windows etc may be doing things like software flow control and sequences which means that control-q and control-s characters may stall your windows terminal from receiving the data
https://en.wikipedia.org/wiki/Software_flow_control
you may need to 'escape' those control-q, control-s sequences and substitute with an encoding that doesn't use those

just 2 cents

victor_pv
Posts: 1607
Joined: Mon Apr 27, 2015 12:12 pm

Re: Serial.write problem

Post by victor_pv » Fri Apr 21, 2017 3:46 pm

vitor_boss wrote:
victor_pv wrote:
vitor_boss wrote: Thanks, I will try that
Did you try changing the line to this as I suggested?

Code: Select all

Serial.write((byte *)&response, npage_size[currentPage]);
Yes, no output bytes either.
Quick question, shouldn't you be using just "response" rather than "(byte *)&response" since response is an array?
Such as:

Code: Select all

Serial.write(response, npage_size[currentPage]);

vitor_boss
Posts: 61
Joined: Wed Apr 19, 2017 9:50 am

Re: Serial.write problem

Post by vitor_boss » Sun Apr 23, 2017 2:48 pm

victor_pv wrote:
vitor_boss wrote:
victor_pv wrote:
Did you try changing the line to this as I suggested?

Code: Select all

Serial.write((byte *)&response, npage_size[currentPage]);
Yes, no output bytes either.
Quick question, shouldn't you be using just "response" rather than "(byte *)&response" since response is an array?
Such as:

Code: Select all

Serial.write(response, npage_size[currentPage]);
Sorry for delay, yes I have tested this and no output too.

I have disables windows PEPS buffers and now it's working. Seems to this UART implementation is not compatible with UART v16550 with supports PEPS

Post Reply