我正在从微控制器(LPC1549 XpressoV2)串行读取ADC和DAC值。已经有可用c编写的可用固件,我正在使用Python为Linux创建一个接口。为了交流,我正在使用Pyserial库。
打开端口后,当我从主机(PC)将数据发送到设备(微控制器)时,它应该发送回相同的数据“ ??????????”。一旦完成,它期望数据的特殊序列“(?,00100000,4095)”调用特定函数。
问题: 打开端口后,我会收到我发送到设备的任何内容。它没有调用我要调用的特定函数并获得响应。
import serial
PC_HELLO = "??????????"
SINE_MODE = "(?,00100000,4095)"
SERIALPORT = "/dev/ttyACM0"
BAUDRATE = 115200
ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
ser.timeout = 2 #timeout block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 0 #timeout for write
print ('Starting Up Serial Monitor')
try:
ser.open()
except IOError as e:
print("error open serial port: " + str(e))
ser.close()
ser.open()
pass
print("port was already open, was closed and opened again!")
if ser.isOpen():
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput() #flush output buffer, aborting current output
ser.write(PC_HELLO.encode())
print("PC hello command is being written.. ")
while True:
try:
ser.write(SINE_MODE.encode('ascii'))
response = ser.readline()
print(response)
except KeyboardInterrupt as e:
print("closing port ... keyboard interupt initiated..")
ser.close()
print('serial port is closed ...')
except Exception as e:
print ("error communicating...: " + str(e))
else:
print("cannot open serial port ")
C代码如下。 //这是NXP的示例代码
while (1)
{
// Check if host has connected and opened the VCOM port
if ((vcom_connected() != 0) && (prompt == 0))
{
prompt = 1;
}
// If VCOM port is opened echo whatever we receive back to host
if (prompt)
{
rdCnt = vcom_bread(&g_rxBuff[0], 256); //i don't quite understand this line of code ;)
if (rdCnt)
{
// character received
uint8_t ch = g_rxBuff[0];
// command start
if (ch == '(')
{
wrCnt = 0;
wrStat = true;
}
// command transmitting
if (wrStat)
{
// copy
Data_string[wrCnt++] = ch;
// command end
if ((ch == ')') || (wrCnt > 17))
{
wrStat = false;
cmdStat = true;
}
}
// command received
if (cmdStat)
{
cmdStat = false;
// PC-to-uC string x17
// (?,11111111,4095)
vcom_master_out(&Data_string[0]);
输入(PC)-------------------------------输出(微控制器)
“ ???????” -“ ???????”
“(?,* MODE(例如正弦模式),* DAC_VALUE)”-“(S,DAC_VALUE,ADC_VALUE)”
“(?,* MODE(例如正弦模式),* DAC_VALUE)”-“(i,DAC_VALUE,ADC_VALUE)”
“(?,* MODE(例如正弦模式),* DAC_VALUE)”-“(n,DAC_VALUE,ADC_VALUE)”
“(?,* MODE(例如正弦模式),* DAC_VALUE)”-“(e,DAC_VALUE,ADC_VALUE)” .... ....