Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 6 months ago.
MTSSerial write() functions not blocking as per library documentation
I am having a problem sending serial data using the MTSSerial library. It appears that the library code does not check the status of the TX buffer before adding additional data.
Am I using the correct library of the DragonFly?
Setup
I have the development kit and I am sending the data through the RS232 port to my PC. I have a program that displays each byte received on the PC.
Problem
The documentation in MTSBufferedIO.h says: /* This method enables bulk writes to the Tx or write buffer.*/ int write(const char* data, int length, unsigned int timeoutMillis);
/ This method writes a single byte as a char to the Tx or write buffer.
- This method blocks until the byte is written. */ int write(char data);
Neither of these two methods block. If I add a delay between successive calls to write(char) then I correctly receive each byte on the PC. (Adding delays throws off the timing between bytes, which makes this method impractical.)
Test Program (See below)
This program is configured to send 10 bytes through the RS232 port. The port is configured as 8 data bits, odd parity, 1 stop bit, 1200 baud.
Results
If I add a delay between each byte sent, I receive all the data correctly. If I do not add a delay I get partial data, with the odd byte being correct.
- include "mbed.h"
- include <iostream>
- include "MTSSerial.h"
DigitalOut bc_nce(PB_2);
int main() { Disable the battery charger unless a battery is attached. bc_nce = 1; char Send_Buffer[160]; int i; wait(2);
/* Must use MTSSerial to get correct configuration 8O1 serial setting.
- / mts::MTSSerial sp(D1,D0); sp.baud(1200); sp.format(8,SerialBase::None,1);
for (i=0; i< 10; i++) Send_Buffer[i] = i;
wait(1); wait for serial port pin voltage to change. sp.write(Send_Buffer,10,200); doesn't block.
wait(1); wait for serial port pin voltage to change. for (i=0; i< 10; i++) { /* The code commented out did not work. The serial port is always writeable. The serial port txFull is always false. I added printf statements to test these conditions. The printf statements have been removed to simplify this code.
- / while (sp.writeable()==0) while (sp.txFull()==true) { wait(0.0096f); adding delay between successive writes fixes the TX buffer overwriting problem. } sp.write(Send_Buffer[i]); }
while (1) { do nothing } }
Do you still have this problem?
posted by Vladimir Akopyan 13 Aug 2016I have the serial communication working correctly now. That was almost 3 months ago and I don't remember all of the details now. I had a lot of problems related to the odd parity but they were M4 configuration problems. I had to manually change the UART settings to get 8 data bits and have the odd parity sent as a 9th bit. The library default settings use the 8th bit as the odd parity bit. I needed all 8 data bits.
posted by Will Blight 15 Aug 2016