SPI Code Instability - Raspbian (Debian) Linux on RPi - c++

I am trying to get my software to read and write writing a CAT25512 EEPROM from a Raspberry Pi 3 with Raspbian (Debian) Linux. Before writing to the memory a Write Enable Latch (WEL) has to be set with command 0x06. This write is successful. It is then checked with the read status register command 0x05, which also succeeds. Then the write, read, and successive status read commands get no response and/or fail.
I have tried adding some delays to wait for the HW. I have also restructured the code many times.
I apologize in advance for the complete file, but I'm not sure where the problem lies.
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <string>
#include <cstring>
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define WAIT_FOR_EEPROM(a) do { for (int z=0; z<0x3FFF; z++); } while (eepromBusy((a) != 0x02));
const char *device = "/dev/spidev0.0";
uint8_t mode=SPI_MODE_0;
uint8_t bits=8;
uint32_t baud=500000;
uint8_t buffer[4] = {0};
int transfer(int spi_file, uint8_t* buffer, int length); // Prototype
int eepromBusy(int spi_file) {
buffer[0] = 0x05;
buffer[1] = 0x00;
transfer(spi_file, buffer, 2);
return (buffer[1]);
}
int main() {
int fd = open(device,O_RDWR);
if (fd < 0) printf("can't open device");
int ret;
ret = ioctl(fd, SPI_IOC_WR_MODE, &mode);
if (ret == -1) printf("can't set spi mode");
ret = ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
if (ret == -1) printf("can't set bits!");
ret = ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &baud);
if (ret == -1) printf("can't set speed!");
ret = ioctl(fd, SPI_IOC_RD_MODE, &mode);
if (ret == -1) printf("can't set spi mode");
ret = ioctl(fd, SPI_IOC_RD_BITS_PER_WORD, &bits);
if (ret == -1) printf("can't set bits!");
ret = ioctl(fd, SPI_IOC_RD_MAX_SPEED_HZ, &baud);
if (ret == -1) printf("can't set speed!");
printf("spi mode set as %u\n", mode);
printf("bits per byte set as %u\n", bits);
printf("max speed set at %u\n", baud);
do {
// Write Enable
buffer[0] = 0x06;
transfer(fd, buffer, 1);
// Read Status
buffer[0] = 0x05;
buffer[1] = 0x00;
transfer(fd, buffer, 2);
} while (!(buffer[1] & 0x02));
printf("Status Reg: %x\n", buffer[1]);
WAIT_FOR_EEPROM(fd)
// usleep(100);
// Write Byte
buffer[0] = 0x02;
buffer[1] = 0x00;
buffer[2] = 0x10;
buffer[3] = 0xAA;
transfer(fd, buffer, 4);
/* uint8_t busy = -1;
do {
usleep(50);
buffer[0] = 0x05;
buffer[1] = 0x00;
transfer(fd, buffer, 2);
busy = buffer[1] & 0x01;
} while (busy);
*/
WAIT_FOR_EEPROM(fd)
// Read Byte
buffer[0] = 0x03;
buffer[1] = 0x00;
buffer[2] = 0x10;
buffer[3] = 0x00;
transfer(fd, buffer, 4);
printf("Received byte: %i\n", buffer[3]);
if (close(fd) > 0) printf("can't close device");
return 0;
}
int transfer(int spi_file, uint8_t *buffer, int length) {
//struct spi_ioc_transfer spi[length] = {0};
int ret = -1;
struct spi_ioc_transfer tr[length] = {0};
for (int x=0; x<length; ++x) {
tr[x].tx_buf = (unsigned long)(buffer+x);
tr[x].rx_buf = (unsigned long)(buffer+x);
tr[x].len = sizeof(*(buffer+x));
tr[x].delay_usecs = 0;
tr[x].speed_hz = baud;
tr[x].bits_per_word = bits;
tr[x].cs_change = 0;
};
ret = ioctl(spi_file, SPI_IOC_MESSAGE(length), &tr);
if (ret < 1) printf("Transfer Error!!! First Byte Was: 0x%x\n", buffer[0]);
return ret;
}
I am currently getting the Transfer Error!!! First Byte Was: 0x5 error twice, indicating that the WAIT_FOR_EEPROM(fd) commands are not properly being executed.
In the beginning, the WEL bit is set with command 0x06 and the status is correctly reported as 2. The data read from EEPROM has read out as 0 or 211 depending on how I've tweaked the code. It should be 0xAA (170).
I would appreciate any suggestions.

I guess the transfer() function is responsible to setup and execute one SPI command per call.
Why you are using an array of 'struct spi_ioc_transfer' ?
Why are you loop over the number of bytes in buffer and setup a 'spi_ioc_transfer' structure for each ?
Take a look at https://raw.githubusercontent.com/raspberrypi/linux/rpi-3.10.y/Documentation/spi/spidev_test.c

Related

How to insert hex value and send to serial port?

