Serial.write problem

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

Serial.write problem

Postby vitor_boss » Wed Apr 19, 2017 10:18 am

Hi, this is my first post on this forum, currently I'm help to develop Speeduino EMS/ECUand now I'm trying to convert code to Maple mini (STM32F103CBT6).

One of communication protocol use Serial.write and I'm facing problems with. Code is:

Code: Select all

    //All other bytes can simply be copied from the config table
    uint8_t response[64];
    //npage_size[] is an int type
    for (uint16_t x = 0; x < 64; x++)
    {
      response[x] = *((uint8_t *)pnt_configPage + (uint16_t)(x)); //Each byte is simply the location in memory of the configPage + the offset + the variable number (x)
    }

    Serial.write((uint8_t *)&response, sizeof(response));


This works on Arduino Mega and Teensy but it didn't work on STM32. Any idea why?
If I add a Serial.print(NULL) after Serial.write the USB serial gives a 65 bytes output.

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

Re: Serial.write problem

Postby ag123 » Wed Apr 19, 2017 10:38 am

try calling Serial.begin(); in setup()
when you plug the the board in the usb port, it should be detected by the os. in linux this can be viewed in lsusb command
i think in windows, you may be able to check that in the device manager

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

Re: Serial.write problem

Postby vitor_boss » Wed Apr 19, 2017 10:43 am

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

User avatar
BennehBoy
Posts: 404
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Serial.write problem

Postby BennehBoy » Wed Apr 19, 2017 10:51 am

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.
-------------------------------------
https://github.com/BennehBoy

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

Re: Serial.write problem

Postby vitor_boss » Wed Apr 19, 2017 11:09 am

I could test it, but that is not the point, on Mega and Teensy it works with no problem, why isn't working with Maple mini?

User avatar
BennehBoy
Posts: 404
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Serial.write problem

Postby BennehBoy » Wed Apr 19, 2017 11:38 am

Why is the sky blue? :D Sometimes it's better to just get a n acceptable workaround.
-------------------------------------
https://github.com/BennehBoy

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

Re: Serial.write problem

Postby stevestrong » Wed Apr 19, 2017 11:43 am

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?
Last edited by stevestrong on Wed Apr 19, 2017 12:11 pm, edited 1 time in total.

edogaldo
Posts: 213
Joined: Fri Jun 03, 2016 8:19 am

Re: Serial.write problem

Postby edogaldo » Wed Apr 19, 2017 12:06 pm

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..

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

Re: Serial.write problem

Postby stevestrong » Wed Apr 19, 2017 12:15 pm

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?

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

Re: Serial.write problem

Postby victor_pv » Wed Apr 19, 2017 6:44 pm

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.


Return to “General discussion”

Who is online

Users browsing this forum: No registered users and 2 guests