Hx_drv_uart_getchar() not working properly?

Hi, i am sending a string from my python script to the himax. however, the classification result was incorrect. the code below is what i wrote to receive characters from my python script

while(hx_drv_uart_getchar(&get_ch) == HX_DRV_LIB_PASS){
v.push_back(get_ch);
}

my python script uses ser.write(data.encode()) to send data to the himax. do i have to decode it in my main.cc? i just cant seem to find out what’s the issue :frowning:

Hi @weijunawj,

If I understand correctly, you’re trying to replace the static features array buffer in main.cc and receive the features through the serial (from Python). This is not yet possible with our firmware, but it’s something on our backlog.

Roughly speaking, you’ll need to:

  • (in Python) convert the float array copied from Studio (Live Classification page) into a byte array and send via serial
  • (in Himax) receive the binary array and convert back into floats, create into a signal and pass it to run_classifier

Of course to make it more general, you may also need to send the length of the array to the Himax, a mini protocol so you can parse the incoming stream correctly.

Can you share how you’re creating the features array in python? Can you verify what you’ve sent is what you’ve received on Himax?

Hi rjames! this is how i’m creating the features array:

import serial
import time

ser = serial.Serial(“COM5”, 115200)

buffer = [1.8435, -0.6177, 1.3378, 0.7452, 1.4521, -0.1024, 0.0012, 0.6913, -1.6179, 3.6703, 1.6897, -4.1073, 6.1298, -2.5109, 2.1009, 8.4455, -2.7797, -0.3531, 9.5193, -1.9878, 0.3561, 7.9398, -0.0886, 0.2664, 5.1008, -1.0325, -0.3938, 1.5868, -1.0157, -1.2641, 0.4094, -1.0032, 1.1420, 0.4549, -0.7937, 0.0132, 2.7683, -1.8208, -4.6136, 5.3343, -2.2051, -2.1907, 7.0563, -0.9894, -0.4064, 8.2690, -2.6653, -0.2376, 7.2682, -2.1171, -0.5028, 5.5414, -1.3749, 0.0449, 2.7432, -0.9116, -0.4256, 1.5275, 0.3208, 0.1856, 0.4687, 0.5692, -0.7111, 1.2360, 0.6907, -1.3593, 5.9526, -1.2276, -0.1760, 5.6419, -4.4083, -1.6382, 9.2817, -2.5929, -0.0856, 8.7712, -0.9170, 0.1472, 6.4141, -0.9433, -0.2999, 4.0462, -0.7045, -1.0451, 1.2779, -0.6051, -0.8308, -0.0760, 0.1939, 0.1538, 0.6045, -0.7853, -0.4854, 2.3463, -2.3302, -3.9037, 6.1178, -0.1089, -0.6668, 8.3450, -1.3096, -0.2753, 8.8597, -2.8533, -0.1844, 7.5352, -3.0849, -0.2005, 6.5218, -1.6149, 0.1401, 3.1801, -1.5814, 1.6466, 0.9152, 0.5692, -0.1419, 0.0587, 0.8194, -0.7691, 0.8966, 1.8352, -2.0309, 5.0607, -3.2555, -0.1778, 5.3828, -4.3604, -0.5944, 9.4296, -3.2938, 0.6644, 8.9376, -1.1504, 0.1024, 7.1778, -0.1754, 0.4758, 4.1108, -0.8428, -0.6608, 1.0534, -0.6626, -1.8920, 0.4154, 0.0922, -1.8094, 0.3693, 0.0036, 0.0497]

while True:

buffer = [str(i) for i in buffer]
buf = ''
for i in buffer:
    buf += i+','
buf = buf[:-1]
buf = buf + '!'

msg = ser.read(1)
receivedMsg = msg.decode('utf-8')
print(receivedMsg)
while receivedMsg != '~':
    msg = ser.read(1)
    receivedMsg = msg.decode('utf-8')

ser.write(buf.encode())

msg = ser.read(100)
receivedMsg = msg.decode('utf-8')
print(receivedMsg)

msg = ser.read(2000)
receivedMsg = msg.decode('utf-8')
print(receivedMsg)

as for the code for the himax to get the characters 1 by 1 and convert the char to float, this is what i wrote:

    hx_drv_uart_print("~");		
	
	while(hx_drv_uart_getchar(&get_ch) == HX_DRV_LIB_PASS){
		if (get_ch == '!'){
			break;
		}
		else{
			v.push_back(get_ch);
			hx_drv_led_on(HX_DRV_LED_RED);
		}
		
	}
	
	char* a = &v[0];
	
	int k=0, index=0;
	float val = 0;
	while(a[k] != NULL){
		if(a[k] == '-'){
			
			if(a[k+2] == '.'){      //if <10
				val = (float)(a[k+1]-'0') + 0.1*(float)(a[k+3]-'0')+ 0.01*(float)(a[k+4]-'0') + 0.001*(float)(a[k+5]-'0')+0.0001*(float)(a[k+6]-'0');
				val *= -1;
				features[index] = val;
				k+=8;
			}
			else {      //if >=10
				val =  10*(float)(a[k+1]-'0') + (float)(a[k+2]-'0')+ 0.1*(float)(a[k+4]-'0') + 0.01*(float)(a[k+5]-'0')+0.001*(float)(a[k+6]-'0')+0.0001*(float)(a[k+7]-'0'); 
				val *= -1;
				features[index] = val;
				k+=9;
			}
			
		}
		
		else{   //if not -ve
			if(a[k+1] == '.'){      //if <10
				val = (float)(a[k]-'0') + 0.1*(float)(a[k+2]-'0')+ 0.01*(float)(a[k+3]-'0') + 0.001*(float)(a[k+4]-'0')+0.0001*(float)(a[k+5]-'0');
				features[index] = val;
				k+=7;
			}
			else {      //if >=10
				val =  10*(float)(a[k]-'0') + (float)(a[k+1]-'0')+ 0.1*(float)(a[k+3]-'0') + 0.01*(float)(a[k+4]-'0')+0.001*(float)(a[k+5]-'0')+0.0001*(float)(a[k+6]-'0'); 
				features[index] = val;
				k+=8;
			}
		}  
		index++;
	}

i managed to get the first few characters right (only first 2 elements) but after that everything is a mess…

Hi,

Can you try just printing what the himax receives on the uart as-is:

                hx_drv_uart_print("%c", get_ch);

And verifiy it receives everything you sent?

Hi! i got 1.8435,-0.6177,1 then it started printing the inference results
image

For testing purposes can you wrap it in a while(1) { } as so:

while(1) {
      while(hx_drv_uart_getchar(&get_ch) == HX_DRV_LIB_PASS){
          hx_drv_uart_print("%c", get_ch);
      }
}

This way you can print everything that the himax receives. In your previous attempt it appears hx_drv_uart_get_char() returned !HX_DRV_LIB_PASS and exited your loop.

Once you’ve verified that everything you’ve sent is received by the himax. Then you can work your way up to building your parser.

Hi rjames! the code you provided doesnt print anything to the serial port :confused:

The code above should echo/print everything it receives from UART. Did you send your features from your host?

My snippet you should place after the the uart has been initalized as so:

int main(void)
{
    hx_drv_uart_initial(UART_BR_115200);
    hx_drv_tick_start();

    ei_printf("Edge Impulse standalone inferencing Himax WE-I Plus EVB\n");

    uint8_t get_ch;
    while (1) {
      while(hx_drv_uart_getchar(&get_ch) == HX_DRV_LIB_PASS){
          hx_drv_uart_print("%c", get_ch);
       }
     }
}

I hope that helps.