Below is my code. I want to send hex values and get the output as hex in the hyper terminal. I am not sure how to send it.
I am getting some garbage values in the hyperterminal output. It is reading but not sending the hex output.
#include "stdafx.h"
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <stdint.h>
#define BUFFERLENGTH 256
int main(void)
{
HANDLE hComm; // Handle to the Serial port
char ComPortName[] = "\\\\.\\COM6"; // Name of the Serial port(May Change) to be opened,
BOOL Status;
DWORD dwEventMask; // Event mask to trigger
unsigned char TempChar; // Temperory Character
char SerialBuffer[256]; // Buffer Containing Rxed Data
DWORD NoBytesRead; // Bytes read by ReadFile()
int i = 0;
printf("\n\n +==========================================+");
printf("\n | Serial Communication (Win32 API) |");
printf("\n +==========================================+\n");
/*----------------------------------- Opening the Serial Port --------------------------------------------*/
hComm = CreateFile(ComPortName, // Name of the Port to be Opened
GENERIC_READ | GENERIC_WRITE, // Read/Write Access
0, // No Sharing, ports cant be shared
NULL, // No Security
OPEN_EXISTING, // Open existing port only
0, // Non Overlapped I/O
NULL); // Null for Comm Devices
if (hComm == INVALID_HANDLE_VALUE)
printf("\n Error! - Port %s can't be opened", ComPortName);
else
printf("\n Port %s Opened\n ", ComPortName);
/*------------------------------- Setting the Parameters for the SerialPort ------------------------------*/
DCB dcbSerialParams = { 0 }; // Initializing DCB structure
dcbSerialParams.DCBlength = sizeof(dcbSerialParams);
Status = GetCommState(hComm, &dcbSerialParams); //retreives the current settings
if (Status == FALSE)
printf("\n Error! in GetCommState()");
dcbSerialParams.BaudRate = CBR_9600; // Setting BaudRate = 9600
dcbSerialParams.ByteSize = 8; // Setting ByteSize = 8
dcbSerialParams.StopBits = ONESTOPBIT; // Setting StopBits = 1
dcbSerialParams.Parity = EVENPARITY; // Setting Parity = None
Status = SetCommState(hComm, &dcbSerialParams); //Configuring the port according to settings in DCB
if (Status == FALSE)
{
printf("\n Error! in Setting DCB Structure");
}
else
{
printf("\n Setting DCB Structure Successfull\n");
printf("\n Baudrate = %d", dcbSerialParams.BaudRate);
printf("\n ByteSize = %d", dcbSerialParams.ByteSize);
printf("\n StopBits = %d", dcbSerialParams.StopBits);
printf("\n Parity = %d", dcbSerialParams.Parity);
}
/*------------------------------------ Setting Timeouts --------------------------------------------------*/
while (1)
{
COMMTIMEOUTS timeouts = { 0 };
timeouts.ReadIntervalTimeout = 5000;
timeouts.ReadTotalTimeoutConstant = 5000;
timeouts.ReadTotalTimeoutMultiplier = 1000;
timeouts.WriteTotalTimeoutConstant = 5000;
timeouts.WriteTotalTimeoutMultiplier = 1000;
if (SetCommTimeouts(hComm, &timeouts) == FALSE)
printf("\n Error! in Setting Time Outs");
else
printf("\n\n Setting Serial Port Timeouts Successfull");
printf("\n |--------- Serial Communication (Win32 API) --------|");
printf("Starting to write......");
//char lpBuffer[] = "ABC"; // lpBuffer should be char or byte array, otherwise write wil fail
uint8_t message[15];
message[0] = 0x16;
message[1] = 0x16;
message[2] = 0x02;
message[3] = 0x01;
message[4] = 0x07;
message[5] = 0x08;
message[6] = 0x00;
message[7] = 0xFF;
message[8] = 0xFF;
message[9] = 0x01;
message[10] = 0x00;
message[11] = 0x01;
message[12] = 0x00;
message[13] = 0xFF;
message[14] = 0xFF;
//byte(bytestosend)[15] = { message[0], message[1], message[2], message[3], message[4], message[5], message[6], message[7], message[8], message[9],message[10], message[11],message[12], message[13], message[14] };
DWORD dNoOFBytestoWrite; // No of bytes to write into the port
DWORD dNoOfBytesWritten = 0; // No of bytes written to the port
dNoOFBytestoWrite = sizeof(message); // Calculating the no of bytes to write into the port
Status = WriteFile(hComm, // Handle to the Serialport
message, // Data to be written to the port
dNoOFBytestoWrite, // No of bytes to write into the port
&dNoOfBytesWritten, // No of bytes written to the port
NULL);
if (Status == TRUE)
printf("\n\n %X %X %X %X %X %X %X %X %X %X %X %X %X %X %X- Written to %s", message[0], int(message[1]), int(message[2]), int(message[3]), int(message[4]), int(message[5]), int(message[6]), int(message[7]), int(message[8]), int(message[9]), int(message[10]), int(message[11]), int(message[12]), int(message[13]), int(message[14]), ComPortName);
else
printf("\n\n Error %d in Writing to Serial Port", GetLastError());
int k;
for (k = 0; k < 50; k++)
{
printf("");
}
/*-----------------------------------Read --------------------------------------------*/
int l;
for (l = 0; l < 50; l++)
{
printf("");
}
printf("\n\n Waiting for Data Reception");
dwEventMask = 1;
//Status = WaitCommEvent(hComm, &dwEventMask, NULL); //Wait for the character to be received
/*-------------------------- Program will Wait here till a Character is received ------------------------*/
if (Status == FALSE)
{
printf("\n Error! in Setting WaitCommEvent()");
}
else //If WaitCommEvent()==True Read the RXed data using ReadFile();
{
printf("\n\n Characters Received");
do
{
//byte(TempChar)[15] = { message[0], message[1], message[2], message[3], message[4], message[5], message[6], message[7], message[8], message[9],message[10], message[11],message[12], message[13], message[14] };
//if(! ReadFile(hComm, &TempChar, sizeof(TempChar), &NoBytesRead, NULL))
if (!ReadFile(hComm, &TempChar, sizeof(TempChar), &NoBytesRead, NULL))
///* ReadFile(
// _In_ HANDLE hFile,
// _Out_writes_bytes_to_opt_(nNumberOfBytesToRead, *lpNumberOfBytesRead) __out_data_source(FILE) LPVOID lpBuffer,
// _In_ DWORD nNumberOfBytesToRead,
// _Out_opt_ LPDWORD lpNumberOfBytesRead,
// _Inout_opt_ LPOVERLAPPED lpOverlapped
// );*/
//if (!ReadFile(hComm, SerialBuffer, BUFFERLENGTH, &NoBytesRead, NULL))
{
printf("wrong character" );
}
//printf("/n /n %X %X %X %X %X %X %X %X %X %X %X %X %X %X %X", int(TempChar[0]), int(TempChar[1]), int(TempChar[2]), int(TempChar[3]), int(TempChar[4]), int(TempChar[5]), int(TempChar[6]), int(TempChar[7]), int(TempChar[8]), int(TempChar[9]), int(TempChar[10]), int(TempChar[11]), int(TempChar[12]), int(TempChar[13]), int(TempChar[14]));
SerialBuffer[i] = TempChar;
//printf("%X is the read", SerialBuffer[i]);
i++;
} while (NoBytesRead > 0);
/*------------Printing the RXed String to Console----------------------*/
printf("\n\n ");
int j = 0;
for (j = 0; j < i - 1; j++) // j < i-1 to remove the dupliated last character
printf("%X are the values read", SerialBuffer[j]);
}
}
CloseHandle(hComm);//Closing the Serial Port
printf("\n ==========================================\n");
_getch();
}
I am giving this as input 16 16 02 01 07 08 FF FF 01 00 01 00 FF FF values, and I want the same to be read in hyperterminal. But I am getting some garbage values. I want the same hex values to be in the hyperterminal.
At very first: What you send is just data. There is no such thing as decimal or hexadecimal data, all that you have is binary data, i. e. the bits stored in groups of (typically) 8 for your bytes. Decimal or hexadecimal literals are just different representations for one and the same (binary) values:
char a[] = { 0x61, 0x62, 0x63, 0 };
char b[] = { 97, 98, 99, 0 };
a and b will now contain exactly the same values, which happen, if interpreted as ASCII characters, to represent the string "abc"...
If, however, your intention is to represent arbitrary binary data in a human readable format (as Intel HEX files do), then you would need to represent each data byte by two chars, e. g. convert the data byte containing 'N', having ASCII value of 78, into a byte sequence of '4', 'e' or alternatively '4', 'E'.
However, this will double your communication overhead, so I'd do the conversion only after receiving the data:
std::vector<char> received = readFromSerial();
// assuming arbitrary data..
for(auto c : received)
std::cout << std::setfill('0') << std::hex << std::setw(2)
<< static_cast<unsigned int>(static_cast<unsigned char>(c)) << std::endl;
Side note: you need the double cast to prevent sign extension for negative characters (at least if char is signed).
If you should prefer signed or unsigned char depends on if you want to represent data in the range of [-128;127] or of [0;255] respectively...

