发表于:2004/6/10 12:33:00
#0楼
我用Linux(Red Hat)编了下面的程序,用于FX-0N的编程口通讯(通过PLC的编程口向PLC发送STX110F6043412CDABETX49 和 STX1011301FFETXE6 两条字符串)。程序运行结果,屏幕显示正常,PLC没收到数据。
用同一程序向另一台PC机(超级终端)发送两条与上面完全相同的字符串,超级终端接收正常。
用VB编的程序向PLC发送两条与上面完全相同的字符串,PLC接收正常,说明连线应该没问题。
不知何故,还望各位指点。在此先谢过。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
main()
{
int bSend;
int cSend;
int fd;
char strC[19];
char strB[13];
strC[0] = '\x02';
strC[1] = '\x31';
strC[2] = '\x31';
strC[3] = '\x30';
strC[4] = '\x46';
strC[5] = '\x36';
strC[6] = '\x30';
strC[7] = '\x34';
strC[8] = '\x33';
strC[9] = '\x34';
strC[10] = '\x31';
strC[11] = '\x32';
strC[12] = '\x43';
strC[13] = '\x44';
strC[14] = '\x41';
strC[15] = '\x42';
strC[16] = '\x03';
strC[17] = '\x34';
strC[18] = '\x39';
strC[19] = '\0';
strB[0] = '\x02';
strB[1] = '\x31';
strB[2] = '\x30';
strB[3] = '\x31';
strB[4] = '\x31';
strB[5] = '\x33';
strB[6] = '\x30';
strB[7] = '\x31';
strB[8] = '\x46';
strB[9] = '\x46';
strB[10] = '\x03';
strB[11] = '\x45';
strB[12] = '\x36';
strB[13] = '\0';
fd =open("/dev/ttyS0",O_RDWR|O_SYNC|O_NOCTTY);
if (fd==-1)
{
perror("OPEN ERROR1");
}
struct termios options;
tcgetattr(fd,&options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd, TCSAFLUSH, &options);
options.c_cflag &=~PARENB;
options.c_cflag &=~CSTOPB;
options.c_cflag &=~CSIZE;
options.c_cflag |= CS7;
options.c_oflag |= OPOST;
bSend = write(fd,strC,sizeof(strC));
if(bSend == -1)
{
printf("Write Error!\n");
}
else
{
printf("Write OK!\n");
}
printf("%d\n",bSend);
puts(strC);
close(fd);
sleep(1);
fd =open("/dev/ttyS0",O_RDWR|O_SYNC|O_NOCTTY);
if (-1==fd)
{
perror("OPEN ERROR2");
}
cSend = write(fd,strB,sizeof(strB));
printf("%d\n",cSend);
puts(strB);
close(fd);
}
用同一程序向另一台PC机(超级终端)发送两条与上面完全相同的字符串,超级终端接收正常。
用VB编的程序向PLC发送两条与上面完全相同的字符串,PLC接收正常,说明连线应该没问题。
不知何故,还望各位指点。在此先谢过。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <string.h>
main()
{
int bSend;
int cSend;
int fd;
char strC[19];
char strB[13];
strC[0] = '\x02';
strC[1] = '\x31';
strC[2] = '\x31';
strC[3] = '\x30';
strC[4] = '\x46';
strC[5] = '\x36';
strC[6] = '\x30';
strC[7] = '\x34';
strC[8] = '\x33';
strC[9] = '\x34';
strC[10] = '\x31';
strC[11] = '\x32';
strC[12] = '\x43';
strC[13] = '\x44';
strC[14] = '\x41';
strC[15] = '\x42';
strC[16] = '\x03';
strC[17] = '\x34';
strC[18] = '\x39';
strC[19] = '\0';
strB[0] = '\x02';
strB[1] = '\x31';
strB[2] = '\x30';
strB[3] = '\x31';
strB[4] = '\x31';
strB[5] = '\x33';
strB[6] = '\x30';
strB[7] = '\x31';
strB[8] = '\x46';
strB[9] = '\x46';
strB[10] = '\x03';
strB[11] = '\x45';
strB[12] = '\x36';
strB[13] = '\0';
fd =open("/dev/ttyS0",O_RDWR|O_SYNC|O_NOCTTY);
if (fd==-1)
{
perror("OPEN ERROR1");
}
struct termios options;
tcgetattr(fd,&options);
cfsetispeed(&options,B9600);
cfsetospeed(&options,B9600);
options.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd, TCSAFLUSH, &options);
options.c_cflag &=~PARENB;
options.c_cflag &=~CSTOPB;
options.c_cflag &=~CSIZE;
options.c_cflag |= CS7;
options.c_oflag |= OPOST;
bSend = write(fd,strC,sizeof(strC));
if(bSend == -1)
{
printf("Write Error!\n");
}
else
{
printf("Write OK!\n");
}
printf("%d\n",bSend);
puts(strC);
close(fd);
sleep(1);
fd =open("/dev/ttyS0",O_RDWR|O_SYNC|O_NOCTTY);
if (-1==fd)
{
perror("OPEN ERROR2");
}
cSend = write(fd,strB,sizeof(strB));
printf("%d\n",cSend);
puts(strB);
close(fd);
}