|
最近要写一个与智能仪器通信的程序,智能仪器连串口,发特定的字符串到串口,将从串口返回一定的字符串!在WIN下,只要设好波特率9600,数据位7,停止位1,偶校验就可以马上通信,直接SEND后,不停的READ就能读到返回的数据.可在LINUX下,我将经典的代码改了下,如果是两台计算机串口直连,这个程序可以成功收发数据!但连上仪器就是收不到数据,请大侠们指点下吧!
c_cc[VTIME] = 0
c_cc[VMIN] = 5这两个参数我也试了,都没有用!我的代码如下!
打开串口的代码:
var
ios: termios;
aa: integer;
begin
fd:= LibC.Open (port,fmOpenReadWrite or o_nonblock);
tcgetattr (fd,ios);
cfmakeraw (ios);
cfsetospeed (ios, b9600);
cfsetispeed (ios,b9600);
ios.c_cflag := ios.c_cflag and (not PARENB);
ios.c_cflag := ios.c_cflag or (not PARODD);
ios.c_cflag := ios.c_cflag and (not CSTOPB);
ios.c_cflag := ios.c_cflag and (not CSIZE);
ios.c_cflag := ios.c_cflag or (not CS7);
ios.c_lflag := ios.c_lflag and (not (ICANON or ECHO or ECHOE or ISIG));
ios.c_oflag :=ios.c_oflag and ( not OPOST);
ios.c_oflag := 0 ;
ios.c_lflag:=0;
tcflush(fd, TCIFLUSH);
tcsetattr (fd,tcsanow,ios);
end
写串口的代码:
var
d:string;
resultat: integer;
begin
d:= memo1.Text;
resultat:= FileWrite (fd, d[1], Length(d));
WriteLabel.Caption:= IntToStr (resultat)
end;
读串口的代码:
var
a:array [1..4096] of char;
s:string;
zaehler, index: integer;
begin
s:= '';
zaehler:= FileRead (fd, a, sizeof (a));
ReadLabel.Caption:= IntToStr (zaehler);
if zaehler > 0
then begin
for index:= 1 to zaehler do
s:= s + a [index];
memo2.Text:= s
end
end;
期盼大侠回复! |
|