Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <termios.h>
- #include <unistd.h>
- #include <stdint.h>
- static int fd;
- #define MAX_STR_LEN 128
- #define MODE_8E1 /**< enables the uart to be in 8E1 mode */
- #define DDDEBUG
- static void close_fd(void);
- static int configure_uart(int rawbaud);
- static int register_read_request(const uint16_t chip, const uint16_t address, const uint16_t naddr);
- static void close_fd(void)
- {
- int ret = close(fd);
- if (ret)
- printf("Could not close serial device... %s\n\r", strerror(errno));
- }
- int init_uart(char * port)
- {
- /* open and configure serial port */
- /* O_RDWR - Read/Write access to serial port */
- /* O_NOCTTY - No terminal will control the process */
- /* Open in blocking mode,read will wait */
- fd = open(port, O_RDWR | O_NOCTTY);
- if (fd < 0){
- printf("Could not open port %s... %s\n\r", port, strerror(errno));
- return -1;
- }
- int ret = configure_uart(B230400);
- if (ret)
- return -1;
- printf("Connected to %s\n",port);
- return 0;
- }
- static int configure_uart(int rawbaud)
- {
- int ret = 0;
- /* causes read to return 0 (non-blocking) */
- #if 0
- fcntl(fd, F_SETFL, FNDELAY);
- /* causes read to block */
- #endif
- fcntl(fd, F_SETFL, 0);
- struct termios attr;
- ret = tcgetattr(fd, &attr);
- if (ret < 0) {
- printf("from tcgetattr: %s\n", strerror(errno));
- return -1;
- }
- ret = cfsetispeed(&attr,rawbaud);
- if (ret){
- printf("from cfsetispeed: %s\n",strerror(errno));
- return -1;
- }
- ret = cfsetospeed(&attr,rawbaud);
- if (ret){
- printf("from cfsetospeed: %s\n",strerror(errno));
- return -1;
- }
- /* 8E1 mode */
- #if defined(MODE_8E1)
- #warning("MODE_8E1 ENABLED!");
- attr.c_cflag |= PARENB; /* enable parity */
- attr.c_cflag &= ~PARODD; /* even parity */
- attr.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
- attr.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */
- attr.c_cflag |= CS8; /* 8-bit characters */
- #elif defined(MODE_8N1)
- #warning("MODE_8N1 ENABLED!");
- attr.c_cflag &= ~PARENB; /* disable parity */
- attr.c_cflag &= ~CSTOPB; /* only need 1 stop bit */
- attr.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */
- attr.c_cflag |= CS8; /* 8-bit characters */
- #else
- printf("Must define a mode\n");
- return -1;
- #endif
- attr.c_cflag &= ~CRTSCTS; /* no hardware flowcontrol */
- attr.c_cflag |= (CLOCAL | CREAD); /* ignore modem controls */
- ret = tcsetattr(fd,TCSANOW,&attr);
- if(ret){
- printf("from tcsetattr: %s\n", strerror(errno));
- return -1;
- }
- // tcflush(fd,TCIOFLUSH);
- return 0;
- }
- static int register_read_request(const uint16_t chip, const uint16_t address, const uint16_t naddr)
- {
- uint16_t chipAddr = chip+address;
- char cmd[MAX_STR_LEN];
- memset(&cmd, MAX_STR_LEN, '\0');
- sprintf(cmd,"r %02x %02x%c",chipAddr,naddr,0xa);
- printf("strlen %d : %s\n",(int)strlen(cmd),cmd);
- #ifdef DDDEBUG
- int s = (int)strlen(cmd);
- int i;
- for(i = 0; i < s; i++){
- printf("tx cmd[%d] = %02x %c\n",i,cmd[i],cmd[i]);
- // ssize_t bytes_tx = write(fd, (char *)&cmd[i], 1);
- // printf("bytes : %d\n",(int)bytes_tx);
- // usleep(100);
- // tcflush(fd,TCIOFLUSH);
- // usleep(100);
- }
- #endif
- ssize_t bytes_tx = write(fd, (char *)cmd, strlen(cmd));
- printf("bytes_tx = %d\n",bytes_tx);
- printf("sizeof(\\n) : %d\n",sizeof('\n'));
- if (bytes_tx < 0){
- printf("%s\n",strerror(errno));
- return -1;
- }
- return 0;
- }
- int main()
- {
- init_uart("/dev/ttyUSB0");
- register_read_request(0x40, 0x0, 0x0);
- close_fd();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement