Ports to 485 with converter USB, please help.

Post Reply
User avatar
Busmatic_wpb
Posts: 162
Joined: Wed Feb 22, 2017 2:19 am

Ports to 485 with converter USB, please help.

Post by Busmatic_wpb »

Hellow My friends

Some of you have had experience with communication with USB ports to 485 with converter. There will be state codes in ports 485 ??

. Please someone to give me a hand.

I have a counter of people, communicate by 485 and I put a converter to USB, I use a third-party program .. to communicate and work very well

See .. http://www.busmaticcr.com/pantalla.bmp

As you can see, a command is sent and the return is fine.

Now My Program..
I perceive when I use my program with the same setting I do not receive anything .. although I see the TXD converter light that if I send data..

This program was taken from some people who post it


#Include "FiveWin.Ch"

Function Comunica()
Local mport := 2 // Puerto COM
Local PortId, cBloque
PortId := abrirport(mport)
cbloque = leer_bloque(PortId,31)
CloseComm(PortId)
RETURN cBloque

STATIC FUNCTION abrirport(n)
LOCAL IdPort, cDcb, nError
IdPort := OpenComm("COM"+STR(n,1),1024,256)
IF IdPort <= 0
nError = GetCommError( IdPort)
MsgInfo( "Error al abrir: " + Str( nError ) )
ELSE
MsgRun("Puerto abierto como " + STR(IdPort))
ENDIF
IF ! BuildCommDcb("COM"+STR(n,1)+":9600,n,8,1" , @cDcb)
nError = GetCommError( IdPort)
MsgInfo( "Error al Configurar: " + Str( nError ) )
RETURN 0
ELSE
MsgRun("Puerto Configurado")
ENDIF
IF ! SetCommState( IdPort, cDcb )
nError = GetCommError( IdPort)
MsgInfo( "Error al setear: " + Str( nError ) )
RETURN 0
ELSE
MsgRun("Puerto Seteado")
ENDIF
RETURN IdPort

STATIC FUNCTION leer_bloque(port,n)
LOCAL bloque:= SPACE(n),bcc1,nBytes,i:=1
DO WHILE .t.
nBytes := ReadComm( port,@bloque)
i++
IF EMPTY(bloque)
MsgWait("Esperando ENQ","Espere",3)
mandar_enq(port)
LOOP
ENDIF
IF i > 20 // .or. bloque <> SPACE(n)
EXIT
ENDIF
ENDDO
RETURN bloque

STATIC FUNCTION mandar_enq(port)
LOCAL mcont,retorno,nBytes
LOCAL cString:="02 30 30 30 31 31 33 30 30 31 34 03", nByteslargo:= Len( cString )

retorno = .t.


FOR mcont := 1 TO 10

IF ( nBytes := WriteComm( port,cString,nByteslargo)) <= 0

MsgAlert("Mando mal ENQ")
retorno := .f.
ELSE
retorno := .t.
exit
ENDIF
MsgWait("Enviando datos...","Aguarde",1)
NEXT
RETURN retorno
Regards.
S.I.T.U.
Sistemas Inteligentes de transporte urbano
http://www.situcr.com
_@Situcr.com
Desarrollos BA4/B4j androide
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Ports to 485 with converter USB, please help.

Post by Antonio Linares »

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Busmatic_wpb
Posts: 162
Joined: Wed Feb 22, 2017 2:19 am

Re: Ports to 485 with converter USB, please help.

Post by Busmatic_wpb »

Thanks Antonio.

Thanks Antonio, either.

I have noticed that the third party application sends a length of RX is 44 and the length that I sent is 36 from where the difference comes out.

http://www.busmaticcr.com/pantalla.bmp

look at the RX below sending 44 byts ?? It tells me that 8 byts more ?? where this came from ??

Not with the commands you sent me either .. it worked, let's keep looking or someone else who has lived this issue, thank you.

02 30 30 30 31 31 33 30 30 31 34 03 this is the same string
Good God, that makes me crazy
Thanks keep looking, help please
Regards.
S.I.T.U.
Sistemas Inteligentes de transporte urbano
http://www.situcr.com
_@Situcr.com
Desarrollos BA4/B4j androide
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Ports to 485 with converter USB, please help.

Post by Antonio Linares »

_,

Ok, I know what is going on :-)

The sent values are hexadecimal !!! ;-)

