Embedded UART RS485 source code works same as uart - JohnHau/mis GitHub Wiki
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<sys/types.h> #include<sys/stat.h> #include<unistd.h> #include<fcntl.h> #include<pthread.h> #include<termios.h> #include<semaphore.h> #include<mqueue.h>
int setopt(int fd) { struct termios newtios,oldtios;
if(tcgetattr(fd,&oldtios)!= 0)
{
perror("tcgetattr failed\n");
exit(EXIT_FAILURE);
}
bzero(&newtios,sizeof(newtios));
newtios.c_cflag |= CLOCAL | CREAD;
newtios.c_cflag &= ~CSIZE;
newtios.c_cflag |= CS8;
newtios.c_cflag &= ~PARENB;
newtios.c_cflag &= ~CSTOPB;
//newtios.c_cflag |= CRTSCTS;
//newtios.c_iflag = IGNPAR | ICRNL;
newtios.c_iflag = IGNPAR;
newtios.c_oflag = 0;
newtios.c_lflag = 0;
cfsetispeed(&newtios,B115200);
cfsetospeed(&newtios,B115200);
newtios.c_cc[VTIME] = 1;
newtios.c_cc[VMIN] = 1;
tcflush(fd,TCIFLUSH);
tcflush(fd,TCOFLUSH);
if(tcsetattr(fd,TCSANOW,&newtios) != 0)
{
perror("tcsetattr failed\n");
exit(EXIT_FAILURE);
}
return 0;
}
#define UART_NAME "/dev/ttyS7"
int FH_open_uart(const char* name) { int fd;
if((fd = open(name,O_RDWR)) <0)
//if((fd = open(name,O_RDONLY | O_NOCTTY)) <0)
{
perror("FH_open_uart failed\n");
exit(EXIT_FAILURE);
}
setopt(fd);
return fd;
}
//command byte //0x40 Read Device Status //function byte //0x15 Read device properties + configCRC
//msgtype device type address dflen checksum //0x00 0x11 0x06 0x03 0x40 0x00 0x15 0x91 static uint8_t Checksum; uint8_t PB_recv_buf[1024] = {0};
int32_t PB_uartSend(int fd,char* pBuf,int len) { uint8_t i = 0; for(i=0;i<len;i++) { Checksum += pBuf[i]; } if(i == len) { Checksum = ~Checksum + 1; pBuf[i] = Checksum & 0xff; }
//0x00 0x11 0x06 0x03 0x40 0x15 0x91
uint8_t test[] = {0x00,0x11,0x06,0x03,0x40,0x00,0x15,0x91};
//uint8_t test[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x06,0x03,0x40,0x00,0x15,0x92};
for(i=0;i<sizeof(test);i++)
{
printf("send is %2x\n",test[i]);
}
//if(write(fd,pBuf,len+1) != (len+1))
if(write(fd,test,sizeof(test)) != (sizeof(test)))
{
perror("PB_uartSend failed\n");
//exit(EXIT_FAILURE);
return -1;
}
// tcflush(fd,TCIOFLUSH);
return 0;
}
int32_t PB_uartSend_test(int fd) { uint8_t i = 0;
//0x00 0x11 0x06 0x03 0x40 0x15 0x91
uint8_t test[] = {0x00,0x11,0x06,0x03,0x40,0x00,0x15,0x91};
//uint8_t test[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x06,0x03,0x40,0x00,0x15,0x92};
for(i=0;i<sizeof(test);i++)
{
printf("send is %2x\n",test[i]);
}
//if(write(fd,pBuf,len+1) != (len+1))
//if(write(fd,test,sizeof(test)) != (sizeof(test)))
if(write(fd,test,8) != 8)
{
perror("PB_uartSend failed\n");
//exit(EXIT_FAILURE);
return -1;
}
// tcflush(fd,TCIOFLUSH);
return 0;
}
int32_t PB_uartRecv(int fd,char* pBuf,int len) { int i = 0;
i = read(fd,pBuf,128);
return i;
for(i=0;i<512;i++)
{
printf("reading\n");
//if(read(fd,pBuf+i,1) <=0)
if(read(fd,pBuf+i,1) !=1)
{
perror("reading error\n");
break;
}
}
return i;
}
uint8_t read_dev_sta__read_dev_property[] = {0x00,0x11,0x06,0x03,0x40,0x00,0x15};
uint8_t in_buffer[256] = {0};
int main(int argc,char* argv[]) { int fd;
fd = FH_open_uart(UART_NAME);
if(fd < 0)
{
perror("open uart failed\n");
exit(EXIT_FAILURE);
}
printf("fd is %d\n",fd);
#if 1 printf("reading\n"); int n = read(fd,in_buffer,5); printf("n is %d\n",n); printf("in_buffer is %s\n",in_buffer);
printf("writing\n");
PB_uartSend_test(fd);
close(fd);
return 0;
#endif
#if 0 printf("reading\n"); int n = read(fd,in_buffer,5); printf("n is %d\n",n); printf("in_buffer is %s\n",in_buffer);
write(fd,in_buffer,n);
#endif
#if 0 //uint8_t tt[] = {0x00,0x11,0x06,0x03,0x40,0x00,0x15}; uint8_t tt[] = {0x00,0x10,0x06,0x03,0x40,0x00,0x15}; uint8_t ck = PB_cal_checksum(tt,sizeof(tt)); printf("ck is %02x\n",ck);
return 0;
#endif
close(fd);
return 0;
}