How to send data in a serial port using button on Python tkinter? - user-interface

I define a function to be used in a button (tkinter). I am sending a string over the port.
But every-time is shows that the "port is closed". Here is the code:
def myClick2(): #defines the button what should be the output when you click and also makes entry to the text box
string = windspeeddata()
button_rx.configure("state") == 'disabled'
button_tx.configure("state") == 'normal'
try:
serialPort = serial.Serial(port=portno(), baudrate=baudval(), bytesize=8, timeout=None, stopbits=serial.STOPBITS_ONE)
serialPort.isOpen()
while True:
windspeedstring = string + '\r\n'
windspeed_encode = str.encode(windspeedstring)
serialPort.write(windspeed_encode)
# if button_rx.cget("state") == 'disabled' and button_tx.cget("state") == 'normal':
# f.writelines(winddirectionstring)
# serialPort.write(windspeed_encode)
# else:
# serialPort.open()
except IOError:
print('Serial port is closed')
if running:
root.after(1000, myClick2) #self calls the function every 1000 millisecond
I am trying to send string over the port. condition are:
If port is closed it will open
If opened already than it will send (or close the port and send )

Related

Windows Perl - Win32::Systray and IO::Socket::Multicast - how to get them work together?

Hi stackoverflow community!
I am trying to make Win32::Systray and IO::Socket::Multicast work together in one script. But I just can't for a week haha!
1st target: make a system tray with buttons (Options/Exit/Other stuff).
2nd target: listen for port communication and execute actions once any communication received.
The problem: Once one of them will execute (like the Win32::Systray), the script loops on either the systray or the port listener (like a loop, I did not review the module itself - lazy).
For the Muticast too, its on loop, that is the end of it. It wont continue to below of the script (obviously its a endless loop, it will loop there).
What I need: I want to make a systray icon that will fuction properly when I click it (showing buttons) and in the same time, listening to port communication with no conflicts on both.
#---------------------------------------------------------------
# Perl Modules
#---------------------------------------------------------------
# use strict;
# use warnings;
use IO::Socket::Multicast;
#use IO::Socket::INET;
use Win32;
use Win32::GUI;
use Win32::SysTray;
use Win32::Process;
use File::Basename;
use File::Spec::Functions qw[ catfile rel2abs updir ];
use Tk;
my $domain_cfg = rel2abs(dirname($0)).'\cfg\site_domain.cfg';
open(FH, '<', $domain_cfg) or die $!;
while(<FH>){
$domain = $_;
}
close(FH);
print "domain_file:$domain_cfg\n";
print "domain:$domain\n";
#---------------------------------------------------------------
# SysTray
#---------------------------------------------------------------
my $tray = new Win32::SysTray (
'icon' => rel2abs(dirname($0)).'\img\server.ico', #\\Demosai DIR
'single' => 1,
'name' => $title,
'tip' => 'test'
) or exit 0;
$tray->setMenu (
"> Option" => sub {
print "LOL!\n";
},
">-" => 0,
"> Exit" => sub {
terminate();
},
);
#$tray->runApplication;
$tray->runApplication unless fork;
#---------------------------------------------------------------
# Wait and Read Port
#---------------------------------------------------------------
$default_port = '3671';
$default_reply_ip = '00.00.00.00';
$default_reply_port = '3672';
$default_reply = 'MESSAGE_RECEIVED';
print "Running Comms... Port [$default_port]\n";
# Create a new multicast UDP socket ready to read datagrams sent to port 3671
my $s = IO::Socket::Multicast->new(LocalPort=>$default_port);
# Add a multicast group address
$s->mcast_add('00.00.00.00');
# Optionally, the address can be associated with a specific network adapter
# $s->mcast_add('00.00.00.00','eth0');
$tmp_ctr = 0;
my $data;
while (1) {
# Wait until a packet is received
$s->recv($data,1024);
print "Data received: ".length($data)." = $data\n";
#---------------------------------------------------------------
# REPLY
#---------------------------------------------------------------
print "Replying...";
#$s->mcast_send($default_reply,$default_reply_ip);
}
#---------------------------------------------------------------
# END OF FILE
#---------------------------------------------------------------
print "EOF\n";

How can I generate traffic from trace file in mininet?