error code 1024 tftp server

I am writing a tftp server in C and testing it with the tftp command on the terminal. However, for most of the times, I receive something liske the following when I try to send RRQ:
tftp> get a.txt
sent RRQ <file=a.txt, mode=netascii>
received ERROR <code=4, msg=>
Error code 1024:
other barly happend case includes:
tftp> get a.txt
sent RRQ <file=a.txt, mode=netascii>
received DATA <block=20334, 512 bytes>
discarded 4 packets
and this one: this might look correct but it barely happened. The text file I use to test it is of 857 bytes.
sent ACK <block=1>
received DATA <block=1, 512 bytes>
sent ACK <block=1>
received DATA <block=2, 345 bytes>
Received 857 bytes in 0.0 seconds
and here is part of my code
Here buffer is a char array of size 512 , to simplify the code, I have removed some of the error handling code
Thanks to whoever may help
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
#include <sys/wait.h>
#include <cerrno>
//define opcode for later use
enum opcode {
RRQ = 1,
WRQ,
DATA,
ACK,
ERROR
};
void handle_write(struct sockaddr_in * sock_info, char* filename, int BUF_LEN){
//printf("Received write request + %d\n", WRQ);
return;
}
void handle_error(unsigned short int * opcode_ptr, char* buffer, socklen_t sockaddr_len, int server_socket, struct sockaddr_in * sock_info, const char* errormsg){
ssize_t n;
*opcode_ptr = htons(ERROR);
*(opcode_ptr + 1) = htons(1);
*(buffer + 4) = 0;
//memcpy(buffer+4, errormsg, strlen(errormsg));
intr_send:
n = sendto(server_socket, buffer, 5, 0,
(struct sockaddr *)sock_info, sockaddr_len);
if(n < 0) {
if(errno == EINTR) goto intr_send;
perror(errormsg);
exit(-1);
}
return;
}
int main() {
int BUF_LEN = 516;
ssize_t n;
char buffer[BUF_LEN];
socklen_t sockaddr_len;
int server_socket;
struct sigaction act;
unsigned short int opcode;
unsigned short int * opcode_ptr;
struct sockaddr_in sock_info;
memset(&sock_info, 0, sockaddr_len);
//----------setup the server----------------//
sock_info.sin_addr.s_addr = htonl(INADDR_ANY);
sock_info.sin_port = htons(5743);
sock_info.sin_family = PF_INET;
if((server_socket = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket");
exit(-1);
}
sockaddr_len = sizeof(sock_info);
if(bind(server_socket, (struct sockaddr *)&sock_info, sockaddr_len) < 0) {
perror("bind");
exit(-1);
}
getsockname(server_socket, (struct sockaddr *)&sock_info, &sockaddr_len);
printf("UDP server listening on port: %d\n", ntohs(sock_info.sin_port));
//----------setup the server----------------//
while(1){
intr_recv:
n = recvfrom(server_socket, buffer, BUF_LEN, 0, (struct sockaddr *)&sock_info, &sockaddr_len);
if(n < 0) {
if(errno == EINTR) goto intr_recv;
perror("recvfrom()");
exit(-1);
}
opcode_ptr = (unsigned short int *)buffer;
opcode = ntohs(*opcode_ptr); //the opcode will be either RRQ or WRQ according to the test
if(opcode != RRQ && opcode != WRQ) {
/* Illegal TFTP Operation */
handle_error(opcode_ptr, buffer, sockaddr_len, server_socket, &sock_info, "invalid command");
}
else {
if(fork() == 0) {
/* Child - handle the request */
FILE* fd;
char* filename;
filename = strdup(buffer+2); //this is the filename to be read (i.e. a.txt)
printf("request received\n");
char data[512];
//----------------------------------handle read request-------------------------------------//
if(opcode == RRQ){
int blocknumber = 0;
int i = 0; //counter for loop
fd = fopen(filename, "r");
free(filename);
//uint8_t data[512];
ssize_t datalen, n;
int done = 0; //this is a boolean indicator that indicates whether the packet transfering process is done or not.
while(!done){
datalen = fread(data, 1, 512, fd);
blocknumber++;
if(datalen < 512){
done = 1; //according to rfc 1350, the last packet will have a data length less than 512 bytes.
}
//for(i = 5; i > 0; i--){
*opcode_ptr = htons(DATA);
opcode = ntohs(*opcode_ptr);
*(opcode_ptr + 1) = htons(blocknumber);
memcpy(buffer + 4, data, datalen);
buffer[datalen + 4] = '\0';
//*(buffer + 4) = 0;
//printf("%d %s\n", datalen, buffer+2);
n = sendto(server_socket, buffer, 4 + datalen, 0, (struct sockaddr *)&sock_info, sockaddr_len);
if(n < 0){
perror("sendto() failed");
exit(-1);
}
//printf("done %d\n", done);
//char buffer[512];
n = recvfrom(server_socket, buffer, sizeof(buffer), 0, (struct sockaddr *)&sock_info, &sockaddr_len);
opcode_ptr = (unsigned short int *)buffer;
opcode = ntohs(*opcode_ptr);
if(n >= 0 && n < 4){
//handle_error(opcode_ptr, buffer, sockaddr_len, server_socket, &sock_info, "invalid request size");
}
if(n > 4){
break;
}
//}
//if(i != 0){
// printf("Transfer timeout!\n");
// exit(1);
//}
//printf("opcode is %d\n", opcode);
if(opcode == ERROR){
printf("Error received\n");
exit(1);
}
if(opcode != ACK){
printf("Invalid message received\n");
exit(1);
}
}
}
//----------------------------------handle read request-------------------------------------//
//----------------------------------handle write request------------------------------------//
//----------------------------------handle write request------------------------------------//
close(server_socket);
break;
}
else {
/* Parent - continue to wait */
}
}
}
return EXIT_SUCCESS;
}
I read your code but not tested by executing.
Compareing with RFC 1350, what I found are
The data field is upto 512 bytes, so 512-byte buffer is not enough because there are no room for the header (Opcode and Block #). You need at least 4 more bytes.
You write the data from buffer + 2 via memcpy(). This should destroy the block number. It seems buffer + 4 should be used instead.
buffer[datalen + 2] = '\0'; shouldn't be needed. I think you should remove it because it will destroy the data or cause buffer overrun.
You should close the file opened after handling read request.

Why my checksum(crc16) in uart is not matched?

I created xmodem used in UART.
This protocol have checksum of data payload in uint16_t.
I tested this part of code showing below, but it is still stucked.
Showing below in the picture.
Received chuck and another checksum calculated from the data payload is matched on the first,but failed on the second
Why can not be matched the received chuck and another checksum calculated from the data payload on the second?
This is a part of code calculating checksum.
Sender of xmodem.(xymodem_send)
chunk.crc = swap16(crc16(chunk.payload, sizeof(chunk.payload)));
Receiver of xmodem.(xmodem_receive)
uint16_t recChksum;
uint16_t recCRC;
// recData is data payload
ret = read(serial_fd, recData, sizeof(recData));
printf("Data buffer is %c\n", &recData);
fflush(stdout);
// processing up to 1024bytes
if (ret != sizeof(recData)) {
rec_chunk_data += ret;
while(rec_chunk_data < sizeof(recData)){
ret = read(serial_fd, recData + (sizeof(uint8_t) * rec_chunk_data), (sizeof(recData) - rec_chunk_data));
rec_chunk_data += ret;
printf("ret is proceeding: %d\n", ret);
fflush(stdout);
}
}
ret = read(serial_fd, &recChksum, sizeof(recChksum));
printf("Check sum is %d\n", recChksum);
fflush(stdout);
// Calculating checksum from data payload
recCRC = swap16(crc16(recData, sizeof(recData)));
// data integrity check
if(recChksum != recCRC){
printf("Check sum is %d and %d\n", recChksum, swap16(crc16(recData, sizeof(recData))));
perror("swap16");
return -errno;
}
And, This is full source code.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string>
#define X_STX 0x02
#define X_ACK 0x06
#define X_NAK 0x15
#define X_EOF 0x04
#define MAX_RETRY (9)
#define min(a, b) ((a) < (b) ? (a) : (b))
struct xmodem_chunk {
uint8_t start;
uint8_t block;
uint8_t block_neg;
uint8_t payload[1024];
uint16_t crc;
} __attribute__((packed));
void printArray(const uint8_t *ptr, size_t length)
{
//for statment to print values using array
printf("*****This is payload*****\n");
size_t i = 0;
for( ; i < length; ++i )
printf("%c", ptr[i]);
printf("*****This is payload*****\n");
fflush(stdout);
}
#define CRC_POLY 0x1021
static uint16_t crc_update(uint16_t crc_in, int incr)
{
uint16_t xor1 = crc_in >> 15;
uint16_t out1 = crc_in << 1;
if(incr)
out1++;
if(xor1)
out1 ^= CRC_POLY; // xor 0b1000000100001
return out1;
}
static uint16_t crc16(const uint8_t *data, uint16_t size)
{
uint16_t crc, i;
printf("data size :%d\n",size);
printArray(data, size); // debug
for (crc = 0; size > 0; size--, data++)
for (i = 0x80; i; i >>= 1)
crc = crc_update(crc, *data & i);
for (i = 0; i < 16; i++)
crc = crc_update(crc, 0);
return crc;
}
static uint16_t swap16(uint16_t in)
{
return (in >> 8) | ((in & 0xff) << 8);
}
enum {
PROTOCOL_XMODEM,
PROTOCOL_YMODEM,
};
static int xmodem_receive(int serial_fd, char* filename, int _crc, int use_crc){
printf("serial_fd is ....%d\n", serial_fd);
fflush(stdout);
int skip = 0;
uint8_t sdCRC = 'C'; // Request-To-Send
uint8_t sdACK = X_ACK; //
uint8_t eof = X_EOF;
uint8_t stx = X_STX;
uint8_t sdNAK = X_NAK;
uint8_t recSTX; // receive SOH from chunk
uint8_t recBlk; // receive blk from chunk
uint8_t recNegBlk; // receive blk negative from chunk
uint8_t recData[1024]; // receive data from chunk
uint16_t recChksum;
uint16_t recCRC;
uint8_t heads_or_tails;
uint8_t expected_blkno;
int rec_chunk_data = 0;
FILE *fp;
int ret, fd;
struct stat stat;
// If we want to receive, We have to send NAK to Sendor.
if (use_crc)
use_crc = MAX_RETRY + 1;
/* Writing as binary */
fp = fopen(filename, "wb");
//Send NAK still read SOH that header of one data chunks
/*
CRC 16
Sending C
Sending NAK
*/
while(use_crc){
char status;
printf("Waiting for sender ping ...");
fflush(stdout);
//
if(_crc){
printf("Send C ping....\n");
ret = write(serial_fd, &sdCRC, sizeof(sdCRC));
}else{
// send NAK before read SOH
printf("Send NAK ping....\n");
ret = write(serial_fd, &sdNAK, sizeof(sdNAK));
} // after sending NAK,receiving SOH from chunk
fflush(stdout);
ret = read(serial_fd, &recSTX, sizeof(recSTX));
printf("serial_fd is ....%d\n", serial_fd);
fflush(stdout);
if(recSTX == X_STX){
printf("STX is %c::%c\n", &recSTX, X_STX);
break;
}else{
printf("Garabage payload %c\n", &recSTX);
}
fflush(stdout);
if (ret != sizeof(recSTX)) {
printf("Not working");
fflush(stdout);
perror("read");
return -errno;
}
use_crc--;
}
expected_blkno = 1;
while(ret != eof){
memset(recData, 0, sizeof(recData));
//So next, receiving blk
ret = read(serial_fd, &recBlk, sizeof(recBlk));
printf("serial_fd is ....%d\n", serial_fd);
fflush(stdout);
printf("ret is proceeding: %d\n", ret);
fflush(stdout);
printf("Block Num is %d\n", recBlk);
fflush(stdout);
if (ret != sizeof(recBlk)) {
perror("read");
return -errno;
}
ret = read(serial_fd, &recNegBlk, sizeof(recNegBlk));
printf("ret is proceeding: %d\n", ret);
fflush(stdout);
printf("serial_fd is ....%d\n", serial_fd);
fflush(stdout);
printf("Negative Block Num is %d\n", recNegBlk);
fflush(stdout);
if (ret != sizeof(recNegBlk)) {
perror("read");
return -errno;
}
ret = read(serial_fd, recData, sizeof(recData));
printf("Data buffer is %c\n", &recData);
fflush(stdout);
if (ret != sizeof(recData)) {
rec_chunk_data += ret;
while(rec_chunk_data < sizeof(recData)){
ret = read(serial_fd, recData + (sizeof(uint8_t) * rec_chunk_data), (sizeof(recData) - rec_chunk_data));
rec_chunk_data += ret;
printf("ret is proceeding: %d\n", ret);
fflush(stdout);
}
}
printf("ret is proceeding: %d\n", ret);
fflush(stdout);
ret = read(serial_fd, &recChksum, sizeof(recChksum));
printf("ret is proceeding: %d\n", ret);
fflush(stdout);
printf("Check sum is %d\n", recChksum);
fflush(stdout);
if (ret != sizeof(recChksum)) {
printf("Can't fetch the Check sum");
perror("read");
return -errno;
}
// data block number check
if(recBlk == 0xff - recNegBlk){
printf("valid block num :%d and %d\n", recBlk, recNegBlk);
fflush(stdout);
}
printf("serial_fd is ....%d\n", serial_fd);
fflush(stdout);
recCRC = swap16(crc16(recData, sizeof(recData)));
// data integrity check
if(recChksum != recCRC){
printf("Check sum is %d and %d, %d\n", recChksum, swap16(crc16(recData, sizeof(recData))), recCRC);
perror("swap16");
return -errno;
}
// check of appended "0xff" from end of data to end of chunk in chunk
unsigned char *ptr = recData;
ptr += sizeof(recData);
while(*ptr == 0xff){
ptr--;
}
fwrite(recData, (ptr - recData),1,fp); // write Datas bytes from our buffer
// set skip flag or end connect
ret = write(serial_fd, &sdACK, sizeof(sdACK));
ret = read(serial_fd, &heads_or_tails, sizeof(heads_or_tails));
if (ret != sizeof(eof) && ret != sizeof(X_STX)){
printf("sizeof eof :%d\n", sizeof(eof));
printf("sizeof STX :%d\n", sizeof(X_STX));
printf("sizeof uint8_t :%d\n", sizeof(uint8_t));
perror("STX EOF length check error");
return -errno;
}else{
if(heads_or_tails == stx){
printf("next chunk\n");
fflush(stdout);
expected_blkno++;
}else if(heads_or_tails == eof){
printf("EOF ...");
fflush(stdout);
break;
}else{
ret = write(serial_fd, &sdNAK, sizeof(sdNAK));
printf("Interruption\n");
fflush(stdout);
}
}
}
printf("done.\n");
fclose(fp);
return 0;
}
static int xymodem_send(int serial_fd, const char *filename, int protocol, int wait)
{
size_t len;
int ret, fd;
uint8_t answer;
struct stat stat;
const uint8_t *buf;
uint8_t eof = X_EOF;
struct xmodem_chunk chunk;
int skip_payload = 0;
fd = open(filename, O_RDONLY);
if (fd < 0) {
perror("open");
return -errno;
}
fstat(fd, &stat);
len = stat.st_size;
buf = static_cast<uint8_t*>(mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0));
if (!buf) {
perror("mmap");
return -errno;
}
if (wait) {
printf("Waiting for receiver ping ...");
fflush(stdout);
do {
ret = read(serial_fd, &answer, sizeof(answer));
if (ret != sizeof(answer)) {
perror("read");
return -errno;
}
} while (answer != 'C');
printf("done.\n");
fflush(stdout);
}
if (protocol == PROTOCOL_YMODEM) {
strncpy((char *) chunk.payload, filename, sizeof(chunk.payload));
chunk.block = 0;
skip_payload = 1;
} else {
chunk.block = 1;
}
chunk.start = X_STX;
int loops = 0;
printf("STX Setted.\n");
fflush(stdout);
while (len) {
printf(" round %d\n", loops);
fflush(stdout);
size_t z = 0;
int next = 0;
char status;
if (!skip_payload) {
z = min(len, sizeof(chunk.payload));
memcpy(chunk.payload, buf, z);
memset(chunk.payload + z, 0xff, sizeof(chunk.payload) - z);
printf("file chunk: %c\n", chunk.payload);
fflush(stdout);
} else {
skip_payload = 0;
}
printf("%d\n", swap16(crc16(chunk.payload, sizeof(chunk.payload))));
fflush(stdout);
chunk.crc = swap16(crc16(chunk.payload, sizeof(chunk.payload)));
chunk.block_neg = 0xff - chunk.block;
ret = write(serial_fd, &chunk, sizeof(chunk));
if (ret != sizeof(chunk))
return -errno;
ret = read(serial_fd, &answer, sizeof(answer));
if (ret != sizeof(answer))
return -errno;
switch (answer) {
case X_NAK:
status = 'N';
break;
case X_ACK:
status = '.';
next = 1;
break;
default:
status = '?';
break;
}
printf("%c", status);
fflush(stdout);
if (next) {
chunk.block++;
len -= z;
buf += z;
}
}
ret = write(serial_fd, &eof, sizeof(eof));
if (ret != sizeof(eof))
return -errno;
/* send EOT again for YMODEM */
if (protocol == PROTOCOL_YMODEM) {
ret = write(serial_fd, &eof, sizeof(eof));
if (ret != sizeof(eof))
return -errno;
}
printf("done.\n");
return 0;
}
static int open_serial(const char *path, int baud)
{
int fd;
struct termios tty;
fd = open(path, O_RDWR | O_SYNC);
if (fd < 0) {
perror("open");
return -errno;
}
memset(&tty, 0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0) {
perror("tcgetattr");
return -errno;
}
cfsetospeed(&tty, baud);
cfsetispeed(&tty, baud);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
perror("tcsetattr");
return -errno;
}
return fd;
}
static void dump_serial(int serial_fd)
{
char in;
for (;;) {
read(serial_fd, &in, sizeof(in));
printf("%c", in);
fflush(stdout);
}
}
#ifdef TEST_XMODEM_RECEIVE
#endif // TEST_XMODEM_RECEIVE
int main(int argc, char **argv)
{
int a, ret, serial_fd;
std::string devs;
printf("args:3: %s\n", argv[3]);
printf("STX: %c\n", X_STX);
printf("ACK: %c\n", X_ACK);
printf("NAK: %c\n", X_NAK);
printf("EOF: %c\n", X_EOF);
fflush(stdout);
if(std::string(argv[3]) == "1"){
devs = "/dev/ttyUSB0";
}else{
devs = "/dev/ttyAMA0";
}
printf("devs: %s\n", devs.c_str());
fflush(stdout);
serial_fd = open_serial( devs.c_str(), 1200);
if (serial_fd < 0)
return -errno;
if(std::string(argv[2]) == "0"){
ret = xymodem_send(serial_fd, argv[1], PROTOCOL_XMODEM, 1);
if (ret < 0)
return ret;
}
if(std::string(argv[2]) == "1"){
ret = xmodem_receive(serial_fd, argv[1], 1, 1);
if (ret < 0)
return ret;
}
if(std::string(argv[2]) == "3"){
dump_serial(serial_fd);
if (ret < 0)
return ret;
}
return 0;
}
Postscript:
1)I send text.txt in Ubuntu,and receive text.txt in Raspibian jessie.
Showing below text is text.txt in Ubuntu
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaasssssssssss
ssssssssssssssssssssd
and,This is in Raspibian.
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaasssssssssss
ssssssssssssssssssssd
ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ
2)Changing "Receiver of xmodem" such as....
recCRC = swap16(crc16(recData, sizeof(recData)));
// data integrity check
if(recChksum /*!= recCRC*/){
printf("Check sum is %d and %d, %d\n", recChksum, swap16(crc16(recData, sizeof(recData))), recCRC);
perror("swap16");
return -errno;
}
are generates the correct checksum showing below
3)Too long text exceed 1024 bytes is only able to output a part of itself.
Unless dumping the serial by "dump_serial" function, I can't make sure of others.
4)Lorem ipsum text of sender showing https://justpaste.it/12eoe is 9 rows, but receiver is only 3 rows https://justpaste.it/12epe and cut off.
Receiver payload is https://justpaste.it/12epk.

