Но у меня возникли проблемы с преобразованием 16-битных данных в 8-битные, пожалуйста, дайте мне знать, как я могу их преобразовать
Код: Выделить всё
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Shared timCount As Integer = 0
Public Shared cmd As Integer = 0
Public Shared deviceID As Integer = 1
Public Shared COMM_OK As Integer = 0
'// sending commands (how do I configure my 16 bit register here )
Private Sub Timer500ms_Tick(sender As Object, e As EventArgs) Handles Timer500ms.Tick
timCount += 1
Select Case cmd
Case 0
ReadDataRegisters(deviceID)
cmd = 1
Case 1
ReadConfigRegisters(deviceID)
cmd = 0
Case 2
Writecommand(10, 20) 'accessing 11th register and writing 20 to the accessed register
' cmd = 2 // if I send cmd command it doesn't work , why ???
Case 3
'Writecommand(10, 30) 'LSB
Case 4
' Writecommand(6)
Case Else
End Select
End If
End Sub
'//reading input 3xx band data here
Private Sub ReadDataRegisters(ByVal device_address As Byte)
Dim crc(1) As Byte
Dim readDataCMD(7) As Byte
readDataCMD(0) = device_address 'Slave address
readDataCMD(1) = 4 'Function code (input_registers)
readDataCMD(2) = 0 'Start Address HIGH
readDataCMD(3) = 0 'Start Address LOW
readDataCMD(4) = 0 'No of registers
HIGH readDataCMD(5) = 4 'No of registers
LOW crc = Me.CRC(readDataCMD) 'Call CRC Calculate.
readDataCMD(6) = crc(0)
readDataCMD(7) = crc(1)
SerialPort1.Write(readDataCMD, 0, readDataCMD.Length)
Thread.Sleep(100) 'Wait
If SerialPort1.BytesToRead > 5 Then
Dim readBuffer As Byte() = New Byte(SerialPort1.BytesToRead - 1) {}
SerialPort1.Read(readBuffer, 0, readBuffer.Length)
crc = Me.CRC(readBuffer) ' Call CRC Calculate.
If readBuffer(readBuffer.Length - 1) = crc(1) And readBuffer(readBuffer.Length - 2) = crc(0) Then
LabelStartCondition.Enabled = False
input register = (readBuffer(3) * 256) + readBuffer(4) // read input register
COMM_OK = 1
Else
COMM_OK = 0
End If
End Sub
'// reading 4xx band registers here
Private Sub ReadConfigRegisters(ByVal device_address As Byte)
Dim crc(1) As Byte
Dim readConfigCMD(7) As Byte
readConfigCMD(0) = device_address
readConfigCMD(1) = 3 ' function code ((holding registers)
readConfigCMD(2) = 0
readConfigCMD(3) = 0 // do i need to write my 16 bit msb and lsb here ????
readConfigCMD(4) = 0
readConfigCMD(5) = 1
crc = Me.CRC(readConfigCMD) ' Call CRC Calculate.
readConfigCMD(6) = crc(0)
readConfigCMD(7) = crc(1)
SerialPort1.Write(readConfigCMD, 0, readConfigCMD.Length)
Thread.Sleep(100)
If SerialPort1.BytesToRead > 5 Then
Dim readBuffer As Byte() = New Byte(SerialPort1.BytesToRead - 1) {}
SerialPort1.Read(readBuffer, 0, readBuffer.Length)
crc = Me.CRC(readBuffer) ' Call CRC Calculate.
If readBuffer(readBuffer.Length - 1) = crc(1) And readBuffer(readBuffer.Length - 2) = crc(0) Then
Device_ID = (readBuffer(3) * 256) + readBuffer(4)
End If
End If
End Sub
'//writing to modbud registers (this is my main concern )
Private Sub Writecommand(ByVal add As Byte, ByVal val As Byte)
Dim crc(1) As Byte
Dim writecommandCMD(7) As Byte
writecommandCMD(0) = deviceID
writecommandCMD(1) = 6
writecommandCMD(2) = 0
writecommandCMD(3) = add
writecommandCMD(4) = 0
writecommandCMD(5) = val
crc = Me.CRC(writecommandCMD) ' Call CRC Calculate.
writecommandCMD(6) = crc(0)
writecommandCMD(7) = crc(1)
SerialPort1.Write(writecommandCMD, 0, writecommandCMD.Length)
Thread.Sleep(100)
If SerialPort1.BytesToRead > 5 Then
Dim readBuffer As Byte() = New Byte(SerialPort1.BytesToRead - 1) {}
SerialPort1.Read(readBuffer, 0, readBuffer.Length)
crc = Me.CRC(readBuffer) ' Call CRC Calculate.
If readBuffer(readBuffer.Length - 1) = writecommandCMD(7) And readBuffer(readBuffer.Length - 2) = writecommandCMD(6) Then
' Echo received CommandSent = 1
If cmd = 2 Then
cmd = 3 'proceed with writing 1 to register 40104
ElseIf cmd = 3 Then
cmd = 4 ' proceed with writing 1 to register 40101eeprom writing
'ElseIf cmd = 4 Then
' cmd = 5 '
Thread.Sleep(2000)
' cmd = 0 End If
End If
End If
Код: Выделить всё
Private Sub Writecommand
Подпрограмма Private Sub Timer500ms_Tick просто отправляет команду в определенный регистр
Я использую cmd для отправки команд, если cmd =2, то записываю команду в регистр аналогично тому, как я делаю это со своими 16-битными регистрами. Пожалуйста, просто напишите мне, как это можно сделать, спасибо.
Хорошо, код работает нормально, единственная проблема в том, что я не могу отображать 16-битные данные, как мне настроить lsb и msb с использованием языка C#
Я буду признателен за любой написанный код для msb и lsb< /em> От 8 до 16 бит в языке C#
Я пробовал отправлять команды, используя 8 бит, и это работает отлично, но я не могу сделать это с 16 битамиПожалуйста, дайте мне знать, как мне писать на этом языке, я знаю, как это делается на простом языке C
Код: Выделить всё
`Msb = u_8 (val >>8)
Lsb = u_8 (val)
`
And to read it should be
`Msb = u_16 (val
Подробнее здесь: [url]https://stackoverflow.com/questions/78713495/16-bit-conversion-to-8-bit-register-of-modbus-communication-using-c-sharp-visual[/url]