Logic Machine Forum
serial library - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: serial library (/showthread.php?tid=1162)



serial library - benanderson_475 - 03.01.2018

I need to open an rs232 port in 2 different baud rates at different times,  how do i achieve this while still receiving data from the rs232 port once the port is open again.
where is the best location to handle the opening/closing of the port and where is the best location to receive the read data?

if i put some looping code in a library 

if port do
port:read(1,1)
end

other functions are not able to run while the loop is running what is the right method to receive data into a library, i need to keep the port open/recieved data coming in as i don't know when some string might be sent from the rs232 end point. 

i have looked at the serial library doc  http://openrb.com/docs/serial.htm

and can get the code below to work and have connectivity to rs232 device
any help is much appreciated.

if not port then
  require('serial')
  port = serial.open('/dev/RS232', { baudrate = 9600, parity = 'none', duplex = 'full' })
  port:flush()
end

-- port ready
if port then
  char = port:read(1, 1)
  if char then
    alert(char)
  end
end


RE: serial library - admin - 04.01.2018

Can you explain why do you need to use different baudrates? Are you trying to connect several devices to one port? This may cause permanent device damage if several devices are transmitting. On the other hand it's usually OK to connect one TX to several RX inputs.


RE: serial library - benanderson_475 - 04.01.2018

(04.01.2018, 08:02)admin Wrote: Can you explain why do you need to use different baudrates? Are you trying to connect several devices to one port? This may cause permanent device damage if several devices are transmitting. On the other hand it's usually OK to connect one TX to several RX inputs.

Yes i was thinking of connecting a few 232 devices to one port, but if this is a bad idea... i think i will keep one device for the rs233 (the one i need 2 way feedback on one device) the others,
if i use an rs232-rs485 converter and put on the rs485port will this be ok?

with the Rs232 port what is the best way to read the incoming data all the time in a library? or am i best to have a resident script and manage the port and incoming data here then parse to library to process etc?

thanks


RE: serial library - admin - 05.01.2018

You still need a resident script to handle data communication. Library is just a way of having a re-usable code in different scripts.
As for RS-485<>RS-232 adapters keep in mind that RS-485 is only half-duplex so you cannot send and receive data at the same time. If you need full-duplex then the onyl solution is USB<>RS-232 adapter.


RE: serial library - benanderson_475 - 08.01.2018

(05.01.2018, 08:24)admin Wrote: You still need a resident script to handle data communication. Library is just a way of having a re-usable code in different scripts.
As for RS-485<>RS-232 adapters keep in mind that RS-485 is only half-duplex so you cannot send and receive data at the same time. If you need full-duplex then the onyl solution is USB<>RS-232 adapter.

ok i have this sorted now, if i use twice serial.open wil this cause any issue?

port = serial.open('/dev/RS232', { baudrate = 9600, parity = 'none', duplex = 'full' })

1 in resident script to recieve data and 1 in library to send data 

many thanks.


RE: serial library - admin - 09.01.2018

Do you mean that you want to write from event scripts? This is not recommended as several scripts might try to access the port at once. Here's a short example that uses single resident script for port access and internal UDP server to handle data from event scripts:
https://forum.logicmachine.net/showthread.php?tid=399&pid=2015#pid2015