how read data from Serial Port?

Typically: "How do I... ", "How can I... " questions
Post Reply
MEsher
Posts: 4
Joined: 22 May 2013, 18:41

how read data from Serial Port?

Post by MEsher » 26 May 2013, 10:43

I misunderstood the description "blockingOperation" in the function simSerialRead. My simple topic:

Code: Select all

if (buttonID==5) then
	countSendChar=simSerialSend(comHandle, simGetUIButtonLabel(ui, 5))
	if (countSendChar==-1) then simSetUIButtonLabel(ui,8,"N")
	else 
		simSetUIButtonLabel(ui,8,countSendChar)
		countReadChar=simSerialCheck(comHandle)
		if (countReadChar==-1) then
			simSetUIButtonLabel(ui,9,"N")
		else	
			simSetUIButtonLabel(ui,9,countReadChar)
			getString=simSerialRead(comHandle, countReadChar+1, (1~=1),'',0)
			if (getString==-1) then simSetUIButtonLabel(ui,6,"NOT")
			else simSetUIButtonLabel(ui,6,getString)
			end
		end
	end
end
I insert TRUST instead of "blockingOperation", but and with NIL the function does not display anything.
Thanks Google Translated for my English

coppelia
Site Admin
Posts: 7075
Joined: 14 Dec 2012, 00:25

Re: how read data from Serial Port?

Post by coppelia » 26 May 2013, 11:59

Hello,

I guess your child script runs non-threaded.

I would do it like that:

Code: Select all

if (buttonID==5) then
   countSendChar=simSerialSend(comHandle, simGetUIButtonLabel(ui, 5))
   if (countSendChar==-1) then 
      simSetUIButtonLabel(ui,8,"N")
   else
      simSetUIButtonLabel(ui,8,countSendChar)
      countReadChar=simSerialCheck(comHandle)
      if (countReadChar<=0) then
         simSetUIButtonLabel(ui,9,"N")
      else   
         simSetUIButtonLabel(ui,9,countReadChar)
         getString=simSerialRead(comHandle, countReadChar, false,'',0)
         if (getString==-1) then 
            simSetUIButtonLabel(ui,6,"NOT")
         else 
            simSetUIButtonLabel(ui,6,getString)
         end
      end
   end
end
Cheers

MEsher
Posts: 4
Joined: 22 May 2013, 18:41

Re: how read data from Serial Port?

Post by MEsher » 26 May 2013, 16:15

Thahks for calligraphy code.
Сheers
Don't get too excited:)

What character ends the message from function simSerialSend(comHandle, STRING).

It does not work with standard transfer for AT-commands of phones or MDrive (stepping motor).

How do I replace at the end of 'NULL'(0x00) to 'LF'(0x0A') character for correct transfer.

coppelia
Site Admin
Posts: 7075
Joined: 14 Dec 2012, 00:25

Re: how read data from Serial Port?

Post by coppelia » 27 May 2013, 09:55

Hum... Google translate is not that good as it seems ;)

To send special chars at the end of a string, try:

Code: Select all

myString="Hello"
myString=myString..string.char(42)
myString=myString.."World"
The result will be Hello*World

Cheers

Post Reply