У меня новое устройство
Я пытаюсь подключить его с помощью кабеля A к C, при подключении плагина включается экран и все.
Я уже искал информацию и тестировал с помощью приложения Hercules, оно открывает порт на устройство через COM, но при отправке данных ничего не происходит.
Я также тестирую код C# .NET, но он по-прежнему то же самое.
Вот код, который я использовал для подключения:
class Program
{
static SerialPort _port;
// Các tốc độ phổ biến của chip CH340
static int[] baudRates = { 9600, 19200, 38400, 57600, 115200 };
static void Main(string[] args)
{
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("
string portName = "COM4";
foreach (int baud in baudRates)
{
Console.WriteLine($"\n
if (TryBaudRate(portName, baud))
{
Console.WriteLine("------------------------------------------");
Console.WriteLine("HÀNH ĐỘNG: Rút USB ra cắm lại NGAY BÂY GIỜ!");
Console.WriteLine("Nếu thấy chữ vàng hiện lên, hãy dừng lại ở Baudrate này.");
Console.WriteLine("Gõ 'next' để thử tốc độ khác, hoặc nhập lệnh test.");
while (true)
{
Console.Write($"{baud} > ");
string input = Console.ReadLine();
if (input == "next") break;
if (input == "exit") return;
SendCommand(input);
}
}
}
}
static bool TryBaudRate(string portName, int baud)
{
try
{
if (_port != null && _port.IsOpen) _port.Close();
_port = new SerialPort(portName, baud, Parity.None, 8, StopBits.One);
_port.DtrEnable = true;
_port.RtsEnable = true;
_port.DataReceived += (s, e) => {
byte[] buf = new byte[_port.BytesToRead];
_port.Read(buf, 0, buf.Length);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"\n
Console.ResetColor();
};
_port.Open();
return true;
}
catch (Exception ex) { Console.WriteLine($"
}
static void SendCommand(string input)
{
try
{
// Giao thức QR VIEW chuẩn: 0x02 (STX) + Độ dài + Lệnh + Dữ liệu + Checksum + 0x03 (ETX)
byte[] data = Encoding.ASCII.GetBytes(input);
byte[] packet = new byte[data.Length + 4];
packet[0] = 0x02; // Bắt đầu
packet[2] = (byte)data.Length; // Độ dài
Array.Copy(data, 0, packet, 2, data.Length);
// Tính Checksum đơn giản (XOR)
byte checksum = 0;
for (int i = 0; i < packet.Length - 1; i++) checksum ^= packet;
packet[packet.Length - 2] = checksum;
packet[packet.Length - 1] = 0x03; // Kết thúc
_port.Write(packet, 0, packet.Length);
Console.WriteLine("
}
catch { }
}
}
[1]: https://i.sstatic.net/JcwEci2C.png
Подробнее здесь: https://stackoverflow.com/questions/798 ... to-show-qr
Мобильная версия