Writing Hex data to COM port

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Monox18
Posts: 118
Joined: Tue Nov 25, 2014 9:48 pm
Location: Deggendorf, Germany

Writing Hex data to COM port

Post by Monox18 » Fri Jul 19, 2019 10:38 pm

Hello all,

I'm trying to control some HART devices by using LiveCode. Since there is no support for that I have to manually create and send data via serial COM ports. I have been able to open and close the port, and I know what bytes to write but I don't know how.

This is how I open it and close it.

Code: Select all

   local tPortName
   put "COM4:" into tPortName
   close file tPortName
   open file tPortName for binary update  //text, binary, write, update, read
   // write command?
   answer the result
   close file tPortName
By using a HART OPC server and a COM listener I am able to see what bytes were written and read.
Image

HART Command #1
Read:
FF FF FF FF 86 91 17 8A F9 A7 01 07 00 54 11 BF 51 84 67 9A
Write:
FF FF FF FF FF 82 91 17 8A F9 A7 01 00 D1

The write command contains both header and body bytes. I'm just clueless how to write hex bytes via LiveCode. My idea is that if I'm able to write those same bytes, I should get the same response, emulating the protocol and start working from there. Any ideas how to write Hex bytes to COM port ?

Thank you!
Monox
Developing a Cyber Physical System.
https://www.monoxware.com/

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Writing Hex data to COM port

Post by bogs » Sat Jul 20, 2019 9:37 am

Well, if you know what the bytes are, you can always use baseConvert to convert them to hex values, like this shows from the last tutorial I put out, where it is converting from rgb to hex for the table.

https://www.youtube.com/watch?v=UkCZaWZ ... dex=4&t=0s
Image

Monox18
Posts: 118
Joined: Tue Nov 25, 2014 9:48 pm
Location: Deggendorf, Germany

Re: Writing Hex data to COM port

Post by Monox18 » Tue Jul 23, 2019 6:42 pm

Ok tnx for mentioning baseConvert. Didn't know that one. I will be testing today using that and others like BinaryEncode.
Monox
Developing a Cyber Physical System.
https://www.monoxware.com/

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: Writing Hex data to COM port

Post by sphere » Thu Aug 01, 2019 3:59 am

Maybe i can help. In a former job i used to repair big up to 98" touchscreens. They where also controllable via RS232 and ethernet. For fun i recreated the Remote Controller in LiveCode. The codes where if i remember correctly in HEX. So i will look it up and put the solution here.

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: Writing Hex data to COM port

Post by sphere » Thu Aug 01, 2019 11:54 am

This is partly what i used.
You could create some dropdown buttons with some settings and use this with a normal rs232 comport or a virtual comport over LAN:
This is a piece for some settings which you can set,save or whatever:

Code: Select all

put "BAUD=" & the label of btn"baud" into tBaud -- the label is for example 9600 or 19200
   put "PARITY=" & the label of btn"parity" into tParity --the label is either none or even or odd
   put "DATA=" & the label of btn"databit" into tDatabit --the label is either 7 or 8
   put "STOP=" & the label of btn"stopbit" into tStopbit --the label is either 0 or 1
   put "xon=" & the label of btn"handshake" into gHandShake  --the label is either on or off
   put tBaud & space & tParity & space & tDatabit & space & tStopbit & space & gHandShake into gStoredSerial
   put fld"serialport"into gComport
   
this is where you open a port, don't forget to close it afterwards.
You can put it in a button or a handler

Code: Select all

   
   put "COM" & gComport & ":" into tCom
   if the platform is "MacOS" then
      open driver tCom for binary update
      else
      if the platform is "Win32" then 
         open file tCom for binary update
      end if
      end if
    if the result is not empty then
      answer "<p> <B><h3> <font color=white>Port " & tCom & " can not be opened." & cr & "Please check your Com port or Virtual Com to TCP program.</font> </B> <p>"
      end if
	  
Then send the hex code to the port:

Code: Select all

	
on mouseUp
   #example hex 07 01 02 4D 49 4E 00 08
   put "COM" & gSerialport & ":" into tCom
   
   # for windows use file, for mac use driver
   if the platform is "MacOS" then
  write numToChar(0x07) to driver tCom
  write numToChar(0x01) to driver tCom
  write numToChar(0x02) to driver tCom
  write numToChar(0x4D) to driver tCom
  write numToChar(0x49) to driver tCom
  write numToChar(0x4E) to driver tCom
  write numToChar(0x00) to driver tCom
  write numToChar(0x08) to driver tCom
   else
      if the platform is "Win32" then  
  write numToChar(0x07) to file tCom
  write numToChar(0x01) to file tCom
  write numToChar(0x02) to file tCom
  write numToChar(0x4D) to file tCom
  write numToChar(0x49) to file tCom
  write numToChar(0x4E) to file tCom
  write numToChar(0x00) to file tCom
  write numToChar(0x08) to file tCom