I have trace file name dec-pkt which have 6 columns as follow:
timestamp of packet arrival.
For the first packet in the trace, this is the
raw tcpdump timestamp. For the remaining
packets, this is the offset from the integer
part of that first timestamp.
For example, if the first timestamp is 187.2, the
second is 188.9, and the third is 191.3, then
the first three timestamps in the ASCII file will
be 187.2, 1.9 (= 188.9-187), and 4.3 (=191.3-187).
Note that sanitize-syn-fin uses as its base time
the arrival of the first TCP packet in the file,
not the first TCP SYN/FIN/RST packet (this helps
when comparing sanitize-syn-fin times with those
produces by sanitize-tcp).
(renumbered) source host
(renumbered) destination host
Note that the renumbering process loses any IP network
information.
source TCP port
destination TCP port
number of data bytes in the packet, or 0 if none (this
can happen for packets that only ack data sent by the
other side)
So I wonder how can I generate this traffic using this file? can Iperf
do that? if not how can I do that?
You can generate traffic easily via Scapy. It also comes with Mininet VM which you can find in mininet official website. It can generate packet for both TCP and UDP.
Here is an example python code. You can find more on Github or Scapy's official tutorial.
import sys
import getopt
import time
from os import popen
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
from scapy.all import sendp, IP, UDP, Ether, TCP
from random import randrange
def sourceIPgen():
not_valid = [10,127,254,1,2,169,172,192]
first = randrange(1,256)
while first in not_valid:
first = randrange(1,256)
ip = ".".join([str(first),str(randrange(1,256)),str(randrange(1,256)),str(randrange(1,256))])
return ip
def gendest(start, end):
first = 10
second =0; third =0;
ip = ".".join([str(first),str(second),str(third),str(randrange(start,end))])
# print start
# print end
return ip
#if __name__ == '__main__':
#main()
def main(argv):
# global start
# global end
print argv
try:
opts, args = getopt.getopt(sys.argv[1:],'s:e:',['start=','end='])
except getopt.GetoptError:
sys.exit(2)
for opt, arg in opts:
if opt =='-s':
start = int(arg)
elif opt =='-e':
end = int(arg)
if start == '':
sys.exit()
if end == '':
sys.exit()
interface = popen('ifconfig | awk \'/eth0/ {print $1}\'').read()
for i in xrange(1000):
packets = Ether()/IP(dst=gendest(start, end),src=sourceIPgen())/UDP(dport=80,sport=2)
print(repr(packets))
sendp( packets,iface=interface.rstrip(),inter=0.1)
if __name__ == '__main__':
main(sys.argv)
Referance
And you can call it like
python ./launchTraffic -s 2 -e 28

'WinError 10061' when using a sockets function in a tk command

Hi and thanks for reading this. I know what the problem is but I can't figure out how to fix it.
So the problem goes like this (I think), python is trying to run connect() before the user inputs the host name(hostname) therefore python is trying to connect to a blank host('') which in turn causes a [WinError 10061] to happen. I have tried buffering connect() with another function(connect_buffer()), the error kept on happening, even when I added a if statement that set hostname to 'localhost' if hostname was blank(''), but that didn't work either and turned up the same error.
So my Question is how do I fix this?
Here is the error:
Traceback (most recent call last):
File "H:\server\New folder\Tk_cleint.py", line 89, in <module>
setup_confirm_button = tk.Button(window,text = 'Connect', command = setup())
File "H:\server\New folder\Tk_cleint.py", line 18, in setup
create_sock(host, int(port))
File "H:\server\New folder\Tk_cleint.py", line 36, in create_sock
connect(cleintsocket, nhost, nport)
File "H:\server\New folder\Tk_cleint.py", line 27, in connect
self.connect((hostname, connectingport))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
And here is my code
#---Import statments---#
import socket, os, multiprocessing
import tkinter as tk
#---global variables---#
setup = ''
cleintsocket = ''
#---Defs---#
def setup():
global host, port, user
host = setup_host_box.get()
port = setup_port_box.get()
user = setup_user_box.get()
def connect_buffer(self, hostname, connectingport):
connect(self, hostname, connectingport)
def connect(self, hostname, connectingport):
if hostname == '':
hostname = 'localhost'
self.connect((hostname, int(connectingport)))
print('connected')
multiprocessing.Process(target = resv()).start()
def create_sock(nhost, nport):
global cleintsocket
cleintsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connect(cleintsocket, nhost, nport)
def send(username, cleintsock):
'''to send a message'''
usrmsg = (username + ' - ' + chat_msg_box.get()).encode()
cleintsock.send(usrmsg)
def resv(sock):
'''resive subscript, run through mutiprosses module'''
while True:
rmsg = sock.recv(1024).decode()
chat_msg_display_text.insert('end.0.', rmsg)
def chat():
'''loads chat page'''
setup_host_text.pack_forget()
setup_host_box.pack_forget()
setup_port_text.pack_forget()
setup_port_box.pack_forget()
setup_user_text.pack_forget()
setup_user_box.pack_forget()
setup_confirm_button.pack_forget()
chat_msg_display_text.pack()
chat_msg_box.pack()
chat_msg_send_button.pack()
def start():
'''starts the setup page'''
setup_host_text.pack()
setup_host_box.pack()
setup_port_text.pack()
setup_port_box.pack()
setup_user_text.pack()
setup_user_box.pack()
setup_confirm_button.pack()
def send_button_callback():
'''add a buffer to allow time for 'cleintsocket' to be defined in "create_sock()"'''
send(user, cleintsocket)
#---TK Setup---#
#--window setup--#
window = tk.Tk()
window.title('Chat')
window.geometry('600x600')
window.configure(background='#ffffff')
#--connection setup page--#
setup_host_text = tk.Label(window, text = 'Host')
setup_host_box = tk.Entry(window, bg = '#ffffff')
setup_port_text = tk.Label(window, text = 'Port')
setup_port_box = tk.Entry(window, bg = '#ffffff')
setup_user_text = tk.Label(window, text = 'Username')
setup_user_box = tk.Entry(window, bg = '#ffffff')
setup_confirm_button = tk.Button(window,text = 'Connect', command = setup())
#--chat page--#
chat_msg_box = tk.Entry(window, bg='#ffffff')
chat_msg_send_button = tk.Button(window, text = 'send', command = send_button_callback)
chat_msg_display_text = tk.Text(window, width=600, height=500, wrap = 'word')
#--------------#
start()
and here are some links to questions that didn't help:
WinError 10049: The requested address is not valid in its context
Connecting to myself through my public IP through TCP
Webscraping with Python: WinError 10061: Target machine actively refused
Thank you to anyone who helps.
On your setup_confirm_button you're using command = setup() this should be command = setup or command = lambda: setup()
By calling setup() you're actually calling the function instead of setting it as a reference to the function for the command, and it's running your function then instead of on the button click.
The reason using lambda: setup() also works is because lambda creates an anonymous function.
Also, in your multiprocessing process you're likewise calling resv() instead of passing resv this is calling the function with a while loop and blocking the main event loop.

