Socket an CAN-Bus aber wie?

Alles rund um die Programmierung mit Qt
Antworten
speedy
Beiträge: 52
Registriert: 23. Juli 2008 03:17

Socket an CAN-Bus aber wie?

Beitrag von speedy »

Hallo

ich komme nicht so wirklich weiter...
Ich würde gern ein Programm in QT haben was Daten an ein CAN-Bus sendet und empfangen kann.

Hat mich auch schon was mühe gekostet bis das in meinem Linux erstmal richtig läuft ...
nun ja
mit "ifconfig can0 up" ist der anschluss jetzt da.

ich hab dazu noch ein paar Can utilities
cansend
candump

diese arbeiten mit socket um Daten zu senden und zu empfangen.
Das ist aber nur in C.

der quellcode von cansend.c
/*
* $Id: cansend.c 753 2008-06-03 08:46:57Z hartkopp $
*/

/*
* cansend.c - simple command line tool to send CAN-frames via CAN_RAW sockets
*
* Copyright (c) 2002-2007 Volkswagen Group Electronic Research
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of Volkswagen nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* Alternatively, provided that this notice is retained in full, this
* software may be distributed under the terms of the GNU General
* Public License ("GPL") version 2, in which case the provisions of the
* GPL apply INSTEAD OF those given above.
*
* The provided data structures and external interfaces from this code
* are not restricted to be used by modules with a GPL compatible license.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* Send feedback to <socketcan-users@lists.berlios.de>
*
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <net/if.h>
#include <sys/ioctl.h>

#include <linux/can.h>
#include <linux/can/raw.h>

#include "lib.h"

int main(int argc, char **argv)
{
int s; /* can raw socket */
int nbytes;
struct sockaddr_can addr;
struct can_frame frame;
struct ifreq ifr;

/* check command line options */
if (argc != 3) {
fprintf(stderr, "Usage: %s <device> <can_frame>.\n", argv[0]);
return 1;
}

/* parse CAN frame */
if (parse_canframe(argv[2], &frame)){
fprintf(stderr, "\nWrong CAN-frame format!\n\n");
fprintf(stderr, "Try: <can_id>#{R|data}\n");
fprintf(stderr, "can_id can have 3 (SFF) or 8 (EFF) hex chars\n");
fprintf(stderr, "data has 0 to 8 hex-values that can (optionally)");
fprintf(stderr, " be seperated by '.'\n\n");
fprintf(stderr, "e.g. 5A1#11.2233.44556677.88 / 123#DEADBEEF / ");
fprintf(stderr, "5AA# /\n 1F334455#1122334455667788 / 123#R ");
fprintf(stderr, "for remote transmission request.\n\n");
return 1;
}

/* open socket */
if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
perror("socket");
return 1;
}

addr.can_family = AF_CAN;

strcpy(ifr.ifr_name, argv[1]);
if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
perror("SIOCGIFINDEX");
return 1;
}
addr.can_ifindex = ifr.ifr_ifindex;

/* disable default receive filter on this RAW socket */
/* This is obsolete as we do not read from the socket at all, but for */
/* this reason we can remove the receive list in the Kernel to save a */
/* little (really a very little!) CPU usage. */
setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);

if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
perror("bind");
return 1;
}

/* send frame */
if ((nbytes = write(s, &frame, sizeof(frame))) != sizeof(frame)) {
perror("write");
return 1;
}

//fprint_long_canframe(stdout, &frame, "\n", 0);

close(s);

return 0;
}
Jetzt die große frage ... wie kann ich das in QT realisieren?
Hab zwar schon gesehen das es in QT ein QTcpSocket gibt aber in dem Fall kann ich das nicht gebrauchen...

Meine ersten Versuche den Quellcode von oben etwas umzubauen sind (erstmal) gescheitert.

Muss auch zugeben das ich mit Sockets auch nicht so viel Erfahrung hab.

mfg
speedy
Beiträge: 52
Registriert: 23. Juli 2008 03:17

Beitrag von speedy »

hat da keiner eine idee?
Also ich hab jetzt was hinbekommen ... aber so wirklich toll ist das noch nicht.
Ist recht viel C Code drin.
Und momentan bekomme ich immer eine fehlermeldung beim beenden.
solarix
Beiträge: 1133
Registriert: 7. Juni 2007 19:25

Beitrag von solarix »

hat da keiner eine idee?
Zu was? Grundsaetzlich ist dieser C-Code ja auch in einem C++-Programm funktionstauglich. Ich selbst wuerde allerdings eine saubere Wrapper-Klasse rundherum bauen, so dass der Umgang genauso einfach wie QTcpSocket/QTcpServer waere.

Daher:
Meine ersten Versuche den Quellcode von oben etwas umzubauen sind (erstmal) gescheitert.
...
Und momentan bekomme ich immer eine fehlermeldung beim beenden.
Wenn du von uns irgendwelche Hilfe erwartest, sollten wir auch wissen, WAS nicht funktioniert (Code, Fehlermeldungen, Debugger-Infos (Variabelninhalte), usw.) .
speedy
Beiträge: 52
Registriert: 23. Juli 2008 03:17

Beitrag von speedy »

ich glaub ich hab das Problem erstmal gelößt.
Die Fehlermeldung bekomme ich jetzt nicht mehr.
Da stand was von irgendwelchen xxx.h die aus dem /usr/include gekommen sind.
Und danach ein Auszug von irgendwelchen Bereichen meines Memorys.

Hatte vielleicht gedacht das es sowas wie ein allgemeineren socket in Qt gibt.
Aber sowas wie QSocket.

Hab da jetzt auch eine Klasse drumherum gebaut.
Ist zwar nicht bediengleich wie QTcpSocket ... aber für mich langt das jetzt was sie kann.
tomy
Beiträge: 1
Registriert: 31. August 2011 16:46

Re: Socket an CAN-Bus aber wie?

Beitrag von tomy »

Du schreibst das du dir ein Qt-Wrapper-Klasse für SocketCAN geschrieben hast. Ich hab momentan eher eine provisorische Lösung gefunden die auf QAbstractSocket und QSocketNotifier besteht.
Vllt kann man sich zu dem Thema austauschen?
Antworten