Chr( 0x02 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x31 ) + Chr( 0x31 ) + Chr( 0x33 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x31 ) + Chr( 0x34 ) + Chr( 0x03 ) // 12 bytes

Tx seems to be transmited bytes and they are 24 which it is the double, maybe the "spaces" are send too as Chr( 32 )
regards, saludos

Antonio Linares
www.fivetechsoft.com
Diego Decandia
Posts: 39
Joined: Fri Aug 22, 2014 6:21 am

Re: Ports to 485 with converter USB, please help.

Post by Diego Decandia »

_,

I do not think I understood the problem well, but reading the function Mandar_enq seems to try 10 times to send a string...

What I can tell you is that you do not have to send the string, but the sequence char (02) + char (30) + char (30) +....
User avatar
Busmatic_wpb
Posts: 162
Joined: Wed Feb 22, 2017 2:19 am

Re: Ports to 485 with converter USB, please help.

Post by Busmatic_wpb »

The hardware of the RS-232 has a dedicated line to transmit only and a separate one to receive, so there are no conflicts when transmitting and receiving.

Now..

 Transmit by 485 is not equal to transmit by 232, because in 485 you have a single line of data that is used to transmit or to receive, and you have to establish that you are going to send or receive before doing it. This will usually be done by the driver, but you still have to perform the steps in order.

   For example:

1) activate 485 to send
2) transmit the data
3) activate 485 to receive
4) wait xx seconds to see if I get a data (set a time-out to wait for them to respond)

This is what we need to do
Regards.
S.I.T.U.
Sistemas Inteligentes de transporte urbano
http://www.situcr.com
_@Situcr.com
Desarrollos BA4/B4j androide
User avatar
Busmatic_wpb
Posts: 162
Joined: Wed Feb 22, 2017 2:19 am

Re: Ports to 485 with converter USB, please help.

Post by Busmatic_wpb »

Look this

Example of C code to transmit data
Here is a basic example on how to transmit a sequence of chars (in this case 0x55 0x00 0x55) over the RS485 line.
// rs485tx.c
// Basic example of RS485 half duplex transmission

#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <asm/ioctls.h>
#include <errno.h>
#include <termios.h>

// Control struct for setting the port in 485 mode
struct rs485_ctrl {
unsigned short rts_on_send;
unsigned short rts_after_sent;
unsigned int delay_rts_before_send;
unsigned short enabled;
};

// his struct is only needed if we would like to write with ioctl()
struct rs485_wrt {
unsigned short outc_size;
unsigned char *outc;
};

int main(void) {
char dev[] = "/dev/ttyS3";
char buffer_to_send[] = { 0x55, 0x00, 0x55 };
int baudrate = B9600;

struct rs485_ctrl ctrl485;
int status;
int fd;
struct termios ti;
struct termios ti_prev;

// Open the serial port
fd = open(dev, O_RDWR|O_NONBLOCK);
if (fd < 0) {
printf("ERROR! Failed to open %s\n", dev);
return -1;
}

// Set the serial port in 485 mode
ctrl485.rts_on_send = 0; // It means that DE is at 3.3 volt on send
ctrl485.rts_after_sent = 1; // It means that DE is at 0 volt on send
ctrl485.delay_rts_before_send = 0; // DE will be active at same time of data
ctrl485.enabled = 1;
status = ioctl(fd, TIOCSERSETRS485, &ctrl485);
if (status) {
printf("ERROR PORT 1! TIOCSERSETRS485 failed %i", status);
return -1;
}

tcgetattr(fd, &ti_prev); // Save the previous serial config
tcgetattr(fd, &ti); // Read the previous serial config
cfsetospeed(&ti,baudrate); // Set the TX baud rate
cfsetispeed(&ti,baudrate); // Set the RX baud rate

cfmakeraw(&ti);
tcsetattr(fd, TCSANOW, &ti); // Set the new serial config

// Send buffer_to_send content to RS485
if (write(fd, buffer_to_send, sizeof(buffer_to_send)) != sizeof(buffer_to_send)) {
printf("ERROR! write() failed \r\n");
}

ti_prev.c_cflag &= ~HUPCL; // This to release the RTS after close
tcsetattr(fd, TCSANOW, &ti_prev); // Restore the previous serial config
close(fd);

return 0;
}
Regards.
S.I.T.U.
Sistemas Inteligentes de transporte urbano
http://www.situcr.com
_@Situcr.com
Desarrollos BA4/B4j androide
User avatar
Busmatic_wpb
Posts: 162
Joined: Wed Feb 22, 2017 2:19 am

