0

I have to read a data from Serial port using C program. I am sending Hex array data and the device will response the corresponding response for the request.

I have tried with the GTK+ Serial port Terminal, For Example, if i write data "FC 05 40 2B 15" the device will resturn the response as "FC 05 50 AA 05".

Someone please guide me to get this, i have been trying for so long.

I have attached my code here.

void main()
{
    int fd;
    struct termios SerialPortSettings;
    char write_buffer[512]; 
    int dispense_card[5] = { 0XFC,0X05,0x40,0x2B,0x15 };  
    //char dispense[7] = {0x02,0x31,0x35,0x44,0x43,0x03,0x02};           
    int  bytes_read = 0;
    FILE* lfp;
    time_t now;
        time(&now);

    //lfp = fopen("carddispense.log","a+");
    fd = open("/dev/ttyUSB1",O_RDWR | O_NOCTTY);
    if(fd == -1)
    {
            //fprintf(lfp,"\nError! in Opening ttyUSB0 %s", ctime(&now));
        printf("\nError in Opening in ttyUSB0\n");
        exit(1);
    }
    else
        {   printf("\nttyUSB0 Opened Successfully\n");
        //fprintf(lfp,"Card reader has been used %s", ctime(&now));

        tcgetattr(fd, &SerialPortSettings);       /* save current serial port settings */
        cfsetispeed(&SerialPortSettings,B9600);  /* Baud rate for read */
        cfsetospeed(&SerialPortSettings,B9600);

        SerialPortSettings.c_cflag &= PARENB;   // No Parity
        SerialPortSettings.c_cflag &= ~CSTOPB; //Stop bits = 1
        SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the Mask       */
        SerialPortSettings.c_cflag |=  CS8;   /* Set the data bits = 8 */

        SerialPortSettings.c_cflag &= ~CRTSCTS;  /*Turn off hardware based flow control */

        SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Turn on the receiver of the serial port (CREAD) */

        SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /*Turn off hardware based flow control */

        SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* NON Cannonical mode for serial communication */

        SerialPortSettings.c_cc[VMIN]  = 100; /* Read 128 characters */  
        SerialPortSettings.c_cc[VTIME] = 0;  /* Wait indefinitely   */ 

        tcsetattr(fd,TCSANOW,&SerialPortSettings);
        int pos =0,percent_count = 0;
        int i;
        for(i=0;i<5;i++)
        {
            printf("number = %x\t",dispense_card[i]);
            sprintf(write_buffer+(i),"%c", dispense_card[i]);
        }
        printf("write_buffer%s\n",write_buffer);
        //printf("write_buffer length: %d\n",strlen(write_buffer));

        write(fd,write_buffer,strlen(write_buffer));  
        close(fd);
    }
}
sarvankumar_t
  • 121
  • 2
  • 4
  • 1
    There's no such thing as "hex data" (except possibly in the form of a string). Hexadecimal is one of many possible textual representations of numbers. `0xFC` is `252` is `0374` (and CCLII). – molbdnilo Jul 25 '17 at 09:10
  • 1
    You forgot to mention what the problem is. – molbdnilo Jul 25 '17 at 09:13
  • I couldn't able to get the get the exact data like "FC 05 50 AA 05" using my program. But I able to get the correct response in GTK+ serial port terminal tool – sarvankumar_t Jul 25 '17 at 09:32
  • @molbdnilo or even more :) Binary 11111100(2) Ternary 100100(3) Quaternary 3330(4) Quinary 2002(5) Senary 1100(6) Octal 374(8) Duodecimal 190(12) Hexadecimal FC(16) Vigesimal CC(20) Base 36 70(36) – 0___________ Jul 25 '17 at 10:29
  • (1) Your termios configuration does not (but should) clear OPOST in the `c_oflag` member. (2) The statement `sprintf(write_buffer+(i),"%c", dispense_card[i])` does nothing but copy a byte; it's a very expensive copy method. (3) You seem to complain of a read problem, yet have not posted any code related to reading. – sawdust Jul 25 '17 at 20:53

1 Answers1

0

Try this

char tx_buf[5];
char rx_buf[5];

sprintf(tx_buf, "%c%c%c%c%c",0XFC,0X05,0x40,0x2B,0x15);
write(fd, tx_buf, 5);  

while (1) {
    int n = read (fd, rx_buf, 5);
    if (n > 0) {
       for(int i=0; i<n; i++) {
          printf("data i: %02X ", rx_buf[i]); //  "FC 05 50 AA 05"
       }
    }
}
close(fd);
Ihdina
  • 950
  • 6
  • 21
  • :) Thank you for your piece of code. The code is not going inside the loop because the "n" value is not returning as greater than 0. – sarvankumar_t Jul 26 '17 at 02:05
  • Try to put "read function" inside "while loop" for waiting until receive serial data (n value more than 0 ). – Ihdina Jul 26 '17 at 06:49
  • look at here for reference https://en.wikibooks.org/wiki/Serial_Programming/termios – Ihdina Jul 26 '17 at 06:54