Click here to Skip to main content
15,887,083 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Update: I've solved it but I don't know how so I can't write a solution for this question

At least I think that's what is happening. It is rough to find out which end the code seems to be failing on.

Basically I have a C# application sending data to an ESP32 once a second over USB CDC serial using the SerialPort class at 4x115200 baud. (8N1)

It sends the title packet okay, which is very small, dependent on the length of the title string.

It also sends a 2052 byte icon packet and that's where the trouble is.

It will function if I drop to the debugger and break on that line, and then continue.

It used to function at least at one point when I dropped a Thread.Sleep in there (after i found out debugging made it go)

So I thought it might be the size of the write buffer, which I increased to 16kB, and the read buffer on the other end, same

Then I figured maybe it's because the UI timer fires on a separate thread? So I thunked my SerialPort calls down to the UI thread using Control.Invoke

Then I thought, maybe I'll try chunking it, writing 64 bytes at a time, so I did that. It seemed to make it so not even the text sent (even though I'm not chunking the text portion)


Stranger still If I keep sending the icon packet once a second it seems to force it, but if I put a keepalive packet in there just to keep things alive when no data has changed the icon stops working.

What I have tried:

here's the meat of it (after all the data is prepared)

C#
string title = Path.GetFileNameWithoutExtension(path);
// open if necessary
if (!port.IsOpen)
{
	port.WriteTimeout = 1000;
	port.WriteBufferSize = 16384;
	port.Open();

}
var ba = new byte[5 + title.Length];
ba[0] = 1;
BitConverter.GetBytes(title.Length).CopyTo(ba, 1);
Encoding.ASCII.GetBytes(title).CopyTo(ba, 5);


// write the command
port.Write(ba, 0, ba.Length);
port.BaseStream.Flush();
if (iconBuffer != null)
{
	// open if necessary
	if (!port.IsOpen)
	{
	    port.WriteTimeout = 1000;
		port.WriteBufferSize = 16384;
		port.Open();
	}
	ba = new byte[iconBuffer.Length + 5];
	ba[0] = 2;
	BitConverter.GetBytes(iconBuffer.Length).CopyTo(ba, 1);
	iconBuffer.CopyTo(ba, 5);
	System.Threading.Thread.Sleep(100);
	// write the command
	port.Write(ba, 0, ba.Length);

	port.BaseStream.Flush();
}
Posted
Updated 11-Nov-23 14:15pm
v2
Comments
0x01AA 11-Nov-23 17:11pm    
And you are sure about you are the one who control the handshake?
E.g. be sure you disabled xon/xoff, rts/cts etc?

And btw. baudrate you don't need to bother about with 'USB CDC'. USB will take over here.
honey the codewitch 11-Nov-23 17:13pm    
its usb cdc serial. if there's a way to control handshaking on it, i wouldn't know. this same technique works for other projects.
Ralf Meier 11-Nov-23 19:19pm    
it could be helpful if you tell us (me) something more about the protocoll.
To get something similar work I would also let it 1st work inside the UI-Thread.
honey the codewitch 11-Nov-23 20:14pm    
I solved it but I'm not sure how exactly so I don't know what to put for a solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900