我试着发送hex数据给ch552g,先发送字符串 552可以接收到,再发送64byte数据都是0
I try to send HEX data to ch552g, first send a char to 552, then send 64 byte ,but recived all 0
PC: python3 (pip install comports pyserial) 需要安装依赖库运行,修改com信息
python code
from serial import Serial
from serial.tools.list_ports import comports
from time import sleep
comx = "COM9"
class Programmer(Serial):
def __init__(self):
super().__init__(baudrate = 115200, timeout = 3, write_timeout = 2)
def connect(self,comport):
for p in comports():
if p.device == comport:
self.port = p.device
try:
self.open()
if self.is_open:
return True
except:
continue
return False
def sendcommand(self, cmd, chip='', startAddr=-1, endAddr=-1):
cmd += ' %6s' % chip
if startAddr >= 0:
cmd += ' %04x' % startAddr
if endAddr >= 0:
cmd += ' %04x' % endAddr
self.write((cmd + '\n').encode())
print("command:{}".format(cmd))
def getReady():
prom = Programmer()
if prom.connect(comx):
print("连接")
else:
print("未连接")
exit(0)
if prom.is_open:
prom.sendcommand('t')
sleep(0.5)
print(prom.readline())
def sendCommandA():
prom = Programmer()
if prom.connect(comx):
print("连接")
else:
print("未连接")
exit(0)
if prom.is_open:
prom.sendcommand('a')
sleep(1)
buffer_data = []
for i in range(64):
buffer_data.append(hex(i))
prom.write(i)
prom.flush()
print("send:{}".format(len(buffer_data)))
print(buffer_data)
line = prom.readline()
print("read:{}".format(len(line)))
chars = []
for i in line:
chars.append(hex(i))
print(chars)
if __name__ == '__main__':
getReady()
sendCommandA()
getReady()
arduino code
#define VERSION "1.0"
#define INFO "EEPROM_CH552"
#define READY "Ready"
// Buffers
#define BUFF_LEN 64
#define CMD_LEN 22
__xdata char pageBuffer[64]; // 写入芯片page缓存
__xdata char cmdBuffer[CMD_LEN]; // 串口cmd命令缓存
void setup() {
while (!USBSerial()) {
delay(50);
}
}
void readCommand() {
for(uint8_t i=0; i< CMD_LEN; i++) cmdBuffer[i] = 0; // clear command buffer
char c = 0xff; uint8_t idx = 0; // initialize variables
do {
if(USBSerial_available()) {
c = USBSerial_read();
cmdBuffer[idx++] = c;
}
} while (c != '\n' && idx < CMD_LEN);
cmdBuffer[idx - 1] = 0; // change last newline to '\0' termination
}
// read buff then write serial
void aa(){
for(uint8_t i=0; i< BUFF_LEN; i++) pageBuffer[i] = 0; // clear command buffer
char c = 0xff; uint8_t idx = 0; // initialize variables
do {
if(USBSerial_available()) {
c = USBSerial_read();
pageBuffer[idx++] = c;
}
} while (idx < BUFF_LEN);
for(idx=0; idx<BUFF_LEN; idx++){
USBSerial_write(pageBuffer[idx]);
USBSerial_flush();
}
USBSerial_println("ok");
USBSerial_flush();
}
// read buff then write serial
void bb(){
for(uint8_t i=0; i< BUFF_LEN; i++) pageBuffer[i] = 0; // clear command buffer
char c = 0xff; uint8_t idx = 0; // initialize variables
do {
if(USBSerial_available()) {
c = USBSerial_read();
pageBuffer[idx++] = c;
}
} while (idx < BUFF_LEN);
for(idx=0; idx<BUFF_LEN; idx++){
USBSerial_print(pageBuffer[idx]);
USBSerial_flush();
}
USBSerial_println("ok");
USBSerial_flush();
}
void loop() {
readCommand();
char cmd = cmdBuffer[0];
switch(cmd) {
case 'i': USBSerial_println(INFO);USBSerial_flush(); break;
case 'v': USBSerial_println(VERSION);USBSerial_flush(); break;
case 't': USBSerial_println(READY);USBSerial_flush(); break;
//test Serial read & write
case 'a': aa(); break;
case 'b': bb(); break;
default: break;
}
}
我试着发送hex数据给ch552g,先发送字符串 552可以接收到,再发送64byte数据都是0 I try to send HEX data to ch552g, first send a char to 552, then send 64 byte ,but recived all 0 PC: python3 (pip install comports pyserial) 需要安装依赖库运行,修改com信息
python code
arduino code
期望发送命令和64byte数据,收到64byte数据和 ok\r\n 实际收到是 64个0 然后是ok\r\n
run python code, send command a to ch552, send 64 byte from 0x0 to 0x3F, recive from USBSerial 64 byte 0x0 not 0x0-0x3F