1

I've recently gotten my hands on an Arduino (or rather a Genuino, but the overall architecture should be the same) UNO. I'm now using that Arduino to test how data is transmitted from a USB to Serial Connector to the Arduino.

I've already verified that data transmission from Arduino to the PC is possible, by using hTerm and PySerial. However, whenever I send data from Pyserial or hTerm, it doesn't show up in the Arduino serial terminal. Also, I'm using SoftwareSerial to set up a secondary serial terminal to see what the Arduino receives. (I'm using the standard serial port to send data to the terminal, and the SoftwareSerial Port to connect to the USB to Serial Connector.)

Here's my code so far:

Python side:

import serial
import time
import sys
import struct
ser = serial.Serial('COM3', 9600, timeout=0)
time.sleep(10)
while 1:
    print(ser.write(struct.pack('>B', 0)))
    time.sleep(2)
    print(1)
    sys.stdout.flush()

The Arduino side looks like this, and is essentially the default example: https://www.arduino.cc/en/Tutorial/SoftwareSerialExample

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
  // put your setup code here, to run once:
  if (Serial) {
    Serial.end();
  }
  Serial.begin(9600);
  while (!Serial) {
    ;
  }

  Serial.println("Godnight moon!");

  mySerial.begin(9600);
  mySerial.println("Hello, world?");
}

void loop() {
  // put your main code here, to run repeatedly:
  mySerial.listen();
  if (mySerial.available() > 0) {
    Serial.println("Hello User");
    Serial.println(mySerial.read());
  }
}

The Terminal never prints "Hello User". Also, no other signals are being printed. By removing the if-condition, I get a whole lot of "Hello User" and "-1" in the Arduino IDE serial monitor.

Concerning my setup:

I've got an USB to Serial converter, which is connected as "COM3". On the other side, I have an Arduino UNO which is connected via USB to the same computer. The TX pin of the converter is connected to the RX pin of the Arduino and vice versa. Also, Ground is connected to Ground. Furthermore, I use Jupyter/IPython notebook to run my Python script, and the default Arduino IDE to compile programs for my Arduino. In the case there I received data from the Arduino, I used the script specified in the URL provided, which is very similar to the one I'm already showing here. Also, ser.write() is replaced by ser.read(1), so that it reads one byte at a time. My system is Windows 7 (64 Bit).

Edit: Here is an established connection between hTerm (a serial terminal) and the Python script while using com0com as a port emulator. A successful communication between the Python script and hTerm

  • What are you using to connect to the SoftwareSerial pins? – Majenko Aug 10 '16 at 13:34
  • Some cables which came with the Genuino starter kit. RX is connected to TX and TX to RX (USB-Serial to Arduino), and ground to ground – user25642 Aug 10 '16 at 13:38
  • "Some cables" isn't very descriptive. Which TX and RX pins are you connecting to? – Majenko Aug 10 '16 at 13:53
  • Oh, ok. Well, I'm connecting to Pin 10 (RX) and Pin 11 (TX). Or do you mean how I connect on the other end? – user25642 Aug 10 '16 at 14:21
  • OK, that makes more sense to me now. – Majenko Aug 10 '16 at 14:22
1

What do you think should come out?

I'm no expert with Python but you seem to be using struct pack, which I think prepends a number of bytes to the data to generate a 4 byte word. You are using NULL, 0 to pack the structure. When Serial.println() encounter a NULL it will assume that is the end of the string. So you seem to be printing a zero byte string.

Oh and Serial.read() only reads one byte of data, so there is an issue with that too. I suspect that the write call has a similar issue, you normally need to supply a size parameter because its binary data.

Does that make sense?

  • Hi, thanks for the reply. The struct pack has a syntax as follows: The ">B" stands for a little endian byte that's gonna be packed, while the 0 is just an int that's packed into the byte. If I print it out, I get b'\x00' , which means that it's just an empty Byte. Is that what you meant with NULL, or is that another parameter? – user25642 Aug 11 '16 at 9:05
  • And yes, I know that Serial.read(1) reads only one Byte at a time. That was just for testing purposes, as I was sending only one byte at a time. (To see if the connection works at all) The pySerialAPI (pyserial.readthedocs.io/en/latest/pyserial_api.html) states that write only takes one argument, the data. For python 3 it has to be formatted, which is why I'm using the struct package. For a 4 byte word, I would need to use something like ">BBBB", since a single B is just one byte. I hope that clarifies what the code does a bit more :) – user25642 Aug 11 '16 at 9:13
  • Generally, the struct package is used to format variables in python to certain standards. The problem is that a python int only takes up as much space as needed, so it will have a varying length, depending on the size of the number. – user25642 Aug 11 '16 at 9:16
  • A variable length int, what a silly language :) Yes thanks that clarifies a lot. By NULL I do mean 0x00, so if that what you are getting its working. If you print that as a string then it will seem like it is doing nothing. Can you write (0x42) rather than packing it, you should get 0x42 on the Arduino or 'B'. – Code Gorilla Aug 11 '16 at 10:04
  • Ok, so I just used the pack-function to pack b"B" (Which is the bytes representation of a B. Python 3 is Unicode, so that should work) and send it instead of a 0. The Arduino still doesn't display anything. – user25642 Aug 11 '16 at 10:37
1

When you run your python code, do you see the things you are sending on the screen there?

I think you need to be calling ser.open().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.