Follow along with the video below to see how to install our site as a web app on your home screen.
Nota: This feature currently requires accessing the site using the built-in Safari browser.
Buen día,
hoy quiero pedir su colaboración acerca de realizar en visual basic.net CRC 16 Modbus, si tienen algun ejemplo les agradezco,
Gracias
#Region "Calculo CRC16 ModBus"
Public Function CRC16(ByVal data() As Byte) As Int64
Dim CRC16Lo As Byte, CRC16Hi As Byte 'CRC register
Dim CL As Byte, CH As Byte 'Polynomial codes & HA001
Dim SaveHi As Byte, SaveLo As Byte
Dim i As Integer
Dim Flag As Integer
CRC16Lo = &HFF '&HFF
CRC16Hi = &HFF '&HFF
CL = &H1
CH = &HA0
For i = 0 To LenghtDatos
CRC16Lo = CRC16Lo Xor data(i) 'for each data and CRC register XOR
For Flag = 0 To 7
SaveHi = CRC16Hi
SaveLo = CRC16Lo
CRC16Hi = CRC16Hi \ 2 'peak shift to the right one
CRC16Lo = CRC16Lo \ 2 'shift to the right a low
If ((SaveHi And &H1) = &H1) Then 'If the high byte last one for a
CRC16Lo = CRC16Lo Or &H80 'then the low byte shifted to the right after the meeting in front of a
End If 'Otherwise, auto-fill 0
If ((SaveLo And &H1) = &H1) Then 'If the LSB is 1, then XOR with the polynomial codes
CRC16Hi = CRC16Hi Xor CH
CRC16Lo = CRC16Lo Xor CL
End If
Next Flag
Next i
ReturnData(0) = CRC16Hi 'CRC high
ReturnData(1) = CRC16Lo 'CRC low
End Function
#End Region
#include <18F26J50.h>
//#ifdefined(_PCH_)
#device adc=10
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPUDIV
#use delay(clock=20000000) //Utilizo frecuencia de 12Mhz la del oscilador
#pin_select U2RX=PIN_B7
#pin_select U2TX=PIN_B6
#use rs232(baud=9600,parity=N,xmit=PIN_b6,rcv=PIN_b7,bits=8,stream=AppPC) //Puerto1
#use rs232(baud=9600,parity=N,xmit=PIN_c6,rcv=PIN_c7,bits=8,stream=Campo)//Puerto2
#include <stdlib.h>
#include <string.h>
#use standard_io(a)
#use standard_io(b)
#use standard_io(c)
#define PIN_ON output_high
#define PIN_OFF output_low
//*************************************************************************************************************************************
//--------------------------------------variables globales
//*************************************************************************************************************************************
int i,lenghtdatos;
int8 cbuff[255]; // Buffer de recepcion de datos serie
//*************************************************************************************************************************************
//--------------------------------------Interrupcion serie
//*************************************************************************************************************************************
#INT_RDA
void Recepcion(void) //
{
lenghtdatos=getc(Campo);
for(i=0;i<=lenghtdatos;i++)
{cbuff[i]=getc(Campo);}
fputc(43,Campo);
}
#INT_RDA2
void Recep(void) //
{
lenghtdatos=getc(AppPC);
for(i=0;i<=lenghtdatos;i++)
{cbuff[i]=getc(AppPC);}
fputc(43,AppPC);
}
//*************************************************************************************************************************************
//------------------------------------Programa Principal
//*************************************************************************************************************************************
void main()
{
SET_TRIS_C(0B10111111);
enable_interrupts(INT_RDA); // Habilita la interrupcion serial
enable_interrupts(INT_RDA2); // Habilita la interrupcion serial
enable_interrupts(GLOBAL); // Habilito todas las interrupciones
//------------------------------------------------MI PROGRAMA!!-------------------------------------------------------------------
while (true) // bucle infinito
{
fputs("MensajePuerto1",Campo);
fputs("MensajePuerto2",AppPC);
delay_ms(3000);
}
}
using System;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports; // No olvidar y añadir en referencia.
namespace Puerto_serie_con_Net_50
{
public partial class Form1 : Form
{
SerialPort serialPort1 = new SerialPort();
public Form1()
{
InitializeComponent();
Configuracion();
}
void Configuracion()
{
// Configuramos el puerto serie.
serialPort1.BaudRate = 9600; // Baudios. 115200
serialPort1.PortName = "COM2"; // Nombre del puerto serie.
serialPort1.Parity = Parity.None; // Sin paridad.
serialPort1.DataBits = 8; // 8 Bits.
serialPort1.StopBits = StopBits.Two; // Bits de parada.
serialPort1.ReadBufferSize = 4096; // Tamaño del Búffer de lectura en Bytes.
serialPort1.WriteBufferSize = 2048; // Tamaño del Búffer de escritura en Bytes.
serialPort1.ReadTimeout = 500;
serialPort1.WriteTimeout = 500;
serialPort1.DtrEnable = false;
serialPort1.RtsEnable = false;
// Abrir puerto mientras se ejecuta la palicación.
if (!serialPort1.IsOpen)
{
try
{
serialPort1.Open(); // Abrir puerto.
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
private void button_ON_Click(object sender, EventArgs e)
{
byte[] mBuffer = Encoding.ASCII.GetBytes("Led_ON");
serialPort1.Write(mBuffer, 0, mBuffer.Length);
}
private void button_OFF_Click(object sender, EventArgs e)
{
// Variable tipo arreglo codificado en ASCII.
byte[] mBuffer = Encoding.ASCII.GetBytes("Led_OFF");
// Envía en la variable mBuffer "Led_OFF" al puerto serie.
serialPort1.Write(mBuffer, 0, mBuffer.Length);
}
// Al cerrar la ventana o el formulario
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
serialPort1.Close(); // Cerrar puerto.
}
catch (Exception error)
{
MessageBox.Show(error.Message, "Aviso:",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
}