Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

serial port programming

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
plugwash
Posts: 2507
Joined: 2006-09-17 01:10
Contact:

serial port programming

#1 Post by plugwash »

I'm trying to write a program to talk to a serial port (eventually to talk to an embedded device but for now i'm using a null modem cable hooked up to a windows PC running putty). The program was initially written in pascal but I translated it to C both to check if the problem was a bug in the fpc run time library and to make it easier to get assistance with it.

The word test is sent sucessfully to the machine running putty but nothing is received. This issue happens with both the pascal and C versions of the code. Anyone familiar with termios able to tell me where i'm going wrong?

i'm pretty sure the hardware is ok because I have tested it using minicom.

pascal version

Code: Select all

program gateway;
uses
  baseunix,
  unix,
  unixutil,
  termio; // despite the name the fpc termio unit seems to be an interface to termios
var
  device: string;
  fd : longint;
  config : termios;
  count : longint;
  buf : array[0..1023] of byte;
begin
  device := paramstr(1);
  fd := fpopen(device,O_RDWR or O_NOCTTY);
  writeln(fd);
  if isatty(fd)=0 then begin
    writeln('not a tty');
    halt(1);
  end;
  if tcgetattr(fd,config) <0 then begin
    writeln('could not get termios attributes');
    halt(2);
  end;
  config.c_iflag := 0;
  config.c_oflag := 0;
  config.c_cflag := 0;
  config.c_lflag := 0;
  cfmakeraw(config);
  cfsetispeed(config,B19200);
  cfsetospeed(config,B19200);
  config.c_cc[VMIN]  := 1;
  config.c_cc[VTIME] := 0;
  if   tcsetattr(fd,TCSAFLUSH,config) <0 then begin
    writeln('could not set termios attributes');
    halt(3);
  end;
  fpwrite(fd,'test'#13#10,6);
  while true do begin
    writeln(fd);
    count := fpread(fd,buf,sizeof(buf));
    writeln(count);
    fpwrite(fd,buf,count);
//    writeln(count);
  end;
  fpclose(fd);
end.
C version

Code: Select all

#include <termios.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


int main(int argc, char *argv[])

{
  struct termios config;
  char * device;
  int fd;
  char buf[1024];
  int count;
  device = argv[1];
  fd = open(device,O_RDWR | O_NOCTTY);
  printf("%i\n",fd);
  if (!isatty(fd)) {
    printf("not a tty\n");
    return 1;
  }
  if (tcgetattr(fd,&config) <0) {
    printf("could not get termios attributes\n");
    return 2;
  }
  config.c_iflag = 0;
  config.c_oflag = 0;
  config.c_cflag = 0;
  config.c_lflag = 0;
  cfmakeraw(&config);
  cfsetispeed(&config,B19200);
  cfsetospeed(&config,B19200);
  config.c_cc[VMIN]  = 1;
  config.c_cc[VTIME] = 0;
  if (tcsetattr(fd,TCSAFLUSH,&config) <0) {
    printf("could not set termios attributes\n");
    return 3;
  }
  write(fd,"test\r\n",6);
  while (1) {
    printf("%i\n",fd);
    count = read(fd,&buf,sizeof(buf));
    printf("%i\n",count);
    write(fd,&buf,count);
//    writeln(count);
  }
  close(fd);
  return 0;
}

plugwash
Posts: 2507
Joined: 2006-09-17 01:10
Contact:

Re: serial port programming

#2 Post by plugwash »

Found the soloution, I needed to set config.c_cflag := CLOCAL or CREAD;

rohxcom
Posts: 1
Joined: 2015-08-07 17:49

Re: serial port programming

#3 Post by rohxcom »


BowCatShot
Posts: 959
Joined: 2006-07-15 12:08

Re: serial port programming

#4 Post by BowCatShot »

If you're into c++, I've found that qt5 has an excellent set of classes for doing serial port i/o.

Post Reply