I'm trying to connect via RS232 connection.
Communication parameters:
*Transmission rate: 1200 baud
*Character coding: 8-bit ASCII
*Parity: None
*Stop bits: 1
Commands are composed of two byte codes with the following format
Transmit format
CODE + "FFh"
Hex code
Receive format
CODE + "FFh"
Hex code
I tried various initializations but I still fail to read anything from the port the following code is one of them:
//RS232test.c
//Transmission rate: 1200 Baud
//8 bit, no parity 1 stop bits.
//Transmit format: CODE + "FFh"
//Receive format: CODE + "FFh"
//Last edited: 23/08/2014
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#define BAUDRATE B1200
#define MULTI "/dev/ttyS0"
//#define MULTI "/dev/ttyUSB0"
int open_port(struct termios *,struct termios *);
int setDtrRts(int, int);
void close_port(int, struct termios *);
int open_port(struct termios *tty, struct termios *tty_old)
//This opens the tty port for linux saves old port setting
//and saves the new ones
{
int fd; //file descriptor
fd = open(MULTI, O_RDWR | O_NOCTTY);
if (fd < 0) {
perror(MULTI);
printf("failed to open port\n");
exit(-1);
}
//get previous port settings so it can be restored on exit
tcgetattr(fd,tty_old);
//get port settings so they can be set
tcgetattr(fd,tty);
//Set baud rates to 1200
cfsetispeed(tty, BAUDRATE);
cfsetospeed(tty, BAUDRATE);
//ICANON -choosing canonical input.
tty->c_lflag |= (ICANON);
//tty->c_lflag &= ~(ISIG);
//unselecting echo
tty->c_lflag &= ~(ECHO | ECHOE);
//CLOCAL - setting local mode, CREAD - enabling receiver
tty->c_cflag |= (CLOCAL | CREAD);
//close doesn't change signals
tty->c_cflag &= ~HUPCL;
//8N1 no parity 1 stop bit
tty->c_cflag |= CS8;
//tty->c_cflag &= ~(PARENB | CSTOPB | CSIZE);
tty->c_cflag &= ~(PARENB | PARODD /**/| CSIZE);
tty->c_cflag &= ~CSTOPB;
//Raw output mode
tty->c_oflag &= ~OPOST;
//Enable hardware handshaking
//tty->c_cflag &= ~CRTSCTS;
//tty->c_iflag &= ~(IXON | IXOFF | IXANY); //*
//Flushing communication buffer and changing port setting
//tcflush(fd, TCIFLUSH);
//tcsetattr(fd, TCSANOW, tty);
tcsetattr(fd, TCSAFLUSH, tty);
setDtrRts(fd,1);
return fd;
}
int setDtrRts(int fd, int set)
//sets or clears the dtr and rts
//the needs both set during operation
//otherwise it switches off after approx. 20 seconds
{
int setbits;
setbits |= (TIOCM_DTR | TIOCM_RTS);
if(set)
return(ioctl(fd, TIOCMBIC, &setbits));
else
return(ioctl(fd, TIOCMBIS, &setbits));
}
void close_port(int fd, struct termios *tty_old)
//closing port
{
//reset to old options
//tcsetattrb(fd, TCSANOW, tty_old);
setDtrRts(fd,0);
close(fd);
}
int main(void)
{
int fd=0; //system file number
int buff_size; //no of characters in buffer
int bytes; //no of bytes in buffer
int ctr=0; //general counter
char in_buffer[] = "F3\xFF";
char out_buffer[255]; //serial character buffer
//new port setting and a structure to keep the old ones
struct termios tty,tty_old;
//checking if root
if (getuid()){
printf("You must be root to use this program.\n");
exit(-4);
}
printf("fd = %d\n",fd);
//opening port for reading and writing
fd = open_port(&tty,&tty_old);
printf("fd = %d\n",fd);
//flushing
tcflush(fd,TCIOFLUSH);
//sending command to serial port
//strcpy(in_buffer, "F3\xFF"); //placing a command in the buffer
printf("%s",in_buffer);
if((buff_size = write(fd, in_buffer, strlen(in_buffer))) < 0){
printf("Error while sending message\nBuffer contents:\t%s\n",in_buffer);
return -2;
}
usleep(50000); //delay for 50ms
out_buffer[0] = '\0';
ioctl(fd,FIONREAD,&bytes);
printf("\nThere are %d bytes in the buffer\n",bytes);
if(bytes > 0) {
//reading response from serial port
if((buff_size = read(fd, out_buffer,sizeof(out_buffer))) < 0){
printf("Error while reading message\n");
return -3;
}
//printing the decimal ASCII values of the response
printf("Multimeter response:\t");
while(out_buffer[ctr] != '\0')
{
printf("%i ",out_buffer[ctr]);
ctr++;
}
printf("\n%s\n",out_buffer);
} else printf("Buffer Empty\n");
//wrap things up
close_port(fd, &tty_old);
exit(0);
}
The programs output is as follows:
fd = 0
fd = 3
F3�h
There are 0 bytes in the buffer
Buffer Empty
Toyed around with several suggestions in previous posts but did not succeed.