Hello Guest it is June 15, 2025, 11:42:08 AM

Author Topic: please help for modbus in macro  (Read 9765 times)

0 Members and 2 Guests are viewing this topic.

Re: please help for modbus in macro
« Reply #10 on: March 13, 2025, 12:58:59 PM »
dear Graham Waterworth
can you help to send hex 00 (chr(nul)) in serial port?
for example: 01 06 00 08 00 DE 88 50

Offline Graham Waterworth

*
  • *
  •  2,783 2,783
  • Yorkshire Dales, England
Re: please help for modbus in macro
« Reply #11 on: March 13, 2025, 01:06:24 PM »
From the Mach3 manual

SendSerial
Sub SendSerial(Data As String)
This function send the String Data to the serial port specified in the Config-
>GeneralConfig serial port configuration. This provides transmit-only capability, at any
supported BAUD rate.
Arguments:
String message to be sent to configured serial device
Return Value:
None
Example:
‘ Send “Hello, world!” to serial device
SendSerial(“Hello, world!” & chr(10) & char(13))
Without engineers the world stops

Offline TPS

*
  •  2,592 2,592
Re: please help for modbus in macro
« Reply #12 on: March 14, 2025, 12:47:38 PM »
from the VBScript_Command manual:

Chr Function
   Chr(int)

so if you want to send a Hex D9 it must be:

Chr(217)

Hex D9 -> Int 217

just to give the Chr function a real chance
 
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.

Offline TPS

*
  •  2,592 2,592
Re: please help for modbus in macro
« Reply #13 on: March 14, 2025, 01:05:00 PM »
if you prefere your Hex values you can also try this:
Code: [Select]
sendbyte1 = &H01
sendbyte2 = &H06
sendbyte3 = &H00
sendbyte4 = &H01
sendbyte5 = &H00
sendbyte6 = &H04
sendbyte7 = &HD9
sendbyte8 = &HC9

SendSerial Chr(sendbyte1)+ _
Chr(sendbyte2)+ _
Chr(sendbyte3)+ _
Chr(sendbyte4)+ _
Chr(sendbyte5)+ _
Chr(sendbyte6)+ _
Chr(sendbyte7)+ _
Chr(sendbyte8)


just one way of thousand's
« Last Edit: March 14, 2025, 01:15:12 PM by TPS »
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: please help for modbus in macro
« Reply #14 on: April 01, 2025, 03:13:18 AM »
if you prefere your Hex values you can also try this:
Code: [Select]
sendbyte1 = &H01
sendbyte2 = &H06
sendbyte3 = &H00
sendbyte4 = &H01
sendbyte5 = &H00
sendbyte6 = &H04
sendbyte7 = &HD9
sendbyte8 = &HC9

SendSerial Chr(sendbyte1)+ _
Chr(sendbyte2)+ _
Chr(sendbyte3)+ _
Chr(sendbyte4)+ _
Chr(sendbyte5)+ _
Chr(sendbyte6)+ _
Chr(sendbyte7)+ _
Chr(sendbyte8)


just one way of thousand's

Dear TPS
thanks so much
the hex byte "00" is not write on port monitor (its ignored). according to attached picture, Also, instead of D9 and C9 characters, 55 and 45 were sent to port.
Is this solvable or not?  Thank you very much for your answer.
thanks and regards

Offline TPS

*
  •  2,592 2,592
Re: please help for modbus in macro
« Reply #15 on: April 01, 2025, 07:01:52 AM »
ok, had to dig a bit deeper, this sendserial function look's like to be a bit bugy.
copy the attached DLL file to C:\Mach3 and use this code:
Code: [Select]

' *****************************************************************************************************
'
' Mach3 / Cypress Enable script code to use the ELEKTOR Serial.dll Dynamic Link Library for serial port
' communication and control from a Mach3 script.
'
' *****************************************************************************************************


' Pass in the portnumber to open (1 for COM1, 2 for COM2 etc). Get the port number in return if successfull.
Declare Function OpenCOM Lib "Serial.dll" (ByVal Port As Integer) As Integer

' Closes the opened port, no parameters to pass. Always returns 0.
Declare Function CloseCOM Lib "Serial.dll" As Integer

' Gets the Windows handle.
Declare Function GetHandle Lib "Serial.dll" As Integer

' Pass in a character to be sent. Datatype was declared as Char but no such type exists in Cypress, Integer seems to work if handled properly.
Declare Function SendCharCOM Lib "Serial.dll" (ByVal ch As Integer) As Integer