Ruby, Telnet, read multiline response without timeout

I need some hints/help, how can I read multiline response into variable.
My current command results me multiline response but after that I get timeout.
Here's how my connection is setup:
connection = Net::Telnet.new('Host' => host,'Port' => 4800, 'Telnetmode' => false, 'Timeout' => 1)
Here's my request and how I save it:
puts "Weather request\n"
connection.cmd("{weather}"){ |c| print c }
parsed = JSON.parse(str)
puts "#{parsed}\n\n"
And here's the error:
/usr/lib/ruby/1.9.1/net/telnet.rb:558:in `waitfor': timed out while waiting for more data (Timeout::Error)
from /usr/lib/ruby/1.9.1/net/telnet.rb:695:in `cmd'
from ruby_check.rb:37:in `<main>'
My response is multiple JSON lines, like this:
{"City":"Tallinn", "Degrees":"23"}
{"City":"Berlin", "Degrees":"23"}
{"City":"Helsinki", "Degrees":"23"}
{"City":"Stockholm", "Degrees":"23"}
Why the timeout?
The Net::Telnet documentation says:
For some protocols, it will be possible to specify the Prompt option once when you create the Telnet object and use cmd() calls; for others, you will have to specify the response sequence to look for as the Match option to every cmd() call, or call puts() and waitfor() directly; for yet others, you will have to use sysread() instead of waitfor() and parse server responses yourself.
This makes more sense when combined with the Net::Telnet#cmd method's documentation, which says that the method:
sends a string to the host, and reads in all received data until is sees the prompt or other matched sequence.
You're not specifying a custom Prompt or Match option, so #cmd is waiting for something from the server that matches the default Net::Telnet prompt (/[$%#>] \z/n) to indicate the end of the message.
If the message doesn't end with that kind of prompt, then it'll be waiting forever.
Possible solutions
Match the server's prompt
If the server does send some kind of prompt to indicate it's finished sending data and you should type the next command, you can pass a regular expression that matches it to the Net::Telnet initialiser. For example, if the server prompted you with command:, you could use:
connection = Net::Telnet.new(
"Prompt" => /command: \z/,
# …
)
Match the end of the response
If there's no prompt, but the response you're waiting for ends with a specific character sequence, you could explicitly specify the Match option when you call #cmd. For example, if your response was a single JSON array it would end with ], so you might be able to use this:
connection.cmd("String" => "{weather}", "Match" => "]") { |c| print c }
Give up on Net::Telnet and use a TCPSocket
If there's no prompt and no known ending, you could try to use the Net::Telnet object's underlying TCPSocket to read the data without using #cmd:
connection.puts("{weather}")
connection.sock.readline
At this point, there might not be much benefit to using Net::Telnet over a plain TCPSocket.
You are setting the timeout to one second and do not specify what str is. You can try increasing the timeout value or even setting it to false. Believieng it is the result from .cmd, try this:
connection = Net::Telnet.new(
"Host" => host, "Port" => 4800,
"Telnetmode" => false, "Timeout" => false)
puts "Weather request...\n"
str = connection.cmd("{weather}"){ |c| print c }
parsed = JSON.parse(str)
puts "#{parsed}\n\n"

How do I block on reading a named pipe in Ruby?

I'm trying to set up a Ruby script that reads from a named pipe in a loop, blocking until input is available in the pipe.
I have a process that periodically puts debugging events into a named pipe:
# Open the logging pipe
log = File.open("log_pipe", "w+") #'log_pipe' created in shell using mkfifo
...
# An interesting event happens
log.puts "Interesting event #4291 occurred"
log.flush
...
I then want a separate process that will read from this pipe and print events to the console as they happen. I've tried using code like this:
input = File.open("log_pipe", "r+")
while true
puts input.gets #I expect this to block and wait for input
end
# Kill loop with ctrl+c when done
I want the input.gets to block, waiting patiently until new input arrives in the fifo; but instead it immediately reads nil and loops again, scrolling off the top of the console window.
Two things I've tried:
I've opened the input fifo with both "r" and "r+"--I have the same problem either way;
I've tried to determine if my writing process is sending EOF (which I've heard will cause the read fifo to close)--AFAIK it isn't.
SOME CONTEXT:
If it helps, here's a 'big picture' view of what I'm trying to do:
I'm working on a game that runs in RGSS, a Ruby based game engine. Since it doesn't have good integrated debugging, I want to set up a real-time log as the game runs--as events happen in the game, I want messages to show up in a console window on the side. I can send events in the Ruby game code to a named pipe using code similar to the writer code above; I'm now trying to set up a separate process that will wait for events to show up in the pipe and show them on the console as they arrive. I'm not even sure I need Ruby to do this, but it was the first solution I could think of.
Note that I'm using mkfifo from cygwin, which I happened to have installed anyway; I wonder if that might be the source of my trouble.
If it helps anyone, here's exactly what I see in irb with my 'reader' process:
irb(main):001:0> input = File.open("mypipe", "r")
=> #<File:mypipe>
irb(main):002:0> x = input.gets
=> nil
irb(main):003:0> x = input.gets
=> nil
I don't expect the input.gets at 002 and 003 to return immediately--I expect them to block.
I found a solution that avoids using Cygwin's unreliable named pipe implementation entirely. Windows has its own named pipe facility, and there is even a Ruby Gem called win32-pipe that uses it.
Unfortunately, there appears to be no way to use Ruby Gems in an RGSS script; but by dissecting the win32-pipe gem, I was able to incorporate the same idea into an RGSS game. This code is the bare minimum needed to log game events in real time to a back channel, but it can be very useful for deep debugging.
I added a new script page right before 'Main' and added this:
module PipeLogger
# -- Change THIS to change the name of the pipe!
PIPE_NAME = "RGSSPipe"
# Constant Defines
PIPE_DEFAULT_MODE = 0 # Pipe operation mode
PIPE_ACCESS_DUPLEX = 0x00000003 # Pipe open mode
PIPE_UNLIMITED_INSTANCES = 255 # Number of concurrent instances
PIPE_BUFFER_SIZE = 1024 # Size of I/O buffer (1K)
PIPE_TIMEOUT = 5000 # Wait time for buffer (5 secs)
INVALID_HANDLE_VALUE = 0xFFFFFFFF # Retval for bad pipe handle
#-----------------------------------------------------------------------
# make_APIs
#-----------------------------------------------------------------------
def self.make_APIs
$CreateNamedPipe = Win32API.new('kernel32', 'CreateNamedPipe', 'PLLLLLLL', 'L')
$FlushFileBuffers = Win32API.new('kernel32', 'FlushFileBuffers', 'L', 'B')
$DisconnectNamedPipe = Win32API.new('kernel32', 'DisconnectNamedPipe', 'L', 'B')
$WriteFile = Win32API.new('kernel32', 'WriteFile', 'LPLPP', 'B')
$CloseHandle = Win32API.new('kernel32', 'CloseHandle', 'L', 'B')
end
#-----------------------------------------------------------------------
# setup_pipe
#-----------------------------------------------------------------------
def self.setup_pipe
make_APIs
##name = "\\\\.\\pipe\\" + PIPE_NAME
##pipe_mode = PIPE_DEFAULT_MODE
##open_mode = PIPE_ACCESS_DUPLEX
##pipe = nil
##buffer = 0.chr * PIPE_BUFFER_SIZE
##size = 0
##bytes = [0].pack('L')
##pipe = $CreateNamedPipe.call(
##name,
##open_mode,
##pipe_mode,
PIPE_UNLIMITED_INSTANCES,
PIPE_BUFFER_SIZE,
PIPE_BUFFER_SIZE,
PIPE_TIMEOUT,
0
)
if ##pipe == INVALID_HANDLE_VALUE
# If we could not open the pipe, notify the user
# and proceed quietly
print "WARNING -- Unable to create named pipe: " + PIPE_NAME
##pipe = nil
else
# Prompt the user to open the pipe
print "Please launch the RGSSMonitor.rb script"
end
end
#-----------------------------------------------------------------------
# write_to_pipe ('msg' must be a string)
#-----------------------------------------------------------------------
def self.write_to_pipe(msg)
if ##pipe
# Format data
##buffer = msg
##size = msg.size
$WriteFile.call(##pipe, ##buffer, ##buffer.size, ##bytes, 0)
end
end
#------------------------------------------------------------------------
# close_pipe
#------------------------------------------------------------------------
def self.close_pipe
if ##pipe
# Send kill message to RGSSMonitor
##buffer = "!!GAMEOVER!!"
##size = ##buffer.size
$WriteFile.call(##pipe, ##buffer, ##buffer.size, ##bytes, 0)
# Close down the pipe
$FlushFileBuffers.call(##pipe)
$DisconnectNamedPipe.call(##pipe)
$CloseHandle.call(##pipe)
##pipe = nil
end
end
end
To use this, you only need to make sure to call PipeLogger::setup_pipe before writing an event; and call PipeLogger::close_pipe before game exit. (I put the setup call at the start of 'Main', and add an ensure clause to call close_pipe.) After that, you can add a call to PipeLogger::write_to_pipe("msg") at any point in any script with any string for "msg" and write into the pipe.
I have tested this code with RPG Maker XP; it should also work with RPG Maker VX and later.
You will also need something to read FROM the pipe. There are any number of ways to do this, but a simple one is to use a standard Ruby installation, the win32-pipe Ruby Gem, and this script:
require 'rubygems'
require 'win32/pipe'
include Win32
# -- Change THIS to change the name of the pipe!
PIPE_NAME = "RGSSPipe"
Thread.new { loop { sleep 0.01 } } # Allow Ctrl+C
pipe = Pipe::Client.new(PIPE_NAME)
continue = true
while continue
msg = pipe.read.to_s
puts msg
continue = false if msg.chomp == "!!GAMEOVER!!"
end
I use Ruby 1.8.7 for Windows and the win32-pipe gem mentioned above (see here for a good reference on installing gems). Save the above as "RGSSMonitor.rb" and invoke it from the command line as ruby RGSSMonitor.rb.
CAVEATS:
The RGSS code listed above is fragile; in particular, it does not handle failure to open the named pipe. This is not usually an issue on your own development machine, but I would not recommend shipping this code.
I haven't tested it, but I suspect you'll have problems if you write a lot of things to the log without running a process to read the pipe (e.g. RGSSMonitor.rb). A Windows named pipe has a fixed size (I set it here to 1K), and by default writes will block once the pipe is filled (because no process is 'relieving the pressure' by reading from it). Unfortunately, the RPGXP engine will kill a Ruby script that has stopped running for 10 seconds. (I'm told that RPGVX has eliminated this watchdog function--in which case, the game will hang instead of abruptly terminating.)
What's probably happening is the writing process is exiting, and as there are no other writing processes, EOF is sent to the pipe which causes gets to return nil, and so your code loops continually.
To get around this you can usually just open the pipe read-write at the reader end. This works for me (on a Mac), but isn't working for you (you've tried "r" and "r+"). I'm guessing this is to due with Cygwin (POSIX says opening a FIFO read-write is undefined).
An alternative is to open the pipe twice, once read-only and once write-only. You don't use the write-only IO for anything, it's just so that there's always an active writer attached to the pipe so it doesn't get closed.
input = File.open("log_pipe", "r") # note 'r', not 'r+'
keep_open = File.open("log_pipe", "w") # ensure there's always a writer
while true
puts input.gets
end

Resources