Puerto to 485 with converter a USB ,Solucionado

Post by Busmatic_wpb »

Muy Estimado Antonio.
Diego Decandia.

Gracias Muchas gracias

Hemos logrado despues de algunos dolores de cabeza conectarnos a un periferico RS485 por medio del un puerto COM XXX donde pasamos muchas horas leyendo y tomando notas de todas las publicaciones de los listeros que se sumaron.

Agradezo de corazon y POSTEO la rutina.

#include "FiveWin.ch"

#define BUFF_IN 1024
#define BUFF_OUT 128
#define GENERIC_READ 0x80000000
#define GENERIC_WRITE 0x40000000
#define OPEN_EXISTING 3
#define FILE_ATTRIBUTE_NORMAL 0x00000080

#define NOPARITY 0
#define ODDPARITY 1
#define EVENPARITY 2
#define MARKPARITY 3
#define SPACEPARITY 4
#define ONESTOPBIT 0
#define ONE5STOPBITS 1
#define TWOSTOPBITS 2


FUNCTION LeePuerto485()

LOCAL nComm, cBloque

Public cResultado := "" ,nByteslargo ,cTrIngPuerta001 ,cTrIngPuerta002

cTrIngPuerta001:= Chr( 0x02 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x31 ) + Chr( 0x31 ) + Chr( 0x33 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x31 ) + Chr( 0x34 ) + Chr( 0x03 )


nComm := AbrePuertoCom1( "2" ) /* COM2*/

cBloque := LeePuertoCom1( nComm,128 )

CloseComm( nComm )

Return Val( SubStr( cBloque,1,7 ) )


STATIC FUNCTION AbrePuertoCom1( cPuerto )

LOCAL cDcb, nError
Local nBauds := 9600, nParity := "N", nDataByt := 8, lIRmode := .f.
Local nStopBits := ONE5STOPBITS

/*De las formas que trate encontre esta manera configurar el puerto y me funciono
"COM1" ,9600, N, 8, 1, .F.
*/

nComm := OpenComm( "COM" + cPuerto ,nBauds, nParity, nDataByt, nStopBits, lIRmode)

If nComm <= 0
nError := GetCommError( nComm )
MsgInfo( "Error al abrir puerto: " + Str( nError ) )
EndIf

If ! BuildCommDcb( "COM" + cPuerto + ":9600,N,8,1" , @cDcb )
nError := GetCommError( nComm )
MsgInfo( "Error al Configurar puerto: " + Str( nError ) )
Return 0
EndIf



If ! SetCommState( nComm, cDcb )
nError := GetCommError( nComm )
MsgInfo( "Error al setear Puerto: " + Str( nError ) )
Return 0
EndIf

Return nComm

////////////////////////////////////////////////////////////////////////////////
STATIC FUNCTION LeePuertoCom1( nComm,n )

LOCAL cBloque := Space( 50), bcc1, i :=1, nWBytes, c

cTrIngPuerta001:= Chr( 0x02 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x31 ) + Chr( 0x31 ) + Chr( 0x33 ) + Chr( 0x30 ) + Chr( 0x30 ) + Chr( 0x31 ) + Chr( 0x34 ) + Chr( 0x03 )

/* De suma importancia que el largo de la cadena sea de acorde */

nByteslargo:= Len( cTrIngPuerta001 )


If ( nWBytes := WriteComm( nComm,cTrIngPuerta001,nByteslargo ) ) <= 0
MsgStop( "Error al enviar datos la Puerto 485","Error" )
Return " "
EndIf


Sleep(100) /* Tiempo de espera para lectura del bufer, es importante esto por dos Razones.
1 Dependiendo del largo del string de retorno asi se calculara el tiempo de espera.
2 Si no lo pones no les das tiempo a Envio de informacion TX de leer el buffer completo
Asi si el bloque de datos no sale completo solo incrementa el tiempo del sleep(000)
*/

nBytes := ReadComm( nComm, @cBloque )

?? @cBloque

Return cBloque
Regards.
S.I.T.U.
Sistemas Inteligentes de transporte urbano
http://www.situcr.com
_@Situcr.com
Desarrollos BA4/B4j androide
Post Reply