大家快来看!UNIX接受LAN网报文程序!求报文!

hero249 2004-01-13 12:47:40

/* **************************************************************************** */
/* */
/* Module: read_h1.c */
/* */
/* Description: - read the new date and/or time from the global clock. */
/* */
/* Author: Peter Vorwerk */
/* */
/* Version: @(#)read_h1.c @(#) */
/* */
/* Copyright Siemens AG (1993) All Rights Reserved. Confidental. */
/* */
/* **************************************************************************** */

/* Includes */


#include <stdio.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/timeb.h>

#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>

#include <netosi/osi.h>
#include <xti.h>

#include "mmi.h"

#define POSITION_LANINFO 63
#define LAN_UNKNOWN 0x0000
#define LAN1_IDENTIFIER 0xFFFF
#define RETURN_LAN_IDENT(a, b, c) (((a + 1) < b) ? (((c[a] & 0xFF) << 8) | (c[a+1] & 0xFF)) : LAN1_IDENTIFIER)

extern void update_time(int, int, int, int, int, int, int, int );


static char sccs_id[] = "@(#)read_h1.c @(#)";
static char environment[256];

/* ************************************************************************** */
/* */
/* Funktionsname : main */
/* */
/* Letzte Aenderung : 15.01.93 */
/* */
/* Eingang : int argc */
/* char **argv */
/* */
/* Returnwert : int */
/* */
/* Kurzbeschreibung : Lesen der Zeit vom Sinec H1 Uhrzeitsender */
/* */
/* ************************************************************************** */


main(int argc, char **argv)
{
static char * __FUNC__ = "MAIN";
int fd;
int rc;
int flags;
struct t_info info;
struct netbuf opt,udata;
struct t_unitdata unitdata;
struct t_bind req,ret;
struct sockaddr_osi *sosi;
int tid;
char tsaplen;
char *tsap;
int Update;
int LANId, PrevLANId = LAN_UNKNOWN;
int L1Active = FALSE;
extern char *tzname[];

/* reset the TZ variable to its default */
tzset();
sprintf( environment, "TZ=:%s", tzname[0]);
putenv( environment);

set_prgname(argv[0]);
set_standardprintclass(H1TIME);
mmi_tcleval("mmi");
mmi_setpidfile(argv[0]);
enrol(argv[0]); /* Register with the Koordinator */

/* Eintragen des TSAP's des Uhrzeitgebers */

tid = OSIPROTO_CLTS;
tsap = "S5_ZEIWA";
tsaplen = strlen(tsap);

/* Oeffnen der "Verbindung" zum H1-Bus */

if ((fd = t_open("/dev/streams/xtiso/clts",O_RDWR,&info))== -1) {
t_error("t_open");
fprintf(stderr,"Error occurred during the call t_open\n");
while(1) sleep(10);
}
/* Anfordern des Speichers fuer die OSI-Addresse */
/* */
/* Die Struktur ist VARIABEL!! */
/* */
/* D.h. der Kopf ist in sockaddr_osi festgelegt und dannach */
/* folgt ein variabler Teil, in dem die TSAP und/oder NSAP */
/* Informationen gespeichert sind. */

if ((sosi = calloc(info.addr,1)) == NULL) {
/* Kein Speicher frei! */

fprintf(stderr,"No free memory available\n");
}
/* Generieren der OSI-Addresse aus dem TSAP */

if ((rc = xti_osimakeaddr(sosi,tid,tsaplen,(unsigned char *)tsap,0,0,0)) != 0) {
errno = rc;
perror("xti_osimakeaddr");
fprintf(stderr,"Error occurred during the call xti_osimakeaddr\n");
}
req.addr.buf = (char*) sosi;
req.addr.len = sizeof(struct sockaddr_osi) + sosi->osi_length;
req.qlen = 0;
ret.addr.maxlen = info.addr;
ret.addr.buf = calloc(info.addr,1);
/* Einbinden der OSI-Addresse */
/* */
/* Nur von dieser Addresse sollen Telgramme empfangen werden */

if ((rc = t_bind(fd,&req,&ret)) == -1) {
t_error("t_bind");
t_close(fd);
fprintf(stderr,"Error occurred while binding the address\n");
}
/* Keine Protokoll Optionen noetig */

opt.buf = NULL;
opt.len = 0;
opt.maxlen = 0;
udata.len = 0;
udata.maxlen = info.tsdu;
/* Allocieren des Epfangspuffers */

if ((udata.buf = calloc(info.tsdu,1)) == NULL) {
/* Kein Speicher frei! */

fprintf(stderr,"No free memory available\n");
}
unitdata.addr = ret.addr;
unitdata.opt = opt;
unitdata.udata = udata;

/* Forever Loop */

while (1) {
/* Warten auf das Telgramm und dieses lesen */

if ((rc = t_rcvudata(fd,&unitdata,&flags)) == -1) {
/* Kein Telgramm empfangen "Verbindugsfehler" */

t_error("read u_data");
fprintf(stderr,"No telegram received\n");
}
else {
char *t = unitdata.udata.buf;

LANId = RETURN_LAN_IDENT( POSITION_LANINFO, unitdata.addr.len, unitdata.addr.buf);
if (LANId == PrevLANId) {
if (LANId == LAN1_IDENTIFIER) L1Active = TRUE;
else {
if (L1Active == TRUE) {
Update = FALSE;
L1Active = FALSE;
}
else Update = TRUE;
}
}
else {
if (LANId == LAN1_IDENTIFIER) {
L1Active = TRUE;
Update = TRUE;
}
else {
if (PrevLANId == LAN_UNKNOWN) Update = TRUE;
else Update = FALSE;
}
PrevLANId = LANId;
}
if (Update == FALSE) continue;

if (t[0] == 'U' && t[1] == 'S') {
/* Auswerten des Telegrammes */
/* */
/* Umwandeln der Zeichen in Zahlen */

#define s(i) (((t[i] & 0xf0) >> 4) * 10 + (t[i] & 0xf))
update_time(s(13),s(14),s(15),s(11),s(10),s(9),(int)t[6], LANId);
}
else {
/* Ungueltiges Telegramm */

fprintf(stderr,"Invalid telegram received\n");
}
}
}
}
谁解析出报文格式,百分相送!
...全文
51 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hero249 2004-01-13
  • 打赏
  • 举报
回复
西门子的代码啊。我已经分析出一部分了
报头
U S 00 00 00 W 00 00 00 秒 分 时 日 月 年
hero249 2004-01-13
  • 打赏
  • 举报
回复
局域网,双网
paopaoer 2004-01-13
  • 打赏
  • 举报
回复
这是什么网?
令牌网的吗?

23,117

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