0

I found this code for a simple chat that uses serial ports to communicate, and I wanted to see it work. I googled for a while and found com0com to simulate the RS-232 connection as I don't have a cable, but now I'm stumped and have no idea how to actually make this work

The code (in python):

from serial import * 
from threading import Thread 

class Receiver(Thread): 
    def __init__(self, serialPort): 
        Thread.__init__(self) 
        self.serialPort = serialPort 
    def run(self): 
        text = "" 
        while (text != "exit\n"): 
            text = serialPort.readline() 
            print ("\n machine1: " + text) 
        self.serialPort.close() 

class Sender(Thread): 
    def __init__(self, serialPort): 
        Thread.__init__(self) 
        self.serialPort = serialPort 
    def run(self): 
        text = "" 
        while(text != "exit\n"): 
            text = raw_input("Type your message>>") + "\n" 
            self.serialPort.write(text) 
        self.serialPort.close() 

serialPort = Serial("\\\\.\\CNCA0")

send = Sender(serialPort) 
receive = Receiver(serialPort) 
send.start() 
receive.start()

Thanks in advance.

2 Answers 2

1

You first need to use com0com to create a serial port with a loop-back, i.e. the output of the port is connected back to the input. This way, everything you send from the Serial Port will received back.

The code snippet uses the Sender class to read the command prompt input. Whatever you write is sent through the serial port. The Receiver class spawns a thread and waits something to be received from the serial port. When a full line is received, it is typed in the command prompt.

Things to notice:

  • Make sure your serial port is actually named CNCA0
  • Press Enter for a message to appear
  • Type 'exit' and Enter to stop the program
Sign up to request clarification or add additional context in comments.

Comments

1

I'm also interested in similar code. Unfortunately till tomorrow I will be unable to test it on 2 computers.

Working only on Tx, On Rx is not working yet.

import serial
from threading import Thread 

serialPort = serial.Serial(port='/dev/ttyUSB0', baudrate=9600)

class Receiver(Thread):
    def __init__(self, serialPort): 
        Thread.__init__(self)
        self.serialPort = serialPort 

    def run(self):
        text = ""
        while (text != "exit\n"):
            text = serialPort.readline()
            print ("\n machine1: " + text)

class Sender(Thread):
    def __init__(self, serialPort):
        Thread.__init__(self)
        self.serialPort = serialPort 

    def run(self):
        text = ""
        while(text != "exit\n"):
            text = raw_input("$:")
            self.serialPort.write(' ' + text + '\n')

send = Sender(serialPort) 
receive = Receiver(serialPort) 
send.start() 
receive.start()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.