I have some problem because I don't know how you can put in which COM(serial port) is my arduino in next code:
require 'serialport'
sp = SerialPort.new "/dev/**PATH_TO_YOUR_DEVICE**", 9600
case ARGV[0]
when 1
sp.write 1
when 0
sp.write 0
end
How do you write that PATH?
Thanks for the help
Well after some investigation I found my own answer and I want to share with you guys because it was hard find it.
parameters = {
"baud"=>9600,
"data_bits"=>8,
"stop_bits"=>1,
"parity"=>SerialPort::NONE }
comm_port = 3
sp = SerialPort.new(comm_port, parameters)
Probably it will be useful for someone.
Related
guys i write a chat bot for twitch in hobby. i want to spam a word in chat in a time like that write to chat"hello" and wait 10 sec then write again
i did that but in my code have a answer system like that when anyone write to chat !hey my bot answering hello to him. and when i tried in same system that not working
until #socket.eof? do
message = #socket.gets
puts message
if message.match(/PRIVMSG ##{#channel} :(.*)$/)
a = Time.now
b = Time.now + 5
while a < b do
write_to_chat("!prime")
a += 4
sleep(20)
end
end
if message.match(/PRIVMSG ##{#channel} :(.*)$/)
content = $~[1]
username = message.match(/#(.*).tmi.twitch.tv/)[1]
if content.include? 'theoSea'
write_to_chat(" theoAse theoAse theoAse")
end
i solved it with scheduler
i wanted a code for write every 5 min i tried lots of method but sleep command
and i found this link "https://github.com/jmettraux/rufus-scheduler"
all in link u can use this gem.
thx for everything guys.
I have a range of ip addresses that thanks to some google fu I have been able to take the range and convert to an array of IP addresses. However I am running out of memory when the script is ran against a range of say 10.234.xxx.1/24. As such I am trying to see if there is a way to pull out a subset of 32 addresses so that I don't have this extra memory usage.
ips.each do |ip|
ip_from = ip.instance_of?(Nexpose::HostName) ? ip.host : (ip.from..ip.to).map(&:to_s)
ip_from2 = ip_from.select{ |i| /[0-9]*\.[0-9]*\.[0-9]*\.([2-3][0-9]|64)$/ =~ i }
ip_from2.each do |ip2|
log.log_debug_message("Getting credentials for #{ip2}")
secret_summary = ss.get_secret_id(token, ip2)
unless secret_summary.nil?
log.log_debug_message("Found credentials for #{ip2}")
# Gets OS
asset_data = {}
asset_data[:ip] = ip2
res = ss.get_secret_simple(token, secret_summary[:secret_id])
asset_data[:username] = res[:username]
asset_data[:password] = res[:password]
credential = Nexpose::SiteCredential.for_service(ss.check_type(secret_summary[:secret_type]),
asset_data[:username],
asset_data[:password],
nil,
ip2)
I would like to only have it hold 32 addresses in the array as you can see in the select statement on line 3. Any help is appreciated.
Sidenote: I have modified the nexpose_thycotic gem to make this work for my purposes. I am working through rapid7 as well but I was hoping someone on StackOverflow may be able to answer a little quicker as this is a time sensitive matter.
Hi guys I am new to coding and I am trying to monitor the voltage and resistance value of a potantiometer by using spi communication on raspberryPi3. I found that code but when I try to run it, the program gives me:
Problem transmitting spi data..ioc" invalid argument
I read the code carefully again but I couldn't find anything wrong. Maybe I am missing something. If you help me it would be so good. Thanks:) By the way the code is here:
http://www.hertaville.com/interfacing-an-spi-adc-mcp3008-chip-to-the-raspberry-pi-using-c.html
You should try to fully initialize your SPI structure.
.........
spi[i].speed_hz = this->speed ;
spi[i].bits_per_word = this->bitsPerWord ;
spi[i].cs_change = 0;
//Ypu should add this lines
spi[i].pad = 0;
spi[i].tx_nbits = 0;
spi[i].rx_nbits = 0;
It should help :)
I am connecting to device using code blow on MacOS and out of 100 times this code would make connection only 1 or two times and dones't respond(since there is no timeout) rest of the times.
ser = serial.Serial(port="/dev/xyz",timeout = None, baudrate=115200, parity = serial.PARITY_NONE, bytesize = serial.EIGHTBITS, stopbits = serial.STOPBITS_ONE)
def exitSer(ser):
print("Closing")
ser.close()
atexit.register(exitSer, ser)
if ser.is_open:
time.sleep(2)
while(1):
print(ser.readline().decode("utf-8"))
Could you please tell me how to use programs like fcntl etc to find if this port is completely free and available for use and how to set tty port's flags to free after making port free forcibly.
Once this works, I have to run this multithreaded where each thread is running different devices expecting output in lines. Any suggestions for that just in case this works.
def startSerial(tty_id):
ser = serial.Serial(port = tty_id, timeout = None)
ser.close()
ser.open()
if ser.isOpen():
print(ser.portstr, ":connection successful.")
return ser
else:
return False
Calling ser.close() before .open() fixed it. I tested it about 200 times and i haven't been dissappointed so far. I am testing it now in multithreaded and hopefully that works too.
Thank you everybody.
Maybe I've gotten my sockets programming way mixed up, but shouldn't something like this work?
srv = TCPServer.open(3333)
client = srv.accept
data = ""
while (tmp = client.recv(10))
data += tmp
end
I've tried pretty much every other method of "getting" data from the client TCPSocket, but all of them hang and never break out of the loop (getc, gets, read, etc). I feel like I'm forgetting something. What am I missing?
In order for the server to be well written you will need to either:
Know in advance the amount of data that will be communicated: In this case you can use the method read(size) instead of recv(size). read blocks until the total amount of bytes is received.
Have a termination sequence: In this case you keep a loop on recv until you receive a sequence of bytes indicating the communication is over.
Have the client closing the socket after finishing the communication: In this case read will return with partial data or with 0 and recv will return with 0 size data data.empty?==true.
Defining a communication timeout: You can use the function select in order to get a timeout when no communication was done after a certain period of time. In which case you will close the socket and assume every data was communicated.
Hope this helps.
Hmm, I keep doing this on stack overflow [answering my own questions]. Maybe it will help somebody else. I found an easier way to go about doing what I was trying to do:
srv = TCPServer.open(3333)
client = srv.accept
data = ""
recv_length = 56
while (tmp = client.recv(recv_length))
data += tmp
break if tmp.length < recv_length
end
There is nothing that can be written to the socket so that client.recv(10) returns nil or false.
Try:
srv = TCPServer.open(3333)
client = srv.accept
data = ""
while (tmp = client.recv(10) and tmp != 'QUIT')
data += tmp
end