Serial port communication

For general Flowcode discussion that does not belong in the other sections.
mnfisher
Valued Contributor
Posts: 938
http://meble-kuchenne.info.pl
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Serial port communication

Post by mnfisher »

Based on an a topic some time back...

I wanted to transmit some data to an Arduino (choice of MCU is irrelevant here though - needs a UART) - via a com port on a PC.
The Arduino needs to process the data - so must signal the PC to 'stop' whilst it does so. Previously I used a Python script that waited for an acknowledgement after each block (buffer full) of data.

I tried to use XOn and XOff to do the same job with the Windows 'copy' command.
This 'sort' of works well.. However I can only get it to work at 9600 baud with a very specific delay after sending XOff.
Anyone able to give some ideas why this isn't working for other parameters? (for example 19200baud doesn't work)

The code here works well (try sending a file of < 65536 bytes) - I use a glcd1306 i2c to display the number of bytes received (and printNumber works for 16 bits)

After sending 'XOff' there is possibly some 'overrun' before the comms pauses (which I've tried to allow for - here I send XOff after 128 bytes received, I allow for up to 1024 bytes by the time the stop has taken effect..)

In a windows 'cmd' box

Code: Select all

mode com3 baud=9600 parity=n data=8 xon=on to=off
Modify for relevant com port (need \\.\com for port numbers > 10) - this sets baud, parity, data bits, timeout to off and xon control on.
Then

Code: Select all

copy filename /b com3
(I think /b (binary) might be the default /a is ascii for text files).
uart_test.fcfx
(16.92 KiB) Downloaded 152 times
One problem this doesn't address is 'end of file' - for my needs the Arduino can calculate the length of the file from it's header. For my test file (10795 bytes and others) - it seems to work fine - but I can't see how 'data' is set true to ever display this (but it does - but as far as I can see the final bytes should be lost... I added a crude timeout - but it didn't make a difference..)

I suspect that XOff isn't actually stopping transmission and the buffer is large enough to cope with a 500ms delay.
Anyone any thoughts on this??

Martin

chipfryer27
Valued Contributor
Posts: 1109
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 278 times
Been thanked: 396 times

Re: Serial port communication

Post by chipfryer27 »

Hi

Not sure I can be of much help but I too have had issues implementing software control in the past. Does your circuit allow for hardware control for comparison?

Regards

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Serial port communication

Post by mnfisher »

Thanks,

No - sadly I don't think DTR, CTS are connected on the Arduino.
Looking at a file transfer using RealTerm - it does send XOff Xon pairs correctly.. I'd like to be proved wrong, but after several hours fiddling - I think that they are ignored. One possibility is that they are ignored for a binary file (where they might appear as data) - I'd hoped that as the sender is only receiving this and the Arduino the binary data this wouldn't be the case.
That gives two options - escape the Xoff Xon characters, or convert the files to ascii (hex) and send it like that - but both are an extra step I'd hoped to avoid...

Martin

medelec35
Matrix Staff
Posts: 1429
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 505 times
Been thanked: 468 times

Re: Serial port communication

Post by medelec35 »

I have not used Flow Control ,but my guess it would only let 1 char at a time be sent and halt from sending anymore during transmission.
For that It would be the RTS line from PC to Arduino.
As RTS is a signal if the Arduino does not halt automatically, then you could connect RTS from the PC to an interrupt pin that will pause the data being sent.
Martin

stefan.erni
Valued Contributor
Posts: 738
Joined: Wed Dec 02, 2020 10:53 am
Has thanked: 149 times
Been thanked: 170 times

Re: Serial port communication

Post by stefan.erni »

Hi Martin

Maybe this hint helps a little.

It's sending /receive a buffer in one go. It's possible to send a buffer with byte or integer .
Should work with the app developer and Arduino

I hope Ben makes these different buffers for the ESP32 bluetooth too.
That would be one of the fastest and easiest connections.

https://flowcode.co.uk/forums/viewtopic.php?f=4&t=186

regards

Stefan
Uart_2021-03-15_9-38-03.png
Uart_2021-03-15_9-38-03.png (193.73 KiB) Viewed 4862 times

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Serial port communication

Post by mnfisher »

Thanks - might be a possibility in the long run, but still need to do some sort of flow control.

I think that the approach with XOff will work if I go to an ASCII file (hex digits) - and powershell has some tools that can work to convert a file to hex.
More fun this evening..

Martin

chipfryer27
Valued Contributor
Posts: 1109
Joined: Thu Dec 03, 2020 10:57 am
Has thanked: 278 times
Been thanked: 396 times

Re: Serial port communication

Post by chipfryer27 »

Hi

My understanding is that Xon / Xoff are ascii and can't be present in data.

I don't have the greatest Arduino knowledge but from forums it seems it's going to be tricky for you https://forum.arduino.cc/index.php?topic=15676.0 as neither H/W or S/W control is supported. I stress my knowledge on the Arduino subject is from that forum so it could be wrong, and of course isn't being programmed in FC.

Following on from Medlec35 regarding implementing your own H/W. This might not be too difficult (famous last words). You want to tell the PC to stop sending data before your Arduino buffer is full. Would it be possible to use a Circular Buffer to store the incoming data? You can poll the buffer to establish how full it is and when at a determined value you could toggle a free port(s) to use as CTS/RTS. After processing the buffer you again toggle the port(s) to signal the PC to start transmitting.

Just a guess.

Regards

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Serial port communication

Post by mnfisher »

Thanks,

Yes - binary files may include xon, xoff characters - I hoped that because the pc was sending data and only receiving xon, xoff - I could get around this. Seems not, sadly. I'm using a circular buffer (in effect) - and the sample above seem to have enough space to skip any timing issues - but it won't cope in the final implementation - transmission either needs to pause or be so slow that the MCU can keep up..

ASCII text files have the advantage that you can use this for flow control - so convert to a string of hex, but the transfer will be (at least) twice as big. Can also use EOF to signal end of file.

For this use case - a hardware option is not suitable - but it's interesting for future use (I,m thinking of using a ftdi convertor with attiny85 for example and then using dtr/CTS becomes a possibility though lack of pins might scupper it yet) Similarly I wanted to move away from the python script with the extra user requirements (python install) - I probably should have just converted the python to an exe file. Hindsight is good..

Martin

mnfisher
Valued Contributor
Posts: 938
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 104 times
Been thanked: 502 times

Re: Serial port communication

Post by mnfisher »

I've come to the conclusion that this isn't going to work (or I'm not going to be able to get it to work :-( )

I can get it to transmit and then stop after a single block (I tried 16, 128 and 512 bytes) - but then the rest of the file is sent as a single large lump.
This might be related to the USB / Serial interface on the PC?
Querying the serial port gave a XOff value of 38 and XOn of 70 but no joy with these values either...

Martin

BenR
Matrix Staff
Posts: 1706
Joined: Mon Dec 07, 2020 10:06 am
Has thanked: 435 times
Been thanked: 598 times

Re: Serial port communication

Post by BenR »

Hi Martin,

Rather then using Windows command prompt what about using Flowcode app creator to parse the file and transmit to the COM port in blocks?

Post Reply