My Implementation of xmodem receiving function is not working

I would like to ask for an advice about my code and show me implementing model of yours.
I am implementing program in the code below, and make this code the basis.
Now, I implemented receiver function for xmodem, but it's not working.
"xmodem_receive" called in main function just once printed "Sending C ping ...." as printf and , finished.
Sending xmodem is working as being shown below.
Receving Xmodem is not working.It finished before receiving data.
So I would like to ask
1)Why my xmodem receive function is not working?
2)Why STX is present as "K" ,and that could go through IF statement
if(recSTX == X_STX){
printf("STX is %c\n", &recSTX);
}else{
printf("Garabage payload %c\n", &recSTX);
}
This is my implementation function
static int xmodem_receive(int serial_fd, char* filename, int _crc){
int skip = 0;
uint8_t sdCRC = 'C';
uint8_t sdACK = X_ACK;
uint8_t eof = X_EOF;
uint8_t sdNAK = X_NAK;
uint8_t recSTX; // receive SOH from chunk
uint8_t recBlk; // receive blk from chunk
uint8_t recNegBlk; // receive blk negative from chunk
uint8_t recData[1024]; // receive data from chunk
uint16_t recChksum;
FILE *fp;
int ret, fd;
struct stat stat;
// If we want to receive, We have to send NAK to Sendor.
/* Writing as binary */
fp = fopen(filename, "wb");
//Send NAK still read SOH that header of one data chunks
while(1){
char status;
printf("Waiting for sender ping ...");
fflush(stdout);
//
do {
if(_crc){
printf("Send C ping....\n");
ret = write(serial_fd, &sdCRC, sizeof(sdCRC));
}else{
// send NAK before read SOH
printf("Send NAK ping....\n");
ret = write(serial_fd, &sdNAK, sizeof(sdNAK));
} // after sending NAK,receiving SOH from chunk
fflush(stdout);
ret = read(serial_fd, &recSTX, sizeof(recSTX));
if(recSTX == X_STX){
printf("STX is %c\n", &recSTX);
}else{
printf("Garabage payload %c\n", &recSTX);
}
fflush(stdout);
if (ret != sizeof(recSTX)) {
printf("Not working");
fflush(stdout);
perror("read");
return -errno;
}
} while (recSTX != X_STX);
//So next, receiving blk
ret = read(serial_fd, &recBlk, sizeof(recBlk));
printf("Block Num is %d\n", recBlk);
fflush(stdout);
if (ret != sizeof(recBlk)) {
perror("read");
return -errno;
}
ret = read(serial_fd, &recNegBlk, sizeof(recNegBlk));
printf("Negative Block Num is %d\n", recNegBlk);
fflush(stdout);
if (ret != sizeof(recNegBlk)) {
perror("read");
return -errno;
}
ret = read(serial_fd, (void *)&recData, sizeof(recData));
printf("Data buffer is %c\n", &recData);
fflush(stdout);
if (ret != sizeof(recData)) {
perror("read");
return -errno;
}
ret = read(serial_fd, &recChksum, sizeof(recChksum));
printf("Check sum is %c", &recChksum);
fflush(stdout);
if (ret != sizeof(recChksum)) {
perror("read");
return -errno;
}
// data block number check
if(recBlk == 0xff - recNegBlk){
perror("read");
return -errno;
}
// data integrity check
if(recChksum == swap16(crc16(recData, sizeof(recData)))){
perror("read");
return -errno;
}
// check of appended "0xff" from end of data to end of chunk in chunk
unsigned char *ptr = recData;
ptr += sizeof(recData);
while(*ptr == 0xff){
ptr--;
}
fwrite(recData, (ptr - recData),1,fp); // write Datas bytes from our buffer
// set skip flag or end connect
ret = write(serial_fd, &eof, sizeof(uint8_t));
if (ret != sizeof(eof) || ret != sizeof(X_STX)){
return -errno;
}else{
if(ret == X_STX){
skip = 1;
}else if(ret == EOF){
printf("EOF ...");
ret = write(serial_fd, &sdACK, sizeof(sdACK));
break;
}else{
return -errno;
}
}
}
printf("done.\n");
fclose(fp);
return 0;
}
And This is full code.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string>
#define X_STX 0x02
#define X_ACK 0x06
#define X_NAK 0x15
#define X_EOF 0x04
#define min(a, b) ((a) < (b) ? (a) : (b))
struct xmodem_chunk {
uint8_t start;
uint8_t block;
uint8_t block_neg;
uint8_t payload[1024];
uint16_t crc;
} __attribute__((packed));
#define CRC_POLY 0x1021
static uint16_t crc_update(uint16_t crc_in, int incr)
{
uint16_t xor1 = crc_in >> 15;
uint16_t out1 = crc_in << 1;
if(incr)
out1++;
if(xor1)
out1 ^= CRC_POLY; // xor 0b1000000100001
return out1;
}
static uint16_t crc16(const uint8_t *data, uint16_t size)
{
uint16_t crc, i;
for (crc = 0; size > 0; size--, data++)
for (i = 0x80; i; i >>= 1)
crc = crc_update(crc, *data & i);
for (i = 0; i < 16; i++)
crc = crc_update(crc, 0);
return crc;
}
static uint16_t swap16(uint16_t in)
{
return (in >> 8) | ((in & 0xff) << 8);
}
enum {
PROTOCOL_XMODEM,
PROTOCOL_YMODEM,
};
static int xmodem_receive(int serial_fd, char* filename, int _crc){
int skip = 0;
uint8_t sdCRC = 'C';
uint8_t sdACK = X_ACK;
uint8_t eof = X_EOF;
uint8_t sdNAK = X_NAK;
uint8_t recSTX; // receive SOH from chunk
uint8_t recBlk; // receive blk from chunk
uint8_t recNegBlk; // receive blk negative from chunk
uint8_t recData[1024]; // receive data from chunk
uint16_t recChksum;
FILE *fp;
int ret, fd;
struct stat stat;
// If we want to receive, We have to send NAK to Sendor.
/* Writing as binary */
fp = fopen(filename, "wb");
//Send NAK still read SOH that header of one data chunks
while(1){
char status;
printf("Waiting for sender ping ...");
fflush(stdout);
//
do {
if(_crc){
printf("Send C ping....\n");
ret = write(serial_fd, &sdCRC, sizeof(sdCRC));
}else{
// send NAK before read SOH
printf("Send NAK ping....\n");
ret = write(serial_fd, &sdNAK, sizeof(sdNAK));
} // after sending NAK,receiving SOH from chunk
fflush(stdout);
ret = read(serial_fd, &recSTX, sizeof(recSTX));
if(recSTX == X_STX){
printf("STX is %c\n", &recSTX);
}else{
printf("Garabage payload %c\n", &recSTX);
}
fflush(stdout);
if (ret != sizeof(recSTX)) {
printf("Not working");
fflush(stdout);
perror("read");
return -errno;
}
} while (recSTX != X_STX);
//So next, receiving blk
ret = read(serial_fd, &recBlk, sizeof(recBlk));
printf("Block Num is %d\n", recBlk);
fflush(stdout);
if (ret != sizeof(recBlk)) {
perror("read");
return -errno;
}
ret = read(serial_fd, &recNegBlk, sizeof(recNegBlk));
printf("Negative Block Num is %d\n", recNegBlk);
fflush(stdout);
if (ret != sizeof(recNegBlk)) {
perror("read");
return -errno;
}
ret = read(serial_fd, (void *)&recData, sizeof(recData));
printf("Data buffer is %c\n", &recData);
fflush(stdout);
if (ret != sizeof(recData)) {
perror("read");
return -errno;
}
ret = read(serial_fd, &recChksum, sizeof(recChksum));
printf("Check sum is %c", &recChksum);
fflush(stdout);
if (ret != sizeof(recChksum)) {
perror("read");
return -errno;
}
// data block number check
if(recBlk == 0xff - recNegBlk){
perror("read");
return -errno;
}
// data integrity check
if(recChksum == swap16(crc16(recData, sizeof(recData)))){
perror("read");
return -errno;
}
// check of appended "0xff" from end of data to end of chunk in chunk
unsigned char *ptr = recData;
ptr += sizeof(recData);
while(*ptr == 0xff){
ptr--;
}
fwrite(recData, (ptr - recData),1,fp); // write Datas bytes from our buffer
// set skip flag or end connect
ret = write(serial_fd, &eof, sizeof(uint8_t));
if (ret != sizeof(eof) || ret != sizeof(X_STX)){
return -errno;
}else{
if(ret == X_STX){
skip = 1;
}else if(ret == EOF){
printf("EOF ...");
ret = write(serial_fd, &sdACK, sizeof(sdACK));
break;
}else{
return -errno;
}
}
}
printf("done.\n");
fclose(fp);
return 0;
}
static int xymodem_send(int serial_fd, const char *filename, int protocol, int wait)
{
size_t len;
int ret, fd;
uint8_t answer;
struct stat stat;
const uint8_t *buf;
uint8_t eof = X_EOF;
struct xmodem_chunk chunk;
int skip_payload = 0;
fd = open(filename, O_RDONLY);
if (fd < 0) {
perror("open");
return -errno;
}
fstat(fd, &stat);
len = stat.st_size;
buf = static_cast<uint8_t*>(mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0));
if (!buf) {
perror("mmap");
return -errno;
}
if (wait) {
printf("Waiting for receiver ping ...");
fflush(stdout);
do {
ret = read(serial_fd, &answer, sizeof(answer));
if (ret != sizeof(answer)) {
perror("read");
return -errno;
}
} while (answer != 'C');
printf("done.\n");
}
printf("Sending %s ", filename);
if (protocol == PROTOCOL_YMODEM) {
strncpy((char *) chunk.payload, filename, sizeof(chunk.payload));
chunk.block = 0;
skip_payload = 1;
} else {
chunk.block = 1;
}
chunk.start = X_STX;
while (len) {
size_t z = 0;
int next = 0;
char status;
if (!skip_payload) {
z = min(len, sizeof(chunk.payload));
memcpy(chunk.payload, buf, z);
memset(chunk.payload + z, 0xff, sizeof(chunk.payload) - z);
} else {
skip_payload = 0;
}
chunk.crc = swap16(crc16(chunk.payload, sizeof(chunk.payload)));
chunk.block_neg = 0xff - chunk.block;
ret = write(serial_fd, &chunk, sizeof(chunk));
if (ret != sizeof(chunk))
return -errno;
ret = read(serial_fd, &answer, sizeof(answer));
if (ret != sizeof(answer))
return -errno;
switch (answer) {
case X_NAK:
status = 'N';
break;
case X_ACK:
status = '.';
next = 1;
break;
default:
status = '?';
break;
}
printf("%c", status);
fflush(stdout);
if (next) {
chunk.block++;
len -= z;
buf += z;
}
}
ret = write(serial_fd, &eof, sizeof(eof));
if (ret != sizeof(eof))
return -errno;
/* send EOT again for YMODEM */
if (protocol == PROTOCOL_YMODEM) {
ret = write(serial_fd, &eof, sizeof(eof));
if (ret != sizeof(eof))
return -errno;
}
printf("done.\n");
return 0;
}
static int open_serial(const char *path, int baud)
{
int fd;
struct termios tty;
fd = open(path, O_RDWR | O_SYNC);
if (fd < 0) {
perror("open");
return -errno;
}
memset(&tty, 0, sizeof(tty));
if (tcgetattr(fd, &tty) != 0) {
perror("tcgetattr");
return -errno;
}
cfsetospeed(&tty, baud);
cfsetispeed(&tty, baud);
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo,
// no canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
// enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
perror("tcsetattr");
return -errno;
}
return fd;
}
static void dump_serial(int serial_fd)
{
char in;
for (;;) {
read(serial_fd, &in, sizeof(in));
printf("%c", in);
fflush(stdout);
}
}
#ifdef TEST_XMODEM_RECEIVE
#endif // TEST_XMODEM_RECEIVE
int main(int argc, char **argv)
{
int a, ret, serial_fd;
printf("STX: %c\n", X_STX);
printf("ACK: %c\n", X_ACK);
printf("NAK: %c\n", X_NAK);
printf("EOF: %c\n", X_EOF);
fflush(stdout);
serial_fd = open_serial("/dev/ttyUSB0", 115200);
if (serial_fd < 0)
return -errno;
if(std::string(argv[2]) == "0" || std::string(argv[2]) == "0"){
ret = xymodem_send(serial_fd, argv[1], PROTOCOL_XMODEM, 1);
if (ret < 0)
return ret;
}
if(std::string(argv[2]) == "1"){
ret = xmodem_receive(serial_fd, argv[1], 1);
if (ret < 0)
return ret;
}
return 0;
}
Thanks for looking through my questions.

