如何从设备端的串口发送和接收?

时间:2016-11-19 14:05:44

标签: python c uart

我尝试在串口和设备之间进行通信;发送和接收数据:

 /*************Receiving Data***************/
           p_rx_buffer = &rx_buffer[0];
    int rx_length = dev_uart_ptr->uart_read(&rx_buffer, 255);
    //int rx_length = read(uart0_filestream, (void*)rx_buffer, 255);        //Filestream, buffer to store in, number of bytes to read (max)
    //Bytes received
    if (rx_length < 0)
            {
                xprintf("Error");
            }
            else if (rx_length == 0)
            {
                xprintf("No data");
            }
            else
            {
                //Bytes received
                rx_buffer[rx_length] = '\0';
                xprintf("%i bytes read : %s\n", rx_length, rx_buffer);
            }

       /*************End Receiving Data ***********/

        /*************Sending Data******************/
         p_tx_buffer = &tx_buffer[0];
        *p_tx_buffer++ = 'H';
        *p_tx_buffer++ = 'e';
        *p_tx_buffer++ = 'l';
        *p_tx_buffer++ = 'l';
        *p_tx_buffer++ = 'o';
         dev_uart_ptr->uart_write(&tx_buffer[0], 255);

这是我在python中的文件:

import serial
import time

ser = serial.Serial(
port='COM4',\
baudrate=230400,\
parity=serial.PARITY_NONE,\
stopbits=serial.STOPBITS_ONE,\
bytesize=serial.EIGHTBITS,\
timeout=0)

print(ser.name)         # check which port was really used

ser.write('hello')     # write a string
str=ser.readline()
while True:
  print(ser.read(30).decode('latin9').encode('utf8'))
ser.close()             # close ports

这就是我的结果,首先,我遇到了编码问题:

 e2 
                „  
  Ô       À  ô  (
  t      d>  d>  d>  d>  d>
 d>  d>  d>  d>  d>  d>  d>  d
>  d>  d>  È?  È?  È?  È?  È?
  È?  È?  È?  È?  È?  È
?  È?  È?  È?  È?  È?  È? test
_Trig
AES2 irq test
Hello

其次,接收数据根本不起作用,它向我显示msg“错误”。

如果你能帮助我,我将非常感激。

0 个答案:

没有答案