' Function expects an Integer sent as ByRef ("pointer to an integer") and returns TRUE if data is available
Declare Function ReadCharCOM Lib "Serial.dll" (ByRef ch As Integer) As Boolean

' Returns true if a COM port with the passed in number exists and is free to use.
Declare Function COMPortExists Lib "Serial.dll" (ByVal Port As Integer) As Integer

' Gets the port number of the currently opend port, zero if no port is opened (?)
Declare Function GetPortNr Lib "Serial.dll" As Integer

' 0=None, 1=odd, 2=even, 3=mark 4=space?????
Declare Function ParitySet Lib "Serial.dll"(ByVal par As Integer) As Boolean

' 4-8 are valid
Declare Function BitsPerByteSet Lib "Serial.dll" (ByVal pbp As Integer) As Boolean

' Any baudrate is valid, returns false if not possible.
Declare Function BaudRateSet Lib "Serial.dll" (ByVal BaudRate As Long) As Boolean

' 0=1 stopbit, 1=1.5 stopbit, 2=2 stopbits. (1.5 may not be valid on all hardware)
Declare Function StopBitsSet Lib "Serial.dll" (ByVal stp As Integer) As Boolean

' ************************** End of function declaration for the Serial.dll library ***********************************


Sub Main
On Error GoTo HandleIt

Dim TxChar   As Integer
Dim TxString As String
Dim ComPort As Integer

ComPort =1

sendbyte1 = &H01
sendbyte2 = &H06
sendbyte3 = &H00
sendbyte4 = &H01
sendbyte5 = &H00
sendbyte6 = &H04
sendbyte7 = &HD9
sendbyte8 = &HC9

'try to open the Comport
If OpenCom(ComPort) <> ComPort Then ' Open COM port
MsgBox("Couldn't open COM port!!")
Result = CloseCom() ' Close the port.
Exit Sub ' Nothing left to do, exit
End If

ParitySet(0) ' Set parity to NONE
BitsPerByteSet(8) ' Set databits to 8
StopBitsSet(0) ' Set stopbits to 1
BaudrateSet(9600) ' Set baudrate to 9600

'send the byte's
SendCharCOM(Asc(Chr(sendbyte1)))
SendCharCOM(Asc(Chr(sendbyte2)))
SendCharCOM(Asc(Chr(sendbyte3)))
SendCharCOM(Asc(Chr(sendbyte4)))
SendCharCOM(Asc(Chr(sendbyte5)))
SendCharCOM(Asc(Chr(sendbyte6)))
SendCharCOM(Asc(Chr(sendbyte7)))
SendCharCOM(Asc(Chr(sendbyte8)))

Result = CloseCom() ' Close the port.

Exit Sub

' *************************************************************************************************************************
HandleIt:
' If an error occurs we must close the port or it will be left open and unusable.
MsgBox("An unexpected error occured. Closing comport and Exiting" & CloseCom())
Exit Sub
' *************************************************************************************************************************
 
End Sub       




anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: please help for modbus in macro
« Reply #16 on: April 03, 2025, 02:18:18 AM »
dear TPS
thanks for answer. now it can send hex 00 but it send line by line. is it possible for you to solve how to send in single line?

I trying below but not working:
Code: [Select]
'send the byte's
SendCharCOM Asc(Chr(sendbyte1))+ _
                        Asc(Chr(sendbyte2))+ _
                        Asc(Chr(sendbyte3))
thanks and regards

Offline TPS

*
  •  2,592 2,592
Re: please help for modbus in macro
« Reply #17 on: April 03, 2025, 04:57:42 AM »
can't get what you mean line by line.

if i trace data here i can see that the raw data is ok, and no aditional between the single bytes.
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.

Offline TPS

*
  •  2,592 2,592
Re: please help for modbus in macro
« Reply #18 on: April 03, 2025, 05:08:13 AM »
made a second test with a real serial link to a second PC and using an other serial port monitor.
same result.
anything is possible, just try to do it.
if you find some mistakes, in my bad bavarian english,they are yours.
Re: please help for modbus in macro
« Reply #19 on: April 03, 2025, 07:11:41 AM »
Dear TPS
thanks for answer. attached pic show line by line output vs single line.
I use vb script windows in mach3 for testing code and serial port monitor.
when the hex data send line by line the receiver side don't receive command. when use Modbus pull software its work correctly because it can be send hex in single line.
thanks
« Last Edit: April 03, 2025, 07:13:30 AM by mjzrpr »