end if
end if
end mouseUp
Let me know if it helps.

Monox18
Posts: 118
Joined: Tue Nov 25, 2014 9:48 pm
Location: Deggendorf, Germany

Re: Writing Hex data to COM port

Post by Monox18 » Thu Aug 01, 2019 9:30 pm

Hi sphere. Thanks for your suggestion. It is interesting seeing how you solved the problem using the numToChar function.

I eventually opted for using binaryEncode and binaryDecode, so that I can send the whole command in 1 string, rather than writing byte by byte.

The bigger problems I had were 2: finding the exact configuration for the SerialControlString and finding out I need to wait some time after writing before reading.

Do you know if there's a better way of knowing when data is ready to be read rather than waiting a fixed delay?

This code sends an Universal command #0 to a HART device and succesfully returns a hex string.

Code: Select all

on mouseUp
   local tPortName, tCommand00, tTest, tEditedCommand, tFirstCommand, tTextData, tRecievedBinaryData, tActualCommand00
   set the serialControlString to "Baud=1200 Parity=O Data=8 Stop=1"
   put "FF FF FF FF FF 02 80 00 00 82" into tCommand00
   put tCommand00 into tEditedCommand
   replace space with empty in tEditedCommand
   put binaryEncode("H" & the number of chars of tEditedCommand, tEditedCommand) into tTest
   put "COM4:" into tPortName
   close file tPortName 
   open file tPortName for binary update 
   write tTest to file tPortName
   wait 1 second //How to improve this time??
   read from file tPortName until empty
   put it into tRecievedBinaryData
   get binaryDecode("H*", tRecievedBinaryData, tTextData)
   answer tTextData //Read Data is finally here
   close file tPortName
end mouseUp
Monox
Developing a Cyber Physical System.
https://www.monoxware.com/

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: Writing Hex data to COM port

Post by sphere » Thu Aug 01, 2019 10:44 pm

Hi Monox18,

i don't think you have to wait or delay for a response.
What i remember of the screens is that with the same code to set a function, you had to set one of the hex codes from 00(write) to 01 or 02(read) so the screen returned the actual setting. This was an immediate response.
We used also a program from the manufacturer to send a hex manually and also a little program to use the LAN of the Mac we used to virtual comport.

So maybe if it is a similair situation with your device, and you send a string to read out a setting, you could try Answer the Result and see what comes up.

Maybe the manufacturer manual can give some solution for this, if really neded for your case.

Try not closing the port in between, perhaps then you will catch the returned string immediately.
I don't think it's neccesary to close/open the port each time, only at the start and when closing the program.
And i would use wait for 200ms with messages so it's not blocking.
But i don't really think you need to wait, just leave the port open.

Monox18
Posts: 118
Joined: Tue Nov 25, 2014 9:48 pm
Location: Deggendorf, Germany

Re: Writing Hex data to COM port

Post by Monox18 » Fri Aug 02, 2019 8:24 pm

Thanks. I figured out the solution. I'm leaving the port Open and using wait with messages. After sending a wirte command, the device will immediately start sending data so I'm ready to read it. However, 1200bps is quite slow, so the sending process is slow and that's why I have to wait.

The manual states the meaning of each Byte and how to read them. So I developed a function that is succesful if it manages to read all the bytes in the way it is supposed to be. I'm calling this function in a loop each 10ms for a max of 100 times which will be maximum 10s. It works wonders. Commands are reported to take 400-500ms each.

Thanks for suggestion!

Code: Select all

   
   local i
   put 0 into i
   repeat for 100 times
      wait 10 milliseconds with messages
      add 1 to i
      read from file tPortName until empty
      put it after tRecievedBinaryData
      get binaryDecode("H*", tRecievedBinaryData, tDecodedData)
      put formatBytes(tDecodedData) into tFormattedData
      put interpretBytes(tFormattedData) into tCommandData
      if tCommandData is not "Error" then exit repeat
   end repeat
   writeLog "Time taken =" && i*10 && "ms" //Max is 10s
   
Monox
Developing a Cyber Physical System.
https://www.monoxware.com/

sphere
Posts: 1145
Joined: Sat Sep 27, 2014 10:32 am
Location: Earth, Except when i Jump

Re: Writing Hex data to COM port

Post by sphere » Fri Aug 02, 2019 9:01 pm

I'm glad it works for you :)

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”