You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

70 lines
1.8 KiB

/*
* Created on: Aug 5, 2019
* Author: Cristian Fatu
* This project implements a basic UART communication over UART on
* Petalinux, unsing Uart Lite linux driver. The hardware platform implements an
* UART Lite IP core and the device tree (in pl.dtsi) sets the appropriate
* parameters. After booting linux, the "/dev/ttyUL1" must be present.
*/
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include "uart.h"
#define MAX_RECV_SIZE 235
void openuart(struct UartDevice *device, char *devname, int rate) {
device->name = devname;
device->rate = rate;
printf("UART open %s\n", device->name);
int rc = uartStart(device, 0);
if (rc) {
printf("uartStart %s fail\n", device->name);
exit(-1);
}
}
int main(int argc, char *argv[]) {
struct UartDevice dev = {0};
// struct UartDevice dev2 = {0};
if (argc != 2) {
printf("Usage:%s dev1\n", argv[0]);
// printf("Demo:%s /dev/ttyUSB0 /dev/ttyUSB1\n", argv[0]);
exit(-1);
}
openuart(&dev, argv[1], B9600);
// openuart(&dev, argv[1], B115200);
// openuart(&dev2, argv[2], B9600);
char buf[1024] = {0};
memset(buf, 0x55, 1024);
for (size_t i = 0;; i++) {
int ret1, ret2;
ret1 = uartReceive(&dev, buf, 2/*接收的字节数2-1*/); // send the received text over UART
if (ret1 != 0) {
// printf("receive %d %x characters %d\n", ret1, buf[0], i);
printf("%x\n", buf[0]);
if (buf[0] != 0x12 && buf[0] != 0x34 && buf[0] != 0x56 &&
buf[0] != 0x78 && buf[0] != 0x9a) {
printf("....................................................%x\n", buf[0]);
}
}
// ret2 = uartSend(&dev2, buf, 1); // send the received text over UART
// usleep(1*1000);
}
while (1) {
sleep(1);
}
return 0;
}