Serial received data not recognised - python

I am connecting PIC with Raspberry PI through a serial port. My max485 transmit and receive enable pin is controlled by gpio pin. My received data is printed successfully. But it is not performing the desired function.
while True:
GPIO.output(repin, False)
port.flushInput()
time.sleep(.oo5)
port.write(bytes(address))
time.sleep(.01)
GPIO.output(repin, Ture)
port.flushInput()
data=port.readline()
if data:
address=address+1
if data == "ok" :
print "reached"
print data
if not data:
print "no data"
when I am receiving OK. It prints "ok" but it didn't print "reached". And when data is not received it is printing a blank value instead of "no data".

There is nothing wrong in your code. If you write PIC program correctly then the problem is coming from the proper termination resistors of max485.connect twisted pair cable ground with proper resistance.

Check your UART_Write method at PIC micro-controller side. Are you sending \r or \n extra along with ok ? You can clarify that by using debugger at PIC and monitoring the PIC UART write register in watch window, which is TXREG

Related

Arducam ov2640 camera not taking/returning data on pico

I have an Arducam 2MP plus camera wired to a raspberry pi Pico 2040 running circuitpython, I'm trying to take and store an image onto the Pico (just one), however from what I can tell the camera takes an image but doesn't return any data back to the Pico.
The code I have is modified from the Arducam GitHub example project where mine tries to save the data not sent it over usb cdc (link for the original GitHub post: https://github.com/ArduCAM/PICO_SPI_CAM).
This is the code I have at the moment:
import time as utime
import busio
import board
from Arducam import *
from board import *
once_number=128
mode = 0
start_capture = 1
stop_flag=0
data_in=0
value_command=0
flag_command=0
buffer=bytearray(once_number)
i2c = busio.I2C(board.GP27, board.GP26)
mycam = ArducamClass(OV2640, i2c)
mycam.Camera_Detection()
mycam.Spi_Test()
mycam.Camera_Init()
utime.sleep(1)
mycam.clear_fifo_flag()
mycam.OV2640_set_JPEG_size(OV2640_800x600)
mycam.set_format(JPEG)
mycam.OV2640_set_Light_Mode(Auto)
mycam.OV2640_set_Color_Saturation(Saturation2)
mycam.OV2640_set_Brightness(Brightness2)
mycam.OV2640_set_Contrast(Contrast2)
mycam.OV2640_set_Special_effects(Normal)
def read_fifo_burst():
count=0
lenght=mycam.read_fifo_length()
mycam.SPI_CS_LOW()
mycam.set_fifo_burst()
file = open("file.txt", 'a') # I know this is a txt not jpeg, its so I can see what's being outputed
while True:
mycam.spi.readinto(buffer,start=0,end=once_number)
file.write(buffer)
utime.sleep(0.00015)
count+=once_number
if count+once_number>lenght:
count=lenght-count
mycam.spi.readinto(buffer,start=0,end=count)
file.write(buffer)
mycam.SPI_CS_HIGH()
mycam.clear_fifo_flag()
break
while True:
if start_capture==1:
mycam.flush_fifo();
mycam.clear_fifo_flag();
mycam.start_capture();
start_capture=0
print("here")
if mycam.get_bit(ARDUCHIP_TRIG,CAP_DONE_MASK)!=0: # this line here
read_fifo_burst()
print("done")`
The line labeled 'this line here' doesn't seem to turn to a value that's not 0, if I remove this line I get (this is a printed output, not from the file, that wouldn't open properly):
bytearray(b'\x80\x1b\x04tf\x02\x04\x00\x19\x00\x04\x07\xa0\x10\x18\xc1\r#\x00\x04$\x00\x80\x84\x00\x04(7T\x91\x10X\teB\x00\x00\x01\x81\x01\x18\x00\x16\x00\x00\x10\x08\x00b\x00[*\x80\x80\x00\x00D\x82\x04\x00\x87\xc2\x10\x98\x05\x00BJ\x04\x18\x90\x00\x04 #\x00\x00\t\x10\x04\x00\x0cF#\x03X"\x02f\xa0\x12\x80\x00\x00\x81#*#\x02\x80\x12\x00#\x002F\x11\t\t \x04\x00q\x06\x10\x00\x00\x01\x04\x10\x04\x08 \xc0')
which when I try to decode I get:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte
Does anyone know why the camera isn't sending any image data back (unless the return above is an image?) or why the get_bit() doesnt change from 0?
Also, if anyone has a better way for this to be done please share.
Thanks!
I suggest you to try the followings. I hope they help you:
Make sure the hardware/code requirements are satisfied.
Make sure the SPI is activated whenever you take a photo.
Check the correct pinout and Clock configurations (for instance using STM32CubeIDE. The CAM may have a problem with high-frequency SPI lines and work fine with lower frequencies.
Try to monitor SPI-related pins (MISO, MOSI, CK, NSS) by oscilloscope. It is important to have correct physical signals on the mentioned lines according to the CAM catalog.
Either Pull-up (for MISO, MOSI, and NSS) or pull-down (for CK) resistors must be used with the correct values (kOhms).

How to use an uhf rfid module with python?

I'm trying to read and write data from an RFID tag using python whit this module:
https://es.aliexpress.com/item/32573423210.html
I can connect successfully whit serial but I don't know how to read any tag, because the datasheet from pr9200(the reader that I am working) use this:
Image for pr9200 operation It's like a raw packet whit only hex address that I need to send to the module for it works
my code on python is this:
import serial
ser = serial.Serial(port = "COM27", baudrate=115200, bytesize=8, parity='N', stopbits=1)
while(ser.is_open == True):
rfidtag = ''
incomingByte = ser.read(21)
print(incomingByte)
for i in incomingByte:
rfidtag = rfidtag + hex(i)
Some comments to jump start your coding:
-What you need to do is send a command to your device to ask it to start sending readings in auto mode. To do that you need to use ser.write(command). You can find a good template here.
-To prepare the command you just need to take the raw bytes (those hex values you mentioned) and put them together as, for instance a bytearray.
-The only minor hurdle remaining is to calculate the CRC. There are some nice methods here at SO, just search CRC 16 CCITT.
-Be aware that after writing you can not start immediately waiting for readings, you have to wait first for the device to acknowledge the command. Hint: read 9 bytes.
-Lastly, take a new count of the bytes you will receive for each tag. I think they are 22 instead of 21.
You can use pyembedded python library for this which can give you the tag id.
from pyembedded.rfid_module.rfid import RFID
rfid = RFID(port='COM3', baud_rate=9600)
print(rfid.get_id())
https://pypi.org/project/pyembedded/

Stop reading bytes from serial port (Python/RPi/UART)

This problem is quite complicated. Seemingly I have simple two-way communication beetwen 2 devices where reading side is Raspberry pi 3. I'm trying to transfer file. I'm sending it part by part (5kb part). It looks like:
1) Sending side send first 5kb part (exactly 5136 bytes where first 16 bytes are ID/SIZE/ADD_INFO/CRC)
2) RPi3 read 5136 bytes, and calculate CRC for chunk.
3) RPi3 compare CRC received from sending side and calculated by RPi
4a) If CRC doesn't match I switch lanes from RX to TX line using Dual Bus Buffer Gate With 3-State Outputs and set High State at TX line (I keep it for 30ms).
4b) If CRC match I just wait for next chunk of file
5) Sending side switch lanes too and read my TX state if state is HIGH/1 (when CRC doesn't match) it sends same chunk (retransmission) if state is LOW/0 sends another chunk. (changing state take 10 ms)
On osciloscope it looks like this (4a):
and this (4b):
Beetwen 5136 chunk there is free time to calculate CRC then we have on RPi3 side changing state if we have to because CRC doesn't match (red lines at 4a) and reading TX side from Sending side.
Ok now some simplified code from RPi:
def recv():
while True:
#Set line to low because I want to read data
GPIO.output(16, GPIO.LOW)
ID = ser.read(4)
SIZE = ser.read(4)
ADD_INFO = ser.read(4)
CRC = ser.read(4)
#get crc from sending side
crc_chunk = int.from_bytes(CRC, byteorder='little')
data = ser.read(5120)
#calculating CRC from chunk
rpiCRC = crcSTM(data)
while True:
<--- here I wait about 20ms to calculate CRC
if rpiCRC != crc_chunk:
#Crc doesn't match I want retransmission
GPIO.output(16, GPIO.HIGH)
<--- keep it HIGH for about 30ms
break
else:
#keep it low because crc match and we are waiting for next chunk
GPIO.output(16, GPIO.HIGH)
break
All this looks legit but to the point. I always get only first chunk after that RPi just stop reading bytes. It only happen in case '4b' I get first chunk properly and I'm waiting for next one and then RPi just stop reading bytes or give me just some scratches from time to time. I test what If i get first chunk properly and set retransmission thing but everything looks great and I was getting all the time retransmission of first chunk and get it all the time. I came to this that changing line on sending side when I have LOW/1 state affects on RPi and it just stop reading bytes properly. Don't know why it's messing it and don't know how to fight it. I tryied flushing buffer, closing port before sending side chaning line and open it after it changes line again but all this just do nothing what can I do more ?
P.S This waiting things i do in my own timer but there is no need to put here code like code from sending side because problem is on RPi side.
P.S sorry for chaotic language but I was trying do explane it as simply as i can
Ok I fight it. I have enabled option "Would you like a login shell to be accessible over serial" in sudo raspi-congif. Don't know why this was messing everything but disabling this fix it. This is quite strange because I'm playing with raspberry and serial some time and there was no problem if RPi3 was sending something via uart or when it was just reading without changing lines etc :)

Symbol mc70 encoding barcode data via TCP

I have a barcode scanner motorola symbol mc70, via TCP I am able to send data with this app
But instead of text it gives me something like this:
\x9d(\xe7\xae\xbc#\x94x\x0b\xa0*\xd8\x93\
How am I supposed to print it as normal word?
Thanks to spirine i know now that motorola crypt it's data. Just needed to switch data wedge option in configuration of ipwedge in device

Read data from load cell

Your help is badly needed...
I'm trying to read data and print it to the python console from a load cell. My setup is as follow:
The load cell is a MD type from Eilersen connected to a load cell signal converter of type MCE2040 Seriel Communication Module also from Eilersen. The MCE2040 is connected to my PC through a USB to seriel connector like this link_http://www.usbgear.com/USB-COM-I-SI.html (I'm only allowed two links) one.
The load cell is connected to COM 1.
I have tried to run this snippet:
import serial
ser = serial.Serial(0) # open first serial port
print ser.portstr # check which port was really used
#ser.write("hello") # write a string
ser.close()
...and that prints 'COM1' to the console so I guess my connection should be okay.
My problem is that I don't know how to proceed. In the end I'd like to plot a graph of the incoming data and output a data file with time stamps, but for starters I'd like to print some load cell data to the console.
Any help will be highly appreciated. If further information is needed, please let me know.
Thx in advance.
Edit:
I have some documentation re MCE2040:
3.1 EVC Mode (without time stamp)
Specification: RS232/RS4422
Baudrate: 115200 bps
38400 bps (select with SW1.5)
Data bits: 7
Parity: Even
Stop bits: 1
Protocol: EVC protocol described below (Transmit Only)
3.1.1 EVC Protocol Format
After each sample period a new weight telegram is transmitted. The transmitted telegram has the following format:
<LF>WWWWWWWW<CR>
Each telegram contains a line feed character, a weight result and a carriage return character. The telegram contains:
<LF> Line Feed character (ASCII 0Ah).
WWWWWWWW Weight value for the loadcell. The value is an 8 byte ASCII hex number with MSB first.
<CR> Carriage Return character (ASCII 0Dh).
I was able to get some output from the following code:
import serial
ser = serial.Serial(0, baudrate=115000 ,timeout=100)
print ser.portstr
x = ser.read(50)
print x
ser.close()
print 'close'
Output:
COM1
ÆÆÆÆA0·5
ÆÆÆÆA0·6
ÆÆÆÆA0·5
ÆÆÆÆA0·±
ÆÆÆÆA0·±
close
First of all make sure it's really your com port, since COM1 is used by a lot of computers i'm not sure it's your com port.
You can use a simple wire to loop back info by connecting TX to RX at the USB to Serial converter, it will result in an echo (you will read what you write) it's a very simple way to verify that you are talking with the right com port.
Regarding how to continue:
Useful basic commands:
ser.write("command") with this command you send to the device some command.
ser.read(n) is for read n bytes from the device
ser.readline() will read line until it reached \n (new line)
Steps:
Send a command to your device.
Read all the data by some end byte (Frame Synchronization).
Parse data to structure (list or something like that..)
Plot it to graph.
Useful Links:
pyserial docs
tips for reading serial
plotly for graphs in python

Categories