Error while reading data from serial port

I am writing a program in Visual C++ to access serial port.Code is given below:-
#include "stdafx.h"
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <commdlg.h>
int nread,nwrite;
void main()
{
COMMTIMEOUTS timeouts;
COMMCONFIG dcbSerialParams;
char *words,*buffRead, *buffWrite;
DWORD dwBytesWritten,dwBytesRead;
HANDLE hSerial= CreateFile(L"COM1", GENERIC_READ | GENERIC_WRITE,
0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if ( hSerial == INVALID_HANDLE_VALUE)
{
if (GetLastError() == ERROR_FILE_NOT_FOUND)
{
printf(" serial port does not exist \n");
}
printf(" some other error occured. Inform user.\n");
}
//DCB dcbSerialParams ;
//GetCommState( hSerial, &dcbSerialParams.dcb);
if (!GetCommState(hSerial, &dcbSerialParams.dcb))
{
printf("error getting state \n");
}
dcbSerialParams.dcb.DCBlength = sizeof(dcbSerialParams.dcb);
dcbSerialParams.dcb.BaudRate = CBR_1200;
dcbSerialParams.dcb.ByteSize = 8;
dcbSerialParams.dcb.StopBits = ONESTOPBIT;
dcbSerialParams.dcb.Parity = NOPARITY;
dcbSerialParams.dcb.fBinary = TRUE;
dcbSerialParams.dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcbSerialParams.dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcbSerialParams.dcb.fOutxCtsFlow = FALSE;
dcbSerialParams.dcb.fOutxDsrFlow = FALSE;
dcbSerialParams.dcb.fDsrSensitivity= FALSE;
dcbSerialParams.dcb.fAbortOnError = TRUE;
if (!SetCommState(hSerial, &dcbSerialParams.dcb))
{
printf(" error setting serial port state \n");
}
GetCommTimeouts(hSerial,&timeouts);
timeouts.ReadIntervalTimeout = 1000;
timeouts.ReadTotalTimeoutConstant = 1000;
timeouts.ReadTotalTimeoutMultiplier = 1000;
timeouts.WriteTotalTimeoutConstant = 1000;
timeouts.WriteTotalTimeoutMultiplier= 1000;
if(!SetCommTimeouts(hSerial, &timeouts))
{
printf("error setting port state \n");
}
//****************Write Operation*********************//
words = "B";
nwrite = strlen(words);
buffWrite = words;
dwBytesWritten = 0;
if (!WriteFile(hSerial, buffWrite, nwrite, &dwBytesWritten, NULL))
{
printf("error writing to output buffer \n");
}
printf("Data written to write buffer is\n %s \n",buffWrite);
//***************Read Operation******************//
buffRead = 0;
dwBytesRead = 0;
nread = strlen(words);
if (!ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL))
{
printf("error reading from input buffer \n");
}
printf("Data read from read buffer is \n %s \n",buffRead);
CloseHandle(hSerial);
}
Above program is working properly while write operation (i.e writing data on serial port) but while read operation its is not reading data from serial port.
I am getting output on console window is given below:-
Data written to write buffer is
B
error reading from input buffer
Data read from read buffer is
<null>
I want to know where I am getting wrong and how to resolve it.
The second ReadFile parameter cannot be NULL. It should be valid pointer to some buffer, for example:
dwBytesRead = 0;
nread = strlen(words);
buffRead = new char[nread + 1];
memset(buffRead, 0, nread+1]; // ensure that string will be null-terminated
if (!ReadFile(hSerial, buffRead, nread, &dwBytesRead, NULL))
{
printf("error reading from input buffer \n");
}
...
delete[] buffRead;