Verfasst am: 20.06.2011, 11:07
Titel: USB -> RS 232 Adapter in GUI implementieren
Hallo Forengemeinde,
ich hoffe mir kann jemand einen Tipp geben oder gar einen Lösungsansatz:
Ich möchte ein Motorsteuerboard über RS232 ansprechen, da mein Rechner nur noch USB hat, habe ich mir ein USB->RS232-Adabterkabel (mit Software) gekauft.
Da ich mit einem GUI arbeite, muss ich diesen Adapter doch sicher in meinem GUI bzw. in meinem m.file irgendwie aufrufen/ansprechen/....
Wenn ja, wo muss das hin (ganz am Anfang, bevor das GUI sichtbar wird?) und wie mache ich das?
Hoffe so etwas hat hier schonmal jemand gelöst!^^ mathworks sagt dazu relativ "wenig"!
Verfasst am: 20.06.2011, 18:45
Titel: eine Frage dazu
nutzt du Windoofs oder Linux?
oder Linux bei Linux weiß ich das du die Serielen Befehle einfach an die usb schnittstelle schicken kannst (/dev/usb)?... der Rest macht der Kernel und das Interface
anbei mal das ganze als cpp Klasse vll hilft dir das ja weiter ... vll auch noch mal die Quellen checken
http://dns.easysw.com/~mike/serial/serial.html#2_2
die zweite Seite gibt es leider nicht mehr...
Code:
#include <stdio.h> /* Standard input/output definitions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
/*
Creator: Martin 2009
sources: http://dns.easysw.com/~mike/serial/serial.html#2_2
http://www.captain.at
CBAUD Bit mask for baud rate
B0 0 baud (drop DTR)
B50 50 baud
B75 75 baud
B110 110 baud
B134 134.5 baud
B150 150 baud
B200 200 baud
B300 300 baud
B600 600 baud
B1200 1200 baud
B1800 1800 baud
B2400 2400 baud
B4800 4800 baud
B9600 9600 baud
B19200 19200 baud
B38400 38400 baud
B57600 57,600 baud
B76800 76,800 baud
B115200 115,200 baud
EXTA External rate clock
EXTB External rate clock
CSIZE Bit mask for data bits
CS5 5 data bits
CS6 6 data bits
CS7 7 data bits
CS8 8 data bits
CSTOPB 2 stop bits (1otherwise)
CREAD Enable receiver
PARENB Enable parity bit
PARODD Use odd parity instead of even
HUPCL Hangup (drop DTR) on last close
CLOCAL Local line - do not change "owner" of port
LOBLK Block job control output
CNEW_RTSCTS
CRTSCTS Enable hardware flow control (not supported on all platforms)*/
#define mountpoint "/dev/ttyUSB0" // look at usbport 0if serialport "/dev/ttyS0"
#define BAUDRATE B115200 //choose 115200 as BAUDRATE
class RS232
{
public:
RS232() {
}
int fd;
int open_port() {
//int fd; /* File descriptor for the port */
fd = open(mountpoint, O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1) //iftrue Could not open the port
{
perror("open_port: Unable to open port - ");
} else
fcntl(fd, F_SETFL, 0);
return(fd);
}
int init_port(int fd) { struct termios options;
tcgetattr(fd, &options); //get config
cfsetispeed(&options, BAUDRATE); //setinput Baudrate
cfsetospeed(&options, BAUDRATE); //set output Baudrate
options.c_cflag |= (CLOCAL | CREAD);//Enable the receiver and set local mode
options.c_cflag &= ~PARENB; //No parity (8N1)
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
int send_port(char *chars) {
int len = strlen(chars); //get number of elements
//printf("%i \n",len);
chars[len] = 0x0d; // stick a <CR> after the command
chars[len+1] = 0x00; // terminate the string properly
int n = write(fd, chars,strlen(chars));//send and get number of send chars
//printf("%i \n",n);
if(n < 0) //if n=-1 send error {
fputs("write failed!\n", stderr);
return0;
} return1;
}
int read_port (char *result) {
int iIn=0;
while(iIn<=0) {
fcntl(fd, F_SETFL, FNDELAY); //disable serial receive block
iIn = read(fd, result, 254); //read in and get number of element
result[iIn-1] = 0x00;
}
//if(iIn < 0)
//printf("%s \n",result);
{ if(errno == EAGAIN) {
//printf("SERIAL EAGAIN ERROR\n");
return0;
} else {
//printf("SERIAL read error %d %s\n", errno, strerror(errno));
return0;
} } return1;
}
Nutze WIN7.... und das schlimme ist auch noch 64 bit!!!
Jetzt hat mir mein Matlab (2008b) ausgespuckt, dass es 64 bit nicht mag und nur mit 32 bit arbeiten will.
Wenn ich meine serielle Schnittstelle öffnen will....
Aber danke schonmal für das Beispiel, muss ich mich reinlesen und schauen wie ich integrieren kann.
Hat noch jemand damit Erfahrung bzw. einen weiteren Tipp??
hat es auch nicht versuche es einfach mal weil der converter sollte das verstehen und das richtige darauß machen
Einstellungen und Berechtigungen
Du kannst Beiträge in dieses Forum schreiben. Du kannst auf Beiträge in diesem Forum antworten. Du kannst deine Beiträge in diesem Forum nicht bearbeiten. Du kannst deine Beiträge in diesem Forum nicht löschen. Du kannst an Umfragen in diesem Forum nicht mitmachen. Du kannst Dateien in diesem Forum posten Du kannst Dateien in diesem Forum herunterladen
MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.