Closed ThierryDFR closed 1 year ago
Welcome @ThierryDFR. This is open source and we welcome contributions. Since you have access to a CH340e, you can fork the code and either edit the baud array or calculate the values from the array, and test for 921,600. And hopefully some of the other supported rates.
To set the baud rate for CH340, you need to set the baud rate and two additional values, a prescaler register and a divisor register. There is a GH repo that documents this at https://github.com/nospam2000/ch341-baudrate-calculation#how-is-the-mapping-between-those-variables-and-the-registers-of-the-ch341.
You may also want to look at the source code for the Linux driver for the CH341 at https://github.com/torvalds/linux/blob/master/drivers/usb/serial/ch341.c
This library was ported from a java based one a few years back. It looks like they have updated their CH341 driver to calculate the prescaler and divisor instead of using the lookup array. If you want to adapt that code, it's in the setBaudRate method of CH34xSerialDriver.java. This would be the best option. The Java code should map more or less directly to C#. It's pretty much what I did when I id the initial port to C#.
I do not have access to a CH34x device, but this should be relatively easy to update the C# CH341 SetBaudRate from the current Java version.
If it works, you can submit a PR.
Online sources (like this one) list the following baud rates for the CH340 family:
50, 75, 100, 110, 134.5, 150, 300, 600, 900, 1200, 1800, 2400, 3600, 4800, 9600, 14400, 19200, 33600, 38400, 56000, 57600, 76800, 115200, 128000, 153600, 230400, 460800, 921600, 1500000, 2000000
Since the current code passes baud as an int, the 134.5 baud rate wouldn't be supported.
Hi Chirs, Many many thanls for your help. That's work perfectly !!! I send to you all information for update your library .... I have translate the "setBaudRate" java function in c# from your link (without "ControlOut" function for only extract Val1 and Val2 data). I have tested some values and that's works perfectly except above 921600 (My USB phone ? Cables ? My ESP32-CAM ?). For 1500000 bauds, ESP32-cam hang and reboot sometimes. For 2000000 bauds, dialogue is bad all the time.
This is the complete list of all values for all baud rates managed by CH340 with product ID 0x7523 (table.AddProduct(0x1A86, 0x7523, typeof(Ch34xSerialDriver)); // ESP32cam programmer), except : 134.5 bauds :
int[] baud = new int[] { 50, 0x1680, 0x0024, 75, 0x6480, 0x0018, 100, 0x8B80, 0x0012, 110, 0x9580, 0x00B4, 150, 0xB280, 0x000C, 300, 0xD980, 0x0006, 600, 0x6481, 0x0018, 900, 0x9881, 0x0010, 1200, 0xB281, 0x000C, 1800, 0xCC81, 0x0008, 2400, 0xD981, 0x0006, 3600, 0x3082, 0x0020, 4800, 0x6482, 0x0018, 9600, 0xB282, 0x000C, 14400, 0xCC82, 0x0008, 19200, 0xD982, 0x0006, 33600, 0x4D83, 0x00D3, 38400, 0x6483, 0x0018, 56000, 0x9583, 0x0018, 57600, 0x9883, 0x0010, 76800, 0xB283, 0x000C, 115200, 0xCC83, 0x0008, 128000, 0xD183, 0x003B, 153600, 0xD983, 0x0006, 230400, 0xE683, 0x0004, 460800, 0xF383, 0x0002, 921600, 0xF387, 0x0000, 1500000, 0xFC83, 0x0003, 2000000, 0xFD83, 0x0002 };
If you see value for 115200 bauds for example, the value is not the same that your actual value, after somes tests the connection is more stable with this new values 115200 bauds.
I send to you the translate code of the new java "setBaudRate" (without "ControlOut" function for only extract Val1 and Val2 data) :
private struct Values { public int Baud; public int val1; public int val2; public long factor; public long divisor; public override String ToString() { return Baud + "\t\t"
for usage : textBox1.Text = textBox1.Text + "\r\n" + setBaudRate(115200).ToString();
Many thanks, your library is perfect now with this new values !!
I just merged your PR into main. Thank you!
Hi and thanks for your library.
I use a CH340e and In your code I see the baud rate supported : int[] baud = new int[] { 2400, 0xd901, 0x0038, 4800, 0x6402, 0x001f, 9600, 0xb202, 0x0013, 19200, 0xd902, 0x000d, 38400, 0x6403, 0x000a, 115200, 0xcc03, 0x0008 };
But I want use a higger baud rate of 921 600 bauds. It is possible with your library ? if it is possible what is the good values for 921600 to complete the int[] baud